Solving Linear Equations How to solve linear equations? General form: Ax = b, where A is a matrix, x is a vector of variables you want to solve for, and b is a vector of constants. So, the system of linear equations is when you have more than one variable and the same number of equations. One way of defining the problem is just writing the equations down explicitly like this or writing them down in a matrix form. So in this example,
A = [3 2 -1; 2 -2 4; -1 0.5 -1]b = [1 -2 0]
A =
3.0000 2.0000 -1.0000
2.0000 -2.0000 4.0000
-1.0000 0.5000 -1.0000
b =
1 -2 0
x = inv(A) * b'
x =
1.0000
-2.0000
-2.0000
inv(A)inv(A) * b'
ans =
2.2204e-16 -5.0000e-01 -2.0000e+00
6.6667e-01 1.3333e+00 4.6667e+00
3.3333e-01 1.1667e+00 3.3333e+00
ans =
1.0000
-2.0000
-2.0000
A\b'
ans =
1.0000
-2.0000
-2.0000
Now let's look at some other linear algebra concepts that we can do in MATLAB. For example, taking the determinant of the matrix. It's a simple function.
det(A)
ans = -3.0000
eig(A)
ans =
3.6295
-3.8445
0.2150
[V, D] = eig(A)
V =
-0.9601 -0.3061 -0.3288
-0.2098 0.9134 0.7469
0.1847 -0.2682 0.5780
D =
Diagonal Matrix
3.6295 0 0
0 -3.8445 0
0 0 0.2150
A*V == V*D
ans =
0 0 0
0 0 0
0 0 0
==
as we do in regular programming environments.
1 == 11 == 0
ans = 1
ans = 0
A*V == V*D
, we get false, which can be surprising because we expect them to be equal according to the definition of eigenvalues and eigenvectors. But here we have to remember that in programming there is always some round-off error. So, if we simply subtract A*V - V*D, we get back very small numbers.
A*V - V*D
ans =
3.5527e-15 1.3323e-15 1.2906e-15
-2.7756e-15 -1.3323e-15 -7.2164e-16
1.1102e-16 -4.4409e-16 1.2490e-16
norm(A*V - V*D)
ans = 5.0950e-15