Solving Linear Equations
Learn how to solve a system of linear equation in MATLAB.
Published: 3 Nov 2020
In today's MATLAB Basics, I want to show how to solve a simple set of linear equations. The equations we'll be solving today are shown here-- 2x equals 3y plus 1 and x plus y equals 4. Since this is MATLAB, or Matrix Laboratory, we're going to want to get this into a matrix format.
We can do this by rearranging the top equation to gather all the x's and y's on one side. Once we do that, we get it into the matrix form. To enter this into MATLAB, we'll use the following notation, where we're going to call the left-hand matrix, A, a two-by-two. We'll call our x and y variables matrix B. And then we will call the right-hand side, matrix C.
To enter this into MATLAB, we will use two lines of code. Let's actually type those in. We type these in as a is equal to, and then square brackets for a matrix. We're going to go across the columns-- 2 minus 3. A semicolon to drop down to the next row-- 1 and 1. And then we're going to enter the C matrix by saying, c is equal to 1 and 4.
When we do that, we see that we got a row vector instead of a column vector. We can hit up arrow, and then put an apostrophe. And that's going to transpose our matrix.
Finally, if we want to solve this, we can simply say that b is equal to a back-divide by c. And we'll get our result. If we want to look at this result as a fraction, we could change our format-- format rational. And now we see it as a fraction.
I'm going to go back to my normal format. And we can finally test this. We can say, a times b. And we'll see that that is the right-hand side. Thank you.