MATLAB Tutor
Part 10: Solving Linear
Systems
Suppose we want to solve the linear
system
x – 2y + z
= 0
2y
– 8z = 8
-4x + 5y + 9z = -9
We can solve this system in several
ways in MATLAB; you will learn two of them in this part and another in the next
part.
- First we need to save the coefficients
of the system in a matrix, and the right-hand side vector in another matrix.
Enter
A = [1 -2 1
0
2 -8
-4 5 9]
b = [0; 8; -9]
and then enter
X = A\b
The three numbers you see are the solution values of x, y, and z.
- You can easily check whether the
solution is correct. The matrix product A times X should equal
the right hand side b. Does it? Check it out.
- If you have MATLAB's Symbolic Math
Toolkit, you can also use the "solve" command to solve the above system.
Enter
syms x y z
eq1 = 'x - 2*y + z = 0';
eq2 = '2*y - 8*z = 8';
eq3 = '-4*x + 5*y + 9*z = -9';
[x,y,z] = solve(eq1, eq2, eq3)
- When you have a system with
fewer equations than unknowns, you can find the symbolic solution for some
of the variables in terms of others, which we could call "free" variables.
Consider the following system of three equations in four unknowns.
x – 2y +
z + 2w
= 0
2y
– 8z + w = 8
-4x + 5y + 9z - w = -9
We can solve for x, y, and z in terms of w.
The last entry in the "solve" command tells which variables are to be solved for, and
hence which variable will be free. Enter
syms x y z w
eq1 = 'x - 2*y + z + 2* w = 0';
eq2 = '2*y - 8*z + w = 8';
eq3 = '-4*x + 5*y + 9*z - w = -9';
[x,y,z] = solve(eq1, eq2, eq3, 'x,y,z')
- Try the same two methods of
solution on the system
x + 4y + 3z = 10
2x + y – z = -1
3x – y – 4z = 11
What do the results tell you? We will look more carefully at this example
in the next part.
|
CCP Home |
Materials | Linear Algebra | Module Contents | Back | Forward |