Solve a Linear System of Equations; Eigenvalues and Eigenvectors

In today’s lesson, we will be covering the following topics:

1. Solving a Linear System of Equations

2. Eigenvalues and Eigenvectors

VIDEO COMING SOON

Solving a Linear System of Equations

MATLAB can help you solve a linear system of equations, which means solving for all the unknown variables in a set of equations. This can be done in two ways – manually or using the built-in function linsolve or solve. This table below shows linsolve as well as other matrix analysis built-in functions that are available. Let’s do example using all of these methods.

Solving a System of Linear Equations using Linsolve

To learn how to solve a system of linear equations, let’s use this system of equations below:

Now we will get MATLAB to give us the answers to x, y and z using linsolve. The code I would use is shown in the image below, highlighted in yellow. I have put comments in green, which explain what each line of code is. The answer is highlighted in red.

Let’s go through this code line by line.

syms x y z 

This line uses the built-in function syms, and the letters after it need to be the variables used in your system of equations. For the example we’re using, the three equations contain the variables x, y, and z. Therefore, I used syms x y z.

a= 2*x + y + z == 2;

b= -x + y – z == 3;

c= x + 2*y + 3*z == -10;

These three lines of code are the equations in our system. I have typed out the equations, then used double equal signs to set them equal to the values on the right hand side. It will not work with a single equals sign because you are not setting one variable equal to a number, but rather setting entire equations equal to a number. I then set these equations equal to a, b, and c, on the left side of the equations with single equal signs. This is not absolutely necessary, but it makes it easier to type in the next line, so you don’t have to retype the entire equations.

[A,B] = equationsToMatrix([a b c], [x y z]);

This line uses the built-in function equationsToMatrix to convert the system of equations into a matrix. To use this function, you need to set it equal to [A,B], which are the A and B used in the equation Ax = B. You can use any two variables you want within the square brackets, so I left them as A and B because they are the default variables. Then after typing equationsToMatrix, need to use round brackets with two sets of square brackets inside separated by a comma, like this ([ ], [ ]). Within the first set of square brackets you need to list the equations you’re using (in our case, these are equations a, b, and c). Within the second set of square brackets you need to list the variables used in your equations (in our case these were variables x, y, and z). This then becomes ([a b c], [x y z]).

answer=linsolve(A,B)

This last line of code finally uses the linsolve function. I set it equal to the variable answer, but you don’t have to unless you want the final answer to have a variable assigned to it (meaning the answer to this code will be stored within the variable answer below). Next to words linsolve you will need round brackets that contain the variables you used to create your matrix (recall I had used A and B above).

After this, your code is complete! The final answer after running the code gives you the answer, which is in the order x, y, z. In the Command Window below, it is highlighted in red. This means x = 3, y = 1, and z = -5.

This is the code I explained above

Now here is another way to get the same answer:

Rather than typing out each equation and setting it equal to a, b, and c (or whatever variables you decided to use) , you can directly use these equations with equationsToMatrix, where you still need to have two sets of square brackets within round brackets like ([ ], [ ]), but the first set of square brackets will contain the equations, using the double equals sign on the right hand side of the equations to set them equal to the correct values.  This shortens the number of lines of code you need, and also gives the correct anwer.

Second way to get the answer with linsolve

Solving a System of Linear Equations using Solve

To solve a system of linear equations using the built-in function solve, we first use the same couple lines of code – syms, as well as defining all the equations. Let’s go through the new code that we didn’t use with linsolve. This is referring to the image below.

sol = solve([a b c], [x y z]);

This is used similar to equationsToMatrix, but you just use the word solve instead. The rest of the code is the same as you would use with equationsToMatrix. You also can, but don’t have to, use commas in between the letters in the square brackets (it can be [a,b,c] or [a b c] and so on). The one difference is that you should set this equal to a variable so it can be used in the next line, so I set this equal to sol.

X=sol.x

Y=sol.y

Z=sol.z

These three lines are used rather than linsolve. You need one line with sol for each unknown variable. There are three unknown variables in our set of equations – x, y, and z. So there are three lines using sol. For each of these variables, use sol.variable – replace the word “variable” with each specific variable. This becomes sol.x, sol.y, and sol.z. I set them equal to the values X, Y, and Z respectively, so in the output window you can tell which variable is equal to what (otherwise MATLAB will automatically assign ans to each answer). You can see I have highlighted X = 3, Y = 1, and Z = -5, in red below.

Code using solve is explained above

Solving a System of Linear Equations without Built-In Functions

There is another way to solve a system of equations without using Linsolve or Solve. These steps are the same as the steps shown using linsolve above, and you can do it either way shown (there were two different ways shown with linsolve) up until the equationsToMatrix line. After that line, you will see I used A\B, highlighted in yellow. Once MATLAB converts the system of equations into a matrix, use A\B, or if you want to set the equationsToMatrix line equal to something else like [C,D] or other variables, those are the ones you’ll have to use with the backslash. A and B are the default though, from the equation Ax = B. I have highlighted the answer in red, and it outputs the same answer as using either solve or linsolve.

Eigenvalues and Eigenvectors

When we get a set of data points, we can deconstruct them into eigenvalues and eigenvectors. Eigenvalues are numbers that tell you how much variance is in a dataset in a specific direction, and an eigenvector is a direction that tells how much variance is in the data (how spread out the data is). Each eigenvalue has a corresponding eigenvector. Sometimes you will see eigenvectors being referred to as left or right eigenvectors, but but default, the term “eigenvector” refers to the right eigenvector. Here’s a picture illustrating the difference between a left and right eigenvector – they depend on the order of multiplication with a matrix:

To find the eigenvalues of a matrix in MATLAB, use eig(matrix), where you will replace the words “matrix” with the variable of your matrix. For example, I have created a matrix A in the image below and used eig(A). The output, highlighted in blue, gives the eigenvalues of this matrix. The reason there are 3 values in the output is because the matrix is 3 by 3 (there are 3 rows and 3 columns), and there is one eigenvalue for each dimension (Eg. 3 x 3 means it is 3 dimensions, so 3 eigenvalues).

To find the corresponding eigenvectors, use [V,D]=eig(A) – it doesn’t have to be A, it will be the variable for the matrix you have. You can also replace V and D with any variables you’d like. This returns the eigenvectors, highlighted in red. The eigenvectors are output as columns, and it also results in the eigenvalues as a matrix right next to it, which is why you see the blue highlighted eigenvalues next to the red. So if you’re using the [V,D] line of code to ge the eigenvectors, you don’t really need to find the eigenvalues separately using eig(A), because they are output with the eigenvectors anyways.

Note that the eigenvectors corresponding to each eigenvalue go in order of the first column in the matrix being eigenvector 1, and the corresponding eigenvalue is the number in the first eigenvalue. The eigenvalues are also shown down the diagonal of the matrix where I highlighted them in blue, and the first value down that diagonal is 13.0873.

To find the left eigenvectors specifically, use the following code. Define a matrix (mine is called A, same as the example above). Then use [V,D,W]=eig(A) or replace A with the variable your matrix is called. This will output the eigenvalues (in blue) as well as the left eigenvectors (in purple) and right eigenvectors (in red). You don’t have to use V, D, and W, but you can use any 3 variables there.