Transcript for:
Exploring Parametric Curves with MATLAB

In this video we're going to look at parametric curves, or also known as vector-valued functions, and we're going to study them using MATLAB. An important ingredient right now is that MATLAB is not tested in APS-C 171. So this is entirely for your information and for those who have a bit of a programming background or are interested in developing one, which you all should given your engineering program, this is a way to dip your toe into the programming. side and tie it back to some of the mathematical ideas we're exploring. So what we're going to do is we're going to run this first and then we'll go back and figure out what these lines of code actually do. Here we're looking at the default interface for MATLAB and it's important to recognize that MATLAB is free for all Queen's students. You simply have to go through the software center that ITS runs and you can follow the process there for downloading and installing MATLAB with your student information. So let's just run the script for the first time. Notice it's setting up t's and then plotting and when we run this we are going to hopefully see a plot fairly soon. There we are. So we see this circle-ish thing that comes from the cos t sine t and I'm just going to add one thing as with every new programming language especially when it comes to graphics there's some less obvious commands that you just learn through experience or through Google searches and here we're just going to use the axis equal command which will make this look like a circle again making the x and y axis dimensions identical so how did that work well if you look at the code here I think we can figure out most of what's happening we see T values and we have a zero starting starting point and we go to 2 pi, which is one full revolution. So if we take a look at that, we can imagine that this is our starting t, this is our ending t, and the thing in the middle, not too surprisingly, would be the step between t values. So what we're going to do is generate a sequence of time values from 0 to 2 pi, stepping by 0.01. And we can see that that's the case by just doing a quick edit, changing the the step to say 1, so we'd be counting 0, 1 radian, 2 radians, and there's only 6 radians total. So this is going to be a very crude graph. And when we take a look at that, we see, yes, in fact, almost a hexagon there as we get up to 6 radians at the end here. So if we go back to our nice.01 and run it, we get our nice circle back. What MATLAB is doing with the second line, of course, is doing what we were imagining earlier, the plot command here. is doing what we imagined with our vector-valued function or parametric curves. We have two separate functions, one for x and one for y. The x coordinates are being computed as cosine of each of these t values, and then we get matching x values back. And when we have the y values, they're being computed one by one as sine of each of these t values. And so this is equivalent to our mathematical construction. x equals cosine of t, y equals sine. sine of t, or the simple vector-valued form cos t sine t. It's important to note, though, that this just gives us a part of, a graph's part of the trajectory. The part I've highlighted here is that we only went from time value 0 to 2 pi. Because we're working with circles, we get off lucky that we would actually repeat the same trajectory over and over again if we use different time values, but that won't always be the case. graph are always going to be limited to some subpart of the entire trajectory. That's just the way it goes based on some finite T range that we're going to use or T domain that we're going to use. The other thing that's missing though is it does not show the actual relationship with time. If we take a look at the trajectory here, all we see is the circle. We don't know whether we started here or here or here. We don't know how quickly we're going. And so that begs for a more in-depth study. And that's what we're going to do by animating this. And that gives us a much more visceral connection between the time passing and the graph that we're going to see. In this code we see the same starting point. We have the time being indexing from zero to two pi with the step size and we're going to use a for loop and we're going to use an indexing strategy here which is the t bracket i that framing in most other languages you might have seen is the same as T square bracket I for indexing. So the idea is we're going to look at the first T value, then the second T value, the third T value, and as we do that we're going to loop through those and plot each point as we go. Taking this code now and running it, what we're going to see is something not very exciting at first. In fact, it may not even be visible to you, but there is a tiny green dot there in the center. There we can highlight it, but that's not really... easy to see. So first off, let us take advantage of one of the options in MATLAB's plot command, and that is to change the marker size, the size of the marker, to something a little more substantial. And usually this is a bit of an experimentation. And there we go. So it shows up in the middle, but it's bigger and more obvious now. So let's stick with that. The second thing is if we run this command, we don't automatically see the picture. So if we take a step back, there is the window here, but it was in behind the main window. So what we're gonna do is add one of my favorite commands for graphing, close all. What this will do is close all the graphics windows, set up our T's and then do the animation. And the way this works, the first plot command after the closing will make the animation. make a new window and that will automatically pop up in front of us. So there is a window right now that's hiding. When we press run though we're going to be closing that and then popping it up in front of us again. There, so at least we can see it. Downside of course is that there was no actual animation happening there, we just saw the final point. So again graphing in any new language is always a challenge, especially with animations where there's a lot more updating of the graph. So I'm going to add a command called draw now here and we'll see what impact that has. Aha! Now we actually get an animation. You can see this dot moving. If you take a careful look at the axes here, it's sliding around a bit. It's going slowly. I might want to speed that up. But it's doing basically what we wanted. But what we do see is that the axes keep changing, and ideally we just like to see that circular pattern happening every single time. So we're going to stop this and... Oh wait, that didn't work. Famous command, or most useful command in MATLAB, Ctrl-C. If you hit Ctrl-C, it'll terminate the program. And usually you have to be either in the graphics window or down here for that to work the way you hope it would. Alright, so let's tweak this a little bit. What we're going to do is add a plot command that actually plots the entire thing. thing. So notice the difference between this line and the line below it. we have the cos, so that's going to plot a single point because i is just going to be 1, 2, 3, 4, and so on, versus plotting cos, where t is all of the values. And I'm just going to make two other changes that again are the kind of thing you learn after watching a few MATLAB tutorials. We're going to CLF, which clears the window, and then we're going to use the command hold on, which will allow both of these plots to be shown on the window at the same time. And now... we get, I think, a reasonable approximation of our animation. We can see the whole trajectory, we can see the green dot moving, and that gives us the visual sense for how things are working. I think my only two complaints now, honestly, are that it's too slow and painful to be actually interesting to watch. And the second thing is the axis dimensions are not even or not equal, so we'll fix that. Control-C again to get us out of there. And before the draw now, we're going to set the axis up to be equal. And the other thing we can do about the speed is make the steps a little bit larger each time now. So we can... Actually, let's do it the other way. There's two ways we can treat this. One is to use fewer time points. But the problem is those fewer time points are also going to be used in the drawing of the circle itself, the entire circle. And we don't want to make that any less detailed. So what we can do instead is, using the same colon notation, is start counting for the point. which are only depending on the eyes, we're going to count the first, tenth, twentieth, thirtieth, and so on point. And that will end up making the animation look faster, basically by skipping every ninth point. And now we start to have something that looks reasonable. Perfect. And if we want to make this a little more exciting, we could have gone out to something like 6pi and run it again. Then we should see multiple passes over the circle. as we iterate through this loop. There we go, we're on the second loop. So the point of this exercise is to remind you that what we're going to be graphing now is not quite the same as what you would have been experiencing when you drew regular xy graphs. So there is a trajectory that's an x and y but I'm Under the hood, or you may not see it, is the fact that time is evolving and causing a point to move over these locations, over these x-y locations as time passes, but each point on this graph has a time value associated with it. possibly more time values in this case here, but at least one time value associated with it. We have to think about things like direction of travel, maximum reach in x and y based on time. Those relationships back to time are going to be crucially important when we look at these as trajectories of particles.