Posted on 1 Comment

Learn Matlab Episode #5: Linear Algebra

Click here to subscribe for more videos like this!

Linear Algebra in MATLAB

So in this matlab tutorial we’re going to talk about some basic linear algebra in matlab. The first thing I want to talk about is how to solve linear equations. So in general these are in the form of A(X) + B where A is a matrix, X is a vector of the variables you want to solve for, and B is a vector of constants. So if you remember from linear algebra a system of linear equations is when you have more than one variable and the same number of linear equations. So, in this example I’m looking at right here the variables are X, Y, and Z and because there are three of them I have three different equations. So one way of writing the problem is just writing the equations out explicitly like this, or you can write it out in matrix form. So, in this example A = [3 2 -1; 2 -2 4; -1 0.5 -1]; OK, so that’s A, b = [1 -2 -2]; ok. So, when you’re working with equations like this you might be tempted to do just simple algebra, so you could say x = inv(a)*b because mathematically that works. So, I can do that oh and right I have to make b a column matrix, so a*b. Sorry, I used the wrong b so inv(A)*b so I get 1 -2 -2 which is the answer given on Wikipedia. So sometimes when you try to do something like this matlab will give you a warning this is a very inefficient way of calculating this answer. It’s matlab already contains algorithms to solve this problem. So if you remember from before when we were talking about operations we were talking about the division operations and how there’s a forward slash division and a backslash division. So, this is the use case where you want to use the backslash division. I mean if you kind of think about it if these were scalars you know x would be b/a, but since they’re matrices we write the answer like this so A\b. So, it’s kind of like A is underneath the b, right, and so if I do that you can see that I get the same answer as when I did inv(a)*b. So that’s how you solve systems of linear equations in matlab, So now we’re going to talk about some other linear algebra concepts that you can do in matlab. One of them is taking the determinant of a matrix,, and so that is just a simple function called det, ok. So remember A = [3 2 -1; 2 -2 4; -1 0.5 -1]; from the last example we can also compute the eigenvalues of a matrix. So, iaeig and now you know when we compute the eigenvalues we usually want the corresponding eigenvectors. So one interesting thing about matlab that differs from other languages is that what is returned by the function is not set in stone, its dynamic. So if we look at the documentation for the eig function we see that if there is one output parameter it returns this little e, and the matlab documentation says e contains the eigenvalues of the matrix a. But, say I want the eigenvectors also so you can see these different function signatures. So if I assign the returned values of eig to two different variables I can get back let’s read here a diagonal matrix D of eigenvalues in a matrix V whose columns are the corresponding right eigenvectors. So the eigenvalues are going to be returned in a matrix called d, and the eigenvectors are going to be a columns of the matrix V. So let’s try that. [V,D] = eig(A) so we get a diagonal matrix D which has the same eigenvalues as before, and then the eigenvectors are all in V. So let’s just confirm that the definition of eigenvalues and eigenvectors holds, so A*V should equal V*D right, and so here’s another thing interesting about MATLAB is that if we want to check if two things are equal we can generally use the == as we do in regular programming. So 1 == 1 is 1 which means true, 1 == 0 is false. So if we do A*V == B*D we get false which is surprising because they should be equal given that that’s the definition of eigenvalues and eigenvectors, but we have to remember that there is always in programming some round off error. So if we subtract A*V – V*D we should get back a very small number, alright. So, we do that we see 1×10^14 which is indeed very small, so for all intents and purposes they are still equal even though equals equals returns false. Now so that brings us to the next thing I want to talk about which is so we have this matrix A times V minus V times D which kind of represents all the differences in those two matrices, and so we want to ensure that all the values are small. Now there are nine different values in this matrix so it’s kind of hard to tell how big or small it is collectively. So one thing we want to do sometimes when we have vectors or matrices is we want to calculate the distance from the origin, and so we can do that with vectors with matrices we call that the norm, and so typically all the elements so in a euclidean space for example it’s all the elements squared, added together, and then you take the square root. So in matlab there’s a function called norm that does this. So let’s just see what that gives us. So it gives us one very small numbers, so 5×10^-15 which we again can use to confirm that it’s very close to zero, so A*V is very close to equal to V*D, and if you remember from your math courses there are different kinds of norms. So I talked about the Euclidean norm which is a norm 2, we can actually do a p norm which is so instead of taking the square and summing them all together and taking the square root we do every element to the power of p, sum them all together and take the p of root, and that’s just the second argument of norm. So if I put a 2 there I should get the same answer because p equals 2 by default. So you can’t use P =3 you can only use P = 1 to infinity or fro, and so one would be the Manhattan distance.

Posted on

Learn Matlab Episode #3: Basic Arithmetic

Click here to subscribe for more videos like this!

QUESTION: How do you perform basic arithmetic in MATLAB?

So in this matlab tutorial we’re going to go through some basic arithmetic in matlab. First I’m going to talk about the usual operations you see in programming which are plus, minus, multiply, and divide, and also the use of parentheses. So I’m going to first create some matrices for us to use, OK. So suppose I want to add two matrices, that’s very simple A + B. I want to subtract two matrices A – B. I want to multiply two matrices that’s A * B. So one thing to note about multiplication is this is pure matrix multiplication which means the inner dimensions of the matrices of A and B have to match. So, for example, if I create another matrix which is, if I create another matrix which doesn’t have the same number of rows as it has columns it’s not going to work. So, the size(B2) is 3 by 2 and the size(A) is two by two, so if I try to multiply A * B2 I’m going to get an error that says inner matrix dimensions must agree. So this does mean though I can do this, right? So, I transpose B2 it’s 2 by 3 and so a two by two matrix times a 2 by 3 matrix will give me a 2 by 3 matrix. So you can also divide matrices with the forward slash, but if you remember from linear algebra there isn’t really a matrix division like that. So, if you want to know what it means you can look it up on the internet, there’s a forward slash division for matrices and there is also a backslash division which we are going to cover in a later tutorial. By the way, if you want to divide two numbers that’s very simple we use the / for that. So the next thing we’re going to talk about is order of operations. It’s generally what you’d expect, anything in parentheses happens first, and then multiplication and division, and then addition and subtraction. So if I have A + B * C I would get that. So if I did B times C and I assign it to a variable D and I add that to A I get the same thing. So you can confirm that the multiplication is happening first. If I wanted the addition to happen first I would put parentheses around them, so we can confirm that what happened in the parentheses came first. So another thing you might want to do when you’re implementing an algorithm or writing some code is element by element operations. So we’ve looked at matrix multiplication which kind of does you
know if you’re looking at row I column J of the output that actually does a dot product between row I of the first matrix and column J of the second matrix. Something we want to do often when we’re coding is element by element multiplication, and so that lets just look at what we have for A & B again. So that’s pretty simple in matlab you just put a .* and so it multiplies each element by each corresponding element in the other matrix. This also works for division so a ./ So if you remember from your linear algebra classes there some different types of multiplication that you can have when it comes to matrices and vectors. First one were going to talk about is the inner product. I have x = (1, 2, 3) y = (4, 5, 6) and I want to do an inner product between these two vectors. One way to do that is simply by using what we already know. So x is a one by three vector and y is a one by three vector and I want the result to be one by one which is a scalar that means I can transpose y and multiply x by y as matrices, right, and so that would be 1 times 4, plus 2 times 5, plus 3 times 6. Another way you can do the dot product in MATLAB is by using the dot function. So we will cover functions more in a later lecture but I will introduce you to some today. So another product you might be interested in is called the outer product, and so that would be just the opposite of what I did before. So instead of transposing y and leaving x alone I’m going to transpose x and leave y alone. So that would give me a three by one vector times a 1 by 3 vector, and so the result would be a three-by-three vector. So that’s the outer product and there is no function for outer product in matlab, and so one thing to note is that usually when we represent vectors when you’re reading a textbook or sitting through a lecture, the vectors are column vectors so they have many rows but only one column, and so that’s kind of the opposite of what I’ve done just now, and so for the inner product you usually see x transpose times y and then for the outer product you see x times y transpose. So, just the opposite since I’ve been using row vectors. Lastly, there is the cross product and so the cross product is the magnitude is the magnitude of the first vector, times the magnitude of the second vector, times the sine of the angle between them, and then you use the right hand rule to determine the direction of the result. Alright, so dot product or inner product is not a vector so it doesn’t have a direction, but the cross product gives you back a vector and so that’s pretty simple to do in matlab, also. It’s just a function called cross.