One of the most useful applications for MATLAB is to solve systems of linear equations. You know, those problems where you’re given a series of equations and are asked to find the input values that solve all of them. In this video, learn how to solve those systems of equations in MATLAB.
- [Instructor] One of the most useful applications for MATLAB is to solve systems of linear equations. You know, those problems where you're given a series of equations and are asked to find the input values that solve all of them simultaneously. In this movie I will save you a lot of work and show you how to solve those systems of equations using MATLAB. I'll start in PowerPoint and then switch over to MATLAB itself. So once again, you can solve systems of linear equations through substitutions or by letting MATLAB do it for you.
So let's say you have the following system of equations. X plus three y minus two z equals five, three x plus five y plus six z equals seven, and two x plus four y plus three z equals eight, and your job is to find the values that solve those three equations simultaneously substituting in x, y, and z. The great thing about working in MATLAB is that there is a method using matrix algebra that allows you to find the result.
So what you need to do is to represent the factors of the first equations. That is, not the solution just the equations themselves. So you have one times x, three times y, and minus two times z. That's in the first row of your matrix. Then you have three x, so you have three, five, six, and then two x plus four y plus three z, so you have two, four, three, and then you create a vector that has those solutions. Five, seven, and eight, and that is a column vector.
You can combine all of those items together and use the linsolve function to solve the linear equations for you. For three-by-three equations, it is saving you some work. For larger circumstances; larger sets of equations with a lot more variables, it's saving you a lot of work. Now let's switch over to MATLAB and see how to implement the solution there. I'm in MATLAB, and I have a blank command window. The first thing I need to do here is to assign my three-by-three matrix and my three row column vector to variables.
So I'll type mat1 equals and then in square brackets I'll have one, three, and minus two. And again, these are the factors by which we're multiplying each of the variables in the original equations. So we have x plus three y minus two z, then we have three, five and six, semicolon for the next row, and two, four, and three, followed by a right square bracket to close out. Enter, and there are my values, and for matrix two, which is actually a column vector; three by one, I'll type equal, because I'm assigning, left square bracket, and then I will have five, semicolon to go to the next row, seven, semicolon, and eight.
And again, those are my desired solutions and enter. Good. Now to solve them or to show that there is no solution I will use linsolve, and that is for linear solving. L-I-N-S-O-L-V-E, left parentheses, and just the name of my two matrices. Matrix one contains the factors, so that is mat1, then a comma, and then the answers of the linear equations, mat2, right parentheses, and enter, and I get my values of minus 15, eight, and two.
Now switch back over to PowerPoint to verify that those numbers actually work. So once again, our equations were x plus three y minus two z equals five, three x plus five y plus six z equals seven, and two x plus four y plus three z equals eight, and we're substituting in the values minus 15 for x, eight for y, and two for z. So here we go. For the first equation, we have one times -15 plus three times eight minus two times two, which is -15 plus 24 minus four which is five.
So we had 24 minus four is 20 and subtract 15 from that and we get five. Then we have three times -15 plus five times eight plus six times two where we have -45 plus 40, that's minus five running total, plus 12 is seven, and finally, we have two times -15 for 30, four times eight for 32, and three times two for six. - 30 plus 32 is two as a running total plus six equals eight, and we can see that we have in fact found our solution, and wasn't that a lot easier than doing it by hand?
- Defining variables and contains
- Exploring operators
- Summarizing with built-in functions
- Generating random numbers
- Defining vectors and matrices
- Accepting input in scripts
- Writing and reading data from external files
- Creating custom functions
- Using conditional logic
- Repeating operations with loops
- Working with text strings
- Plotting data and function output
- Formatting, saving, and printing plots
- Using statistical and matrix functions