 In this next section, what I want to do is go through an introduction to programming in MATLAB so that you can get a flavor if you haven't seen it before of how it works and what you can do and just kind of an overview of what it is. A lot of the programs that you may have to work on this summer are all written in MATLAB syntax, so this is a useful thing to know. Okay, now you can try this out and some of you did earlier. If you're in a workspace on Nanohub, you can fire up MATLAB as long as you said use and then a particular version of MATLAB, use MATLAB-7.11 or whatever. As long as you got a version on your use path, then you should be able to type MATLAB and it'll fire up the MATLAB GUI. We had a few problems with that this morning, but one other problem that you can run into is that if you try to do that from off campus, then Nanohub actually blocks you. MATLAB, the way MATLAB we're using inside Nanohub is really only for Purdue people. It's licensed to Purdue. So we actually have special controls in place to block people from using MATLAB when they're off campus. And that's actually what was happening this morning. It was actually blocking you guys, even though you're on campus. There was some glitch that was causing you to be blocked. So if you're having trouble firing up MATLAB, it's probably because of that blocking that we do. But if you fire up MATLAB, you can type some simple things like this. Now there's an open source version I want to show you, which will be good too. Instead of MATLAB, or if you're having trouble with MATLAB, try firing up the GNU program called Octave. And again, you probably have to say use so that it's on your path. It might already be on your path. But try at a command prompt, type Octave. And what you'll get is sort of the poor man's MATLAB. It's the free version, right? There's no fancy gooey and all that stuff. But you get a nice command line where you can type MATLAB syntax. So if you want to, as we're going along, you can type some stuff at the command prompt. You can type the commands that I'm going to show you. Oh yeah, on your use path, it's a good idea to type use Octave-3.2.4. That's the newest version of Octave. If you happen to be, Nanohub has a bunch of different versions. And if you're using an older version, you could easily get confused. So it's a smart idea to type use Octave-3.2.4. And then if you type Octave, then you're good to go. You got the latest version. Octave does almost all the stuff that MATLAB does. It's like a clone, letter for letter, syntax for syntax. It does plots, not quite as fancy as the MATLAB gooey. It's missing the fancy interactive step-by-step debugger. And it's missing the Simulink toolbox. So it's not quite as fancy as MATLAB, but it actually does all the work. And if you have a script written in MATLAB, it'll run pretty much in Octave. So it's a good thing to use. There's also a tool on Nanohub called Octave-U. It's actually just a gooey on top of Octave. And so again, if you're looking for something to try out the examples while we're going along, you can fire up Octave-U. So if you look on Nanohub for the tool called Octave-U and then click Launch Tool, it'll bring up a little gooey that looks like this. And on the left-hand side here, you can type in a little Octave script. And then you click Run Script, and it'll run the script and plot the results. It's not too comprehensive. It only does like XY plots and stuff like that. There are lots of things MATLAB does that Octave-U doesn't do very well. But it's a good quick and dirty thing where you can type in some MATLAB code and click the button and you'll get a plot of the result. And it helps you debug your code. So that's another way to run it, slightly better than the command line, because at least it does the plotting for you kind of interactively. So you could try the Octave-U out too, if you want to try these examples. All right, so one way or another, I'm going to walk you through some examples. And you can try typing them out as we go, see what happens. So there's a way to create, MATLAB works in what we call vectors, arrays of numbers, and MATLAB's very good at handling vectors. And this is the way you define or one way to define a vector. I can create a variable called x-axis, and I can set it to 1 colon 0.1 colon 5. And that's the way of telling MATLAB, start at 1, end at 5, and take steps of 0.1 in between. So that'll create a whole bunch of numbers, 1.0, 1.1, 1.2, all the way up to 5.0, and it'll store them in an array called a vector. So a giant bunch of numbers that you can operate on. Another way of creating a vector is using the limb space function. Slightly different because basically you say start at 1, end at 5, and give me 41 points. So instead of specifying the increment, you can say how many points you want in the vector. Notice when you type in these things, if you type x-axis equal 1 colon 0.1 colon 5, if you put the semicolon on the end, you get a different behavior than if you leave it off, right? Did you guys notice that? Anybody, what's the difference? Boy, I'm really deaf. Right, thank you. The semicolon basically suppresses the results out of MATLAB. So for debugging, if you leave the semicolon off, you can see all the intermediate results of what's happening inside your simulation. But if you put the semicolon in, which you normally do for programs, it kind of shuts MATLAB up, so it stops reporting all that stuff. It's useful though when you're just typing at the command prompt to leave the semicolon off and you can use it for debugging. For example, I'm wondering how big x-axis is. You can type in length x-axis and it'll show you the result. It'll show you 41 points or however many points are in it. You can just type x-axis all by itself and that's a way of printing out x-axis. See, if you put x-axis semicolon, it wouldn't do anything. But if you say x-axis by itself, it prints out the current result, right? All right, now suppose that I don't want uniform spacing like that. 0.1, 0.2, 0.3, all of that. I just have a few values for the vector. The way you do that in MATLAB is you can use a square bracket and then a comma separated list of values. So I can set this x-axis to square bracket 1.0 comma 1.1 comma 3.0, all the values that I want in the vector. So they don't have to be uniformly spaced, even though it's common to do that. And then you can write simple functions. Once you've got a vector, whether it's generated like we did a minute ago or like this, I can start to operate on it. I can say 2 times x-axis minus 2 and that basically gives, that's the equation for a straight line, right? So that'll take all the values in x-axis, multiply them by 2 and subtract 2. And I'll get back a vector of y values that matches the vector of x values. So that's basically a function, like y equals f of x, right? So you can try it and convince yourself that's what it does. You can also say plot x-axis comma y and it'll generate a plot of the value. That's kind of useful as you're going along. You can look at the values just by typing in y and you'll see all the values in y or you can say plot x-axis comma y. You can do that interactively when you're at the interactive MATLAB or the octave prompt. Now suppose I wanted to try y equals 3 times x-axis times x-axis plus 1. I'm trying to say y equals 3x squared plus 1, right? If you try that, it actually fails. And the reason it fails is because when MATLAB is trying to multiply these two vectors, you got to start thinking in matrix notation. If I take two vectors that are actually the same size and the same orientation and try to multiply them, you can't do that in matrix notation, right? I need to take the wide vector and a long vector and multiply them. I need to do something more like the dot product if you're familiar with that. And there's a notation for that in MATLAB syntax, you use dot star. Dot star says multiply this vector by this vector, but don't create a matrix. Instead, just take the first value here and the first value there and multiply them, the second one, the second one, multiply them. So it goes value by value through the vector and multiplies them, instead of trying to create a matrix out of it, right? So there's a subtle difference here I wanted to make you aware of. When you're dealing with vectors, you also can have matrices. You got to start thinking in terms of matrices and vectors. And there's a reason that there's a subtle difference between star and dot star, depending on which effect you want. Let's talk about matrices more because that's where MATLAB really starts to get interesting when you start dealing with matrix values. So suppose I define a matrix like that, or I want a matrix like that. The MATLAB syntax for that is like this. It's kind of like a vector, but you can use semicolons to separate the rows. So I can say A equals square bracket, and then give it the first row of values, semicolon, the next row of values, semicolon, the next row of values. So those two things are equivalent. And once I've defined that matrix, I can do operations on it. The transpose of a matrix, usually in mathematics, you use like a prime symbol. So if you do that in MATLAB, or octave, if you type A tick, it'll do the transpose of the matrix for you. And it's a single command, and it happens lightning fast, right? So normally in C language, you have to write a little function to do that. But in MATLAB, it's all built in, and it's super fast. All right, you could try this too. If you took the matrix A and multiplied by A transpose, then it would basically do matrix multiplication on those two matrices. And again, when you're dealing with matrix multiplication, the matrices have to have the right size. They have to fit, right? The number of columns in the first one has to be the same as the number of rows in the second one, so that you can actually multiply them. And you can do the opposite. You can take A transpose times A, and you actually get a different result. You can do things like A transpose times A plus one. But then, what does that mean? The way I wrote it like this, A transpose times A plus one, basically it adds one onto every single value in the matrix. But you might say, well, I really wanted more like the identity matrix. I wanted to add that in. And you start getting into all of that stuff. Am I bringing up bad memories from your early, your first Purdue calculus course or something? Is that, do you guys remember all of this? Matrices, the identity matrix, and all that? MATLAB has these built in functions, zeroes, ones, and i. So the zeroes matrix, when I say zeroes three, it gives me a three by three matrix full of zeroes. If I say ones four, it gives me a four by four matrix full of ones. And if I say i two, then it gives me the identity matrix. The identity matrix has ones along the diagonal and zeroes everywhere else, right? And that's useful in math when you're building other functions. So it's good to know about these functions. If you don't believe me, pull up the MATLAB prompt and type these things in and you'll see the results that you get back. All right, so it could be that for my program to get the right effect, what I really wanted to do is take A times A transpose and then add in the identity matrix, which would only add ones on the diagonal, instead of ones everywhere. All right, so now that we know a little bit about matrices, we could do something a little more interesting. So there are lots of other functions in MATLAB. For example, there's a function called IMREAD, and IMREAD reads an image. So I can give it the path for a JPEG file, any JPEG you want. And MATLAB will read that up and then it will store it as a matrix. It'll store it as a matrix that you can manipulate and pick apart and plot and do things with. So if we do that, we say IMREAD that function and we store the result as the IM. If I say figure and IMShow, it'll show me that image. It'll pop up on the screen. In this particular case, what it is is a picture of the MATLAB development team, a simulink development team, I guess. So if you're curious about the folks who wrote simulink toolbox, there they are. They're in the distribution. I just grabbed a standard image that comes with MATLAB. By the way, there's an equivalent for octave, but in earlier versions of octave, earlier than 3.0, you had to get a separate function to do it, but now it's built in. So again, octave's getting better and better, closer and closer to MATLAB. All right, now let's do something interesting with the image. We'll take that matrix that we defined a second ago. I've got my matrix A, which has minus 1s in the first row, 0s and then 1s, and there's a function in MATLAB called IMFilter where you can take an image, IM, and you can take a matrix like A and you can convolve the two of them. Again, I'm bringing back all those bad memories from your last math class, right? Convolution basically takes that matrix at each pixel value. It's going to multiply the surrounding pixel values and then give you a result and store it back on that pixel. Then it'll go to the next pixel and it'll take that whole matrix and multiply it by neighboring pixels and add it all up and then store it on the center again. And it'll apply that little three by three matrix that we've got. It'll apply it to every single pixel in the image and give it a certain effect. That's convolution. And if you actually do this and then say IMShow, you get something interesting. I don't know if anybody's following along, but I'll just show you the results. So here's the convolution procedure. Basically, that matrix gets applied to the three by three pixels and then it stores the result on the center pixel. And then it does that for every single block in the image. And what you get is something really weird. You get a really weird image where all the top lines are all bright. And side to side it becomes all dark. In other words, what we did was we get something that looks like edge detection in Photoshop. We already used Gaussian blur, how many people use Photoshop? Gaussian blur, edge detection, all the filters that you get in Photoshop. This is how they're implemented. This is what Photoshop does under the hood. It takes a convolution matrix and it applies it to your image. And you can do the same thing in MATLAB, only you get to create the effect. It turns out if you use different matrices, you get different effects. So in this particular case, my matrix was minus ones, zeros, and ones. And that gave me this vertical edge detection. If I flipped it and had minus ones, zeros in the middle, and ones on the right side, I would get kind of a horizontal edge detection. And if I started messing with all the values and not just using minus ones and ones, but using weird values, you start to get other things. You get Gaussian blur. You get all the different effects that you can find in Photoshop. So now you guys have another summer job. You can go off and create new effects for Photoshop. Just by playing with MATLAB and coming up with different matrices. You can use larger matrices which will apply the effect out further in many neighboring pixels or smaller matrices. You can play with all the different values and you'll get all kinds of different effects, but that's how it works. And there's a lot of matrix math in there, a lot of pixels in that image. And at each pixel, there's a lot of matrices that are being applied. So MATLAB's very good at that, very good at matrices. All right, you can mess around and try different things too. In fact, maybe I'll leave this as an exercise to all of you guys later. If you mess around with, and you take A times A transpose, you get yet a different matrix. And when you apply that as a filter, you'll get a different effect. Which, yeah, I don't know. I guess maybe if you try enough matrices, you'll discover something amazing. All right, yeah, I guess the A in the A transpose is kind of a charcoal, which I do have a screenshot for. It kind of gives you more of a, instead of vertical or horizontal, it kind of applies it across the image in a more diagonal fashion, I think. All right, more with matrices. Sometimes you don't want to use the whole matrix. Sometimes you want to manipulate individual parts of the matrix. And there's a syntax for that. So again, supposing I have this matrix A and I want to get at the very first element, the first row and the first column, that you can type A parenthesis 1 comma 1, and that gives you that first element. Should be minus 1. You can convince yourself if I say A2 comma 1 should go to the second row and the first column, should get 0 and so forth. So you can pick apart the matrix by asking for different values within the matrix and addressing it. So in general, that first index is always the row and the second one's the column. And in MATLAB, they're always indexed from 1. C language is more like starting from 0. This is from 1. All right, now MATLAB also has some other things where if I want the first row and three columns, I can write, I can use that vector syntax that we had a minute ago that we were looking at. I can say 1 colon 3 and it'll give me three columns out of that. In this case, it's a 3 by 3, so I'm basically getting the whole thing. There's also a way, if you just want the whole row or the whole column, you can just say colon. So in this case, I'm asking for row 1 and everything across all the columns. The colon basically means from whatever to whatever, everything. So if you don't give specific indices, it pulls the whole row out in this case. You can also change values in the matrix. You could say a2, 2 equals 3 and that'll just change that one element within the matrix in the second row, in the second column, middle element, I guess. And then if you print out a, it'll show it to you and there'll be a different value in there. So again, as you're messing around with all these matrices trying to come up with the ultimate Photoshop effect, this is how you put values in, one at a time. We can use that same kind of matrix stuff to pick the image apart, pixel by pixel. So if you've got this image, you can address the image as if it were a matrix. You can say row 1, column 5, and it will give you back the red, green, blue values in the image at that point. Actually gives you back a vector of red, green, and blue. You can say I want row 3, I want all the columns. I don't know how big the image is, but row 3 whatever's there on the line is what I want, and or you can subset the image. You can say I want the first 100 rows and the first 100 columns and that'll cut out a 100 by 100 image out of the corner. So pretty powerful syntax to grab all this stuff. All right, now let's look at the plotting stuff. If I did that, if I said in my image I want row 3, I want all the columns and I want to pick out the first value, red. Remember it's going to give me, if you tried this out it would give you back red, green, and blue. So the last one value there is red, I want red. So what I'm going to get is the third row, all the columns, and the red component. And I'll get that back as a vector called rv, and then I can plot it. I want to plot from 1 to the length of rv, all the rv values, and then I can give it r and it'll make it red. And if you try that, you'll actually get a plot like this. It'll show you the intensity of red values across that row of the image. And I can also add some other stuff. I can put a title on the plot, I can put an x label and a y label. So if you want to generate plots in MATLAB, I'm basically showing you how to do it, right? This is the plotting function. That's how you set labels and titles and that's how you plot stuff. In MATLAB you can also say hold, which means I like that figure just like it is and I want to add something onto it. And then I can get another vector, like the green vector is the same thing, except instead of getting the first component red, I get the second component green. And then I can plot that. And now it'll add. Now I've got the red plot and the green plot on the same plot. And that's because I said hold, so I got both. I can also add a legend onto the plot too, it's kind of when you start doing that I can mark, get a little legend up in the corner that shows a sample of red and a sample of green and labels it. All right, here's a little, by the way, CLF is the clear function that clears out the plot. So we had a hold on but we'll clear that. And now I'm gonna plot the next one and I'm using a little bit fancier styles here. I wanted dotted lines and circles and all of that kind of stuff. So with that funny string r colon o, that basically, the r means red, the colon means a dotted line and the o means put circles at all the points. So MATLAB has this real weird cryptic notation for the plotting. And here's your cheat sheet if you ever wanted to generate a MATLAB plot. You can use these single letters for all the colors. You can use those symbols to get dashed lines and solid lines and dot dash and all that. And then if you want to mark the points, you can put pluses and stars and x's and all that weird stuff. So you don't have to use that stuff but if you want to dress up your plot, that's how you dress up the style. So you got the basic syntax. We're almost done but I want to show you what it looks like when you start writing MATLAB programs. When you write a program, you start to getting into functions and language syntax and all of that. So here's an example of a function that I call edge matrix. Edge matrix is that matrix that we had a minute ago, the minus ones, the zeros, the ones. I want to be able to generate, I have a function here. I can pass in any size n. I could create a 10 by 10 edge matrix and it would generate this giant edge detection matrix for me. I can orient it. The orient can either be horizontal or vertical. So I can now call this edge matrix function, pass in any size, any orientation, and edge matrix will generate a matrix for me that has all those minus ones and all that stuff in it. And I could use that then. As I'm writing my image filters, we're creating Photoshop. I can call my function in filter with my image but instead of hard coding a matrix A here, I can call edge matrix. Here I'm getting a five by five matrix and I'm oriented horizontally. Or I could pass in seven and vert or whatever. So I can pass in whatever values I want. This will generate an edge matrix just like our matrix A and then apply it as a convolution. So this is what it looks like to define a function in MATLAB and to call it in MATLAB. It looks a lot like other programming languages you've probably seen. All right, now like other programming languages MATLAB can let you do looping. So there's a for loop and here I have two nested for loops. You can say for i equals and then we use that vector syntax. One colon n basically generates a vector from one to n and then it loops through all those values. So this is how you write a loop in MATLAB and same thing for j. So I've got the outer loop i which is going through all the rows n and the inner loop j which is going through all the columns up to one to n. Because n was the size of my matrix. So that's how you write for loops. And this is what you can think of in your mind. I'm trying to build this five by five edge detection matrix and I'm looping over all the values i and I'm looping over all the values j. And I'm figuring out all the values. Now inside the code I have a little if statement to see what the orientation is. If it's horizontal then I use one formula for each value and if it's vertical then I use a slightly different, that gives me different values. You remember the edge detection matrix? If it's horizontal, I think it's like this. And if it's vertical it's sort of the transpose of it. All the minus values are here and then zero and then the plus values on the other side. Kind of a transpose thing. So my if statement, if you want to write an if statement in MATLAB, you say if and give it some kind of a condition that you can check. And then you have some code else and then some other code and then end statement. Again, it probably looks a lot like other programming languages, like C or Fortran or something like that. That's how you write an if statement. The details of this function aren't so important. I'm just trying to show you language syntax so you get used to it if you have to write some code. The way this formula is set up, if I pass in the value horrors for horizontal, I'll end up with a matrix that looks like that. And if I pass in anything else, Burt probably is what I should pass in, then I'll get that. So the way this is set up, it's producing the ones and the zeros in the right orientation based on that if statement. So that kind of shows you a lot of the syntax, basically how to declare a function. You can see some variable assignments, some for loops, some if statements. That's basically how MATLAB works, just kind of an overview. Now the trouble with this example is that if you actually run this example, you write this example and you start running it, it's terrible. It runs really slow and it's a horrible implementation. I hope you guys, any of you already know MATLAB are already laughing at me, thinking, why would you write it that way? You wouldn't, that's crazy. So don't do this, it's a good example to show you all the stuff in MATLAB. But when you program in MATLAB, you actually program a different way. You think about the programming differently. Turns out if I were writing a C language program or a Fortran program, what I wrote is good. But when you're programming in MATLAB, you have to take advantage of the vectors and the matrices and you really got to do that. If you program the same example in Python or in Tickle, again, you do it differently. It turns out each language sort of has a way of doing, way of approaching the problem, and in MATLAB, that wasn't the way to approach the problem. If you were really going to write this function in MATLAB, you write something that's a whole lot more cryptic, but a whole lot faster. That's always the trade off, right? So the way this function works, my short version basically generates a matrix of ones and creates a vector from minus half to half, the size of my matrix. So if I'm trying to generate a five by five, it goes from minus two to two. Those are the values. I think I have a little aid here to help you understand it. So what I'm doing is I'm generating this vector minus half to half, and I'm multiplying by a matrix or a vector of ones, I guess, just I'm pulling one row out of the ones matrix. And if I multiply those two vectors, I end up with what I want. And except for the case where if I want to orient it horizontally, then I need the transpose, right? So by using matrix multiplication, I can avoid all those four loops. The four loops are what kill you in MATLAB. If you're going to scan for i equals 1 to a million, and then inside that for j equals 1 to a million, it's going to take you a million days to get your job done. So don't do that. What you do instead is you come up with a way of figuring out vectors and you take two vectors and multiply them together, and you'll get the result at lightning speed. And then you say, well, that's close, but I want the transpose. Well, you can get that at lightning speed too. You just say m equals m transpose. Boom, done. So the lesson to take away from all of this is first of all, there's a lot of syntax in MATLAB that's a lot like what you've seen in other languages. And second of all, don't use MATLAB like you use other languages. Learn how to program with these vectors in these matrices and you'll end up with programs that are much better than brute force. It's astonishing, I'll tell you. I've seen grad students who write code and they run this program. And it takes like, I don't know, seven hours to run the program. And they start trying to figure out how they can get the program out on TerraGrid and on Condor. And what's the biggest supercomputer that I could use to run my MATLAB program? And all you have to do is rewrite the code like this and your program will run in 35 seconds. Not every program, but there are a lot of bad code out there that takes too long to run simply because of the way it's written. So pay attention to the way your code's written if it's taken too long to run and you can make it run a whole lot faster, I guess. All right, one last thing I want to show you. If you're writing a MATLAB program, you might have some simple inputs, some simple outputs, some simple file stuff. And again, just to show you kind of the quick overview of MATLAB, just so you've seen it once, you have some examples to copy. This is a very simple program that kind of prompts for different things. So there's a disk function which will display a prompt. So disk, who are you, will print out who are you. And then the next step, input, lets you enter something. It says enter your name, and then the S at the end of the line says, I'm gonna get a string input. So enter your name, give me a string. And that will come back as the variable called name. The next line says enter your age, but you notice there's no S there. So MATLAB by default will ask you for numbers. So if you typed in 37, then age will be 37. Actually, the way this is written, I think you could type in 37.2. I don't know whether that's a bug or a feature, but anyway. You can type in any number for your age and it'll take that. So that's how you basically prompt using disk and using input. When you go to run it, it'll actually look like this. So if you run this program, it'll say who are you, enter your name, you can type that in, enter your age, you can type that in, and it'll print out hello Michael and so on and so forth. So if you look at the S print F function, and if you're familiar with C language at all, C language has this print F function, formatted print. It basically builds a string and prints it out. The S print F version builds a string but doesn't print it out. It just gives you the string part. So C language has S print F and a lot of other languages do too. Just MATLAB is one of them. So if you want to build a string, a nice way to build it is by using S print F and you can say hello percent S. Percent S becomes the place where it puts the string. So the name that I give it here, that name argument gets substituted in place of percent S and it prints out hello Michael, whatever the name was here. Hello Michael, like that. And disk is the way you actually print. I think there's a print F function too, but anyway. Disk is a way of printing out a string. The bottom part for enter a file name, you can again use the input function to prompt the person for a file name. And then like C language, MATLAB has F open, F print F and F closed. Those are the file functions. F open opens the file and the W means for right. F print F prints out, it does like the S print F except for a file. It prints out to the file ID that you opened FID and it builds a string, percent S is percent D years old. So the name gets substituted in percent S and the age gets substituted in percent D. Percent D is a decimal number, integer. So it builds that string and then writes it out to the file and then F closed, closes the file. So just so you've seen it once, you can tell your advisor, yes, I've seen file IO in MATLAB. Hopefully you never have to do it again, but that's an example. All right, there's a lot more stuff about MATLAB that you can learn, including I mean there's great tutorials at MathWorks that you can look up. There's also some talks on Nanohub, including this really great talk by this guy from Intel, MATLAB do's and don'ts. So if you want to learn more about MATLAB, there's a lot more that you can go into. That was just kind of the quick fly by to get you guys to the point where you've seen it once. So I want to give you a chance now to kind of mess with MATLAB. And you can do that in a workspace by bringing up MATLAB. You can do that in a workspace by bringing up octave or you can use octave view, whichever way you want. But what we're going to do is type in some code and try out the plotting. It turns out you guys ever use Spirograph when you were a kid? Am I dating myself? Yeah, okay. Nobody else, it's an age thing, all right. So back in the 18th century, when people were looking for fun and there was no YouTube and no Facebook, there was a system of gears that you could pin down on a piece of paper. And you put the gears in with a pen and you move the pen and the gears around in a circle and it would generate something that looks like that. And depending on which gear you used and which hole you stuck your pen in, you would get different patterns. So basically you stick your pen in and you move it around and it would create this curly loopy kind of pattern. And then you take another gear and different color pen and you put in and move it around and it would create another pattern on top. It was great fun. Again, back in the days before the internet, fantastic. This is what people did. Also we sat around campfires and showed our drawings to one another. That's what we did. All right, so there's a mathematical theory behind this. You can use complex exponentials to generate the same kind of effect as Spirograph. So E to the complex number I times 2 pi times some number N1 times T. And if you add that to E to the I 2 pi times a different N2 times T, and use the same thing again with N3, if you pick three weird values for N1 and N2 and N3, you basically get a Spirograph. It's like the gears, the complex numbers of the gears basically oscillating around at different random weird frequencies. And they give you these strange looking plots. So if you take the real part of the vector that you produced and the imaginary part and you plot them, you get a Spirograph. If you're interested in the theory, and what life was back like in the 1800s, then go ahead and take a look at the, you can follow the article or the link to the Linux Gazette. It'll explain to you the whole thing about Spirographs and all that. But if you just want to try it out, in your workspace, fire up Matlab or Octave and type in this code and get some plots. Or you can try it in Octaview too. Go ahead and give that a shot, try it a little bit. All right, let me show you the solution that I came up with for this lab assignment. I'll just do it on the fly. So first of all, I'll go to NanoHub and we can find that tool Octaview. So here's this Octaview tool and you can see some screenshots and kind of play through the video. It kind of shows you how it works and all that. But anyway, this is an easy way to launch stuff. You have to log in, then it fires up like any other tool. Okay, so I can type whatever I want. I'll start with t equals linspace 0 to 1000. And if I just do that much and click Run Script, it'll show me, okay, there's my vector t, right? And on the console output, I can see all of the values. And that's because I didn't have a semicolon at the end here, so it's showing me all the intermediate results. So I'll stick a semicolon there and keep going. I'll say z equals x i times 2 times pi times n1 times t plus x of i times 2 times pi times n2 times t plus the last one. i times 2 times pi times n3 times t. The i and the pi are built in in MATLAB, so it knows about those. And then I can plot the real of z versus the match of z. All right, now I can type Run Script, and oh, it gives me an error. It says n1's undefined, right? Octaview is kind of good for that too, because it helps you trap the errors in your script and figure things out. n1's undefined because yeah, I didn't define it. What values am I going to use for this plot? Let's try, let's set a few. Let's set n1 equals 23. What was that one I liked? Negative 19, 38, I forget. Try that. So those are three values, and then the rest of my code, and I can click Run Script, and it'll go off and run it. Hey, that's kind of a good one. So that's the plot that I asked for. There's also a debug tab here that shows me the debugging information. I can look at the variable t, I can look at the variable z. I see the other variables too, n1, n2, and n3, when they're just ordinary values. And I can try different, I can also see the console output here, and review all of that. And again, I missed a semicolon somewhere, because I'm getting a lot of output on the console. It's probably at the end of the z line. Yeah, I'll just put a semicolon there, I won't get nearly as much output. And I can try some different cases. I can play with, see what happens if it's minus 12, and run it again. And I get a different pattern and so forth. So Octaview is kind of a good quick and dirty way to write a little bit of MATLAB code. It's not very good for writing really complex programs with lots of functions, and it's not very good at debugging the real MATLAB programs better than that. Or at that kind of thing, or you can get into the actual Octaview, or I'm sorry, the actual Octav program at a workspace. So let me show you that real quick. Let's go in to here. I've got a little program here called Spyro.m. So here's my little MATLAB script. Comments in a MATLAB script, you use a percent sign for the comment. I defined my n1, my n2, my n3. I set my linspace, my z, my plot, all that stuff I showed you a minute ago. I stored that now in a script file called Spyro.m. So let's run that. The way you run that is first of all, I have to make sure I'm using Octaview. So yep, there's Octaview, it's got a star next to it. So I'm all set up. I can type Octav, and it fires up, and then I can say Spyro. And what it'll do is it'll look for the file called Spyro.m in the current path, or whatever my search path is for Octav, and then it loads that file in and runs it. And of course my file asked for a plot, so it went ahead and plotted it. So I could edit the file and run it again, and this is the way you'd build a program by probably if you were trying to do more of a script and debug it, you wanna save it in a file and you wanna try it out. The Octav command prompt is good for debugging too because I could say, if something's going wrong, I can be like, now what's going on? What's Z? Oh, that's Z. What's T? Oh, that's T, right? So you can just type commands interactively and mess around. I could say, well, what's the plot of Z versus T? And it'll plot that up for me. Woo, that's not a Spyrograph, but anyway, that's Z versus T. So you can do lots of things interactively on the command line and kinda debug and figure out your program that way.