 In this lecture I want to look at the rotation of rectangular coordinates. So we're going to have this coordinate system X, Y. We're going to rotate it through certain degrees and then we want to express the coordinates of a vector in this new coordinate system. The pictures will make life easier. It'll make it easy to understand what we're trying to do. First of all, as per usual, we're going to import some libraries here in Python using the IPython notebook. So from SMPI I'm going to import in a printing for Milatic. Nice printing to the screen. I'm also going to import symbols because I want to change some variables into mathematical variables. And I'm also going to use a matrix. I want to import the physics vector libraries as well. So from that I import star. That means everything. I want to display some images to the screen. So I've got to import ipython.core.display. And from that I'm going to import the image function. And from warnings as always, filter warnings is something I always do. So let's initialize the pretty printing and we ignore the filter warning. So let's just look what happens in two space. We're going to start with that. So we're going to consider a Cartesian coordinate system, X, Y. Normal one, you learn from school. And we're going to rotate it counterclockwise through SATA radians to create a new coordinate system, which I'm going to call capital X, Y. We're going to have a vector in red that has coordinates X, Y. And we want to express it in the new coordinates. What do I mean by that? Let's run these blocks of code. So I'm just saying image, which is what I've imported up there. And it takes an argument filename equals and then in inverted commas. We have the filename and this file is stored in the same folder as this ipython notebook. And this is what we have. There we go. So now it makes life a bit easier. There's my X axis. There's my Y axis. And I'm rotating it through an angle of SATA degrees so that my new X axis is there. Capital X and my new Y axis is there. And here's my red vector OB here, O2B. They see the point B there and I want to express it. So if I look in the normal lowercase X, Y coordinate system, obviously this vector is going to have components X there at point A. I've just indicated and it's going to have a Y coordinate right there. And if I rotate it, if I rotate the reference frame, this red vector is going to have a new X component, which is going to be down here, noted capital X at point C. And then parallel to the X axis there, it's going to have obviously a Y component. And all I'm stating is a new angle phi, which is the angle the vector now makes with the new uppercase X axis there. So in essence, I just want to show you the two triangles that I formed here, OAB, which is a 90 degree right angle down here, and triangle OCB, OBC here, the light blue one as opposed to the slide orange one at the bottom. Also right angle, but with this phi angle between the X axis, the new uppercase X axis and the vector. So let's just consider what happens in vector OBC. So OBC, this blue one with this phi. Let's have a quick look what happens there. So this is just the file I'm importing again. This file is in the same folder. So I can just reference it directly. If I look at the cosine of phi, the cosine of phi, well that's going to be X. That's a norm, the length of this component divided by R. Now I'm just stating that R is the length. I'm just putting R as the length, the magnitude of this vector OB here. So if I were just to do some algebraic manipulation, I have the fact that X equals R, cosine, theta, phi I should say, just taking the R to that side. And I also note that the sine of phi, the sine of phi is Y over R in this little, little, remember. So that there is that component along the new Y axis. That's the same as this length here. So that's also Y. So it's Y divided by R. And if I just get Y on its own, it's that. Now in the bigger triangle, the OAB triangle, let's run that. Let's see what happens there. I'm now talking about the combination of those two, theta and phi. So the cosine is going to be this lowercase X divided by R and sine is going to be this lowercase Y. Remember there's the Y coordinate divided by R. Nothing special there. And also just expressing X on its own and Y on its own. There's no problem. Now I'm going to make use of trigonometric identities for double angles. If I have the cosine of the sum or difference of two angles, I can use trigonometric identities and the same goes for the sine. And this is what it is. Remember the cosine of the addition of two angles becomes the cosine of one angle times the cosine of the other angle minus the sine of one angle and sine of the other angle. And for the addition of two angles, the sine of that addition becomes the sine of one angle, the cosine of the other one plus the cosine of the first one and the sine of the second one. Those trigonometric identities have to remember. And it's R times that. Remember it's R times the cosine and R times the sine. So it's R times this new identity and here we have R times the old identity. If I distribute this R in and I just grouped the terms, I'm going to put this R with this cosine phi, that R with that cosine phi and then I still have cosine theta and minus I'm going to put the R with the sine of phi and I still have the sine of theta left. I do remember though that R cosine phi, if I look up there, R cosine phi equals upper x and R sine phi equals upper y so I'm just putting that back into these two. Now I have lowercase x equals uppercase x cosine phi minus y sine theta minus y sine theta and lowercase y here equals x uppercase x sine theta plus y cosine theta. All you have to do now, I'm not going to do it here, just solve for uppercase x and uppercase y. So get uppercase y on its own here by taking it to this side, taking negative x to this side, dividing both sides by sine theta so that I have y on its own. Uppercase y and substitute that into this one, then you're going to get an expression just for uppercase x. Do the same and solve for uppercase x. If you do that, it's very easy algebraic manipulation. You're going to get this. That uppercase x equals x cosine theta plus y sine theta. So all we have here is the old coordinates x and y and the angle. Same for the new y coordinate. We just have the old x and y coordinate values and the angle of rotation. Phi is missing from this. And usually we express this in matrix form. So we have this column matrix x and y equals this matrix times that matrix. Now if you do this matrix multiplication, this is a 2 by 2 matrix, a 2 by 1 matrix so the result will be a 2 by 1 matrix. 2 rows, 2 columns, 2 rows, 1 column will result in 2 rows, 1 column as we have there. So it will be cosine x plus x cosine theta plus y sine theta and that's what we have there for x. And y will be x minus x sine theta plus y cosine theta just as we have there. And this is our matrix of rotation. So I can plug in any xy coordinate there, multiply it throughout by this and I'm going to get the new coordinates. It's exactly the same, I can do it by hand there or I can do this matrix multiplication here. So given any coordinates for the vector in the xy plane above rotation matrix can be used to express the vector coordinates in the new uppercase xy plane. It's so simple to do. So let's say example time. I'm going to introduce the Greek letter theta as a variable in sympi. So I equal it to symbols theta there. Let's run that. Now again I'm going to create a reference frame. I'm going to call my first reference frame c, that's my lowercase xy and the uppercase xy I'm going to call a so that I have two reference frames. I can now express my vector in either of these two reference frames. So we've got to tell Python now how these two are connected to each other. That a is just c rotated through theta degrees. Now we use the right hand rule now. Suddenly see the xy plane in three space. Remember there's a z axis as well. If you point your thumb up the z axis you will note that your right hand fingers curl in the counterclockwise rotation. So in the counterclockwise rotation imagine if I looked at this again and the z axis comes out of me, out towards you from this point, out towards you. This rotation takes, when you put your thumb towards yourself this rotation is taking place counterclockwise and it makes a positive rotation. Okay so this is how we do it. This is the code. We say a.orient. We're going to orient a according to c. So it's not c according to a. We're going to say a is oriented based upon c in the three arguments here. One, two, three where the last one is actually a combination of two arguments. The first argument is what is the base reference frame from which you are doing the rotation. It's from c. What kind of rotation is it? It's a string. So you've got to put it in quotation marks. Axis. We're going to do a rotation along a certain axis and then we tell it by how many degrees. Theta degrees. And along what axis? Well that is along c's z axis. Again imagine this comes out of you at the screen. So along the z axis I'm going to imagine there's a pin in there. I'm just going to rotate around that axis. Axis. So there we go. That's how we do that. And there is now, if I run that, Python has now gone ahead and it's coupled those two reference frames. It knows which one is which. And now it becomes so easy to make this rotation matrix. We just say a.dcmc. So we're going to express vectors inside this new reference frame based on the old one. So again just note which way around you do your a's and c's. And there we have it. Now it's doing it in three space. But remember there was our two space one. The cosine sign, negative sign and cosine. They're still there. Cosine sign, negative sign, cosine. But because it's in three dimensions it adds these zeros and ones there. You'll see if you do multiply then by an x, y, z. Because now you're going to have a three by three matrix. You better have three rows here. You just put zero for the z because this is in two space. So that isn't a big problem. And there you have it. If you explain to some pi what kind of rotation you want to do it will give you this. You can multiply it. Let's do that. And I thought a good time to introduce you to how to set up a function in Python. A function is a short little program. And if you call that function name it's going to execute that little program and actually give you an answer. And this is how you set it up. You use the keyword dev. That's for define a function. Now this is not the same as a mathematical function. This is a computer function, a bit of code that gets executed. You give it the name and I'm calling it angle. And that angle you tell it what kind of arguments it takes. So it takes, I've just called it whatever I wanted. I called it CX, CY and ROT. So I'm creating this function name and the arguments that it takes. Then you put a colon. If you hit enter after that colon it's going to give you white space there which is the way Python tells itself that this is a contained loop of code that it has to run through. So the first thing we're going to do we're going to have A. We're going to orient it up from C along some axis. And the angle I'm going to call ROT and the axis through which I do it is CZ. Then my function is going to not all functions do, but this function is going to return a value once it's finished. And what it does it's going to return this rotational matrix which is this one up here by a matrix that contains CX, CY and zero. So you can imagine what's going to happen here. I can enter a value because the CX, CY and zero these are, except that's a constant but CX, CY are just computer variables. So I can attach a value and number to that. So that is going to be this column here. Imagine I said 5 and 4 and zero. So if this original vector had components 5 and 4 I can write that as a column vector 5 and 4 and that's what I'm doing here. It's a matrix. Now look at the matrix, how you do matrix. Remember we imported it up here. It's part of SMPI. It takes, where did we go? There we are. It takes open and close parentheses. Then open and close square brackets and then every row goes inside of its own square bracket separated by a comma. So this is going to be one, two, three of those. So it's going to multiply that by the rotation matrix. I'm actually doing exactly what is in this picture in this image here. I'm doing that multiplication there. Now I'm introducing this. I'm actually unsure if this really works in Python 2.x. This is 3.4.1. So it might not work if you're running 2.7 or earlier editions of Python. So Cx is going to be a float. That means a decimal value. And what is the decimal value? It's going to be input, open and close parentheses. The string value. Input is something that gets printed on the screen and it waits for you to enter a value. And that value that you enter is going to be attached to Cx. Then it's going to ask you for a Y component, CY and then an angle of rotation. So now you have values for Cx, CY and ROT. Now I'm calling, remember there I call this angle. I'm calling that definition. So it's going to execute this. For Cx, CY and ROT it's going to take these three values, remember which I'm going to give it. Which I'm going to give. So let's run that piece of code. Let me show you how this works. So look at that. So beautiful. It asked me for an X component. So let's say it was 5 in that in the X direction. I hit enter, the next line of code. So that 5 was attached to Cx. So this Cx now will be 5. Let's say 4, enter and the rotation in radians. Now unfortunately I have to do this in decimals because I'm calling floats are decimal values. I think a pi over 6, which is about 0.524. Roundabout there. Before I hit enter, so I've got these three values now. It's going to call this definition and with those three values it can now fill in the rotation there which is about pi over 6. It will do the matrix for me and it will multiply by this column vector, this column matrix containing the points 540. 540. So let me hit enter there and there you go. In the new coordinate system the XY components are going to be about 6.3 and about 0.96. Beautiful. And you can see clearly there if I rotate it by something like this. This new X component is obviously going to be slightly longer than the old one so while this was 4 or 5 this will probably be about 6 something now and while this one was 4 you can see the new one is much shorter. So this is obviously not too scale but you can see through this rotation this was going to become shorter so this actually makes some intuitive sense. So you've now seen how to write a little program. You know how to connect the rotation of one reference frame onto the other and how to work out this rotation matrix. That's really wonderful stuff. SimPy makes things so easy to do.