1 00:00:00,720 --> 00:00:04,936 This is a short video about MATLAB trying to demonstrate its capabilities and how 2 00:00:04,936 --> 00:00:08,966 it can be used in this course, but also I think you'll find it useful beyond just 3 00:00:08,966 --> 00:00:12,872 the confines of this course, and actual practice as an engineer or in future 4 00:00:12,872 --> 00:00:18,176 courses. You can see I am running MATLAB because 5 00:00:18,176 --> 00:00:22,961 of what it says at the top of this title bar over here, on the right side. 6 00:00:22,961 --> 00:00:28,637 That's the command window for MATLAB. There is a public domain version of 7 00:00:28,637 --> 00:00:35,473 MATLAB, which is known as Octave. And, you can see the Octave can be 8 00:00:35,473 --> 00:00:44,790 obtained at the GNU website. You can download your own copy of Octave 9 00:00:44,790 --> 00:00:53,770 for free. It's, supports lots of different systems. 10 00:00:53,770 --> 00:00:57,522 I'm, I happen to be running on a Macintosh, and that's the one I would 11 00:00:57,522 --> 00:01:05,76 download if I were using it. The MATLAB command window is here over on 12 00:01:05,76 --> 00:01:09,108 the right side, the really interesting thing about MATLAB is I don't have to 13 00:01:09,108 --> 00:01:13,360 write a standalone program and then run it. 14 00:01:13,360 --> 00:01:17,455 I can do a lot just typing commands to MATLAB at the keyboard, and that's what 15 00:01:17,455 --> 00:01:22,194 I'm primarily showing today. Over on, here on the left side, is a 16 00:01:22,194 --> 00:01:27,945 figure window, a plot window, and that's where all my plots are going to show up. 17 00:01:27,945 --> 00:01:32,300 And I think you'll finally see why we're so enthusiastic about using MATLAB, 18 00:01:32,300 --> 00:01:39,74 especially for this course. So every variable In MATLAB, is a vector, 19 00:01:39,74 --> 00:01:45,596 or more generally a matrix. And this is extremely useful, as I want 20 00:01:45,596 --> 00:01:50,184 to show you. So let me define a, a vector named x, and 21 00:01:50,184 --> 00:01:55,358 I'm going to say that's equal to zero colon n. 22 00:01:55,358 --> 00:02:01,760 And so this says, it says, this command says, set the variable x equal to a row 23 00:02:01,760 --> 00:02:07,968 vector whose elements start at zero, increment by one, going from element to 24 00:02:07,968 --> 00:02:16,492 element in the vector and wind up with the value of ten. 25 00:02:17,830 --> 00:02:22,238 And if you just hit Return, you'll see that x has been defined to be that 26 00:02:22,238 --> 00:02:27,330 variable, and sure enough it's a vector of the 11 elements, consistent of the 27 00:02:27,330 --> 00:02:32,714 integer 0010. Well I can plot this against itself. 28 00:02:32,714 --> 00:02:36,298 It' snot a very interesting plot, but I can do it, and the plot command in that, 29 00:02:36,298 --> 00:02:41,750 in that tab is plot. This is what I would plot on the x-axis. 30 00:02:41,750 --> 00:02:44,564 You can see the little help window that pops up in Matlab. 31 00:02:44,564 --> 00:02:47,927 That won't pop up in Optic, but you really don't need it, eh, to do your 32 00:02:47,927 --> 00:02:50,964 work. And I'm going to plot that against 33 00:02:50,964 --> 00:02:54,314 itself. And you plot on the left side. 34 00:02:54,314 --> 00:02:59,194 It won't change much because that was what I was setting up for, and you can 35 00:02:59,194 --> 00:03:05,228 see that it's plotted a straight line as you would expect. 36 00:03:05,228 --> 00:03:17,570 Well, I can plot x to and instead I'll make the vertical scale 5 times x. 37 00:03:17,570 --> 00:03:21,830 So the usual syntax for doing multiplication, division, addition, and 38 00:03:21,830 --> 00:03:25,810 everything still apply to variables in MATLAB. 39 00:03:25,810 --> 00:03:34,132 So now I'm going to do something a bit fancier I'm going to plot x 5 times x 40 00:03:34,132 --> 00:03:41,303 plus 2. The reason that's fancy is what is going 41 00:03:41,303 --> 00:03:51,260 to happen when I add a, this is a vector, 5 times x is a vector, but 2 is a scalar. 42 00:03:51,260 --> 00:03:55,642 And what does MATLAB do? Well it does the thing you want it to do. 43 00:03:55,642 --> 00:04:01,146 It's going to add 2 to every element of 5 times x, and so when I finally, plot 44 00:04:01,146 --> 00:04:06,828 this. You see the plot changes, and there's an 45 00:04:06,828 --> 00:04:11,980 offset by two at the origin. So the vector plus a scalar is a vector 46 00:04:11,980 --> 00:04:16,770 with the scalar added to every element of the vector. 47 00:04:16,770 --> 00:04:26,545 Same for subtraction, etc. Now suppose I wanted to plot, Y = X^2. 48 00:04:26,545 --> 00:04:35,224 Well first of all, what is this mean? Well, you cannot multiply a vector times 49 00:04:35,224 --> 00:04:37,928 a vector. It doesn't work, and it's going to 50 00:04:37,928 --> 00:04:41,586 complain because it, basically it's assuming everything is in matrix 51 00:04:41,586 --> 00:04:45,160 multiplication. All the matrix multiplication rules 52 00:04:45,160 --> 00:04:51,276 apply. But MATLAB has a very special syntax for 53 00:04:51,276 --> 00:04:57,650 term by term multiplication. So, what this, see the dot in front of 54 00:04:57,650 --> 00:05:01,560 the star? What that means instead of thinking about 55 00:05:01,560 --> 00:05:06,552 x as a vector, and just doing vector times vector multiplication, this means 56 00:05:06,552 --> 00:05:13,488 to term by term multiplication. So, since x consists of the integers zero 57 00:05:13,488 --> 00:05:18,906 to ten, the y vector here should consist of the squares of every element, and 58 00:05:18,906 --> 00:05:27,350 that's exactly what they are. And so this dot star is what you need, if 59 00:05:27,350 --> 00:05:36,66 you want to apply the same, mathematical operation to every element of the vector. 60 00:05:36,66 --> 00:05:43,79 And now I can plot, y versus x, and I get a parabola, as you would expect. 61 00:05:45,150 --> 00:05:49,50 So let me label the axes. I always like to do that. 62 00:05:49,50 --> 00:05:56,154 You label the x axis with the command x label and you enclose strings in MATLAB 63 00:05:56,154 --> 00:06:05,395 with single quotes, not double quotes, it turns out, single quotes. 64 00:06:05,395 --> 00:06:12,401 And there will pop up a label over here in the plot window, and I'll do one for 65 00:06:12,401 --> 00:06:19,112 the y-axis. I'm not going to do anything fancy, just 66 00:06:19,112 --> 00:06:23,525 calling it y. And you can see it actually rotates it, 67 00:06:23,525 --> 00:06:30,783 counter-clockwise, so it looks pretty. Well, you've certainly, if you do 68 00:06:30,783 --> 00:06:40,538 something like what is sine of two time pi times x over 5. 69 00:06:40,538 --> 00:06:49,425 So pi is defined. It's already predefined for you and I 70 00:06:49,425 --> 00:06:54,680 think this makes perfect sense. But what does sign of a vector mean? 71 00:06:54,680 --> 00:06:59,864 All of the special functions in MATLAB are set up, so if you type this what this 72 00:06:59,864 --> 00:07:07,40 means is term by term application of this function to the factor x. 73 00:07:07,40 --> 00:07:13,88 And so what we get back is, you can see now the values are equal to sine of 2 pi 74 00:07:13,88 --> 00:07:19,660 times the integers here of the 10 divided by 5. 75 00:07:19,660 --> 00:07:22,924 It's not a very exciting result, but there it is. 76 00:07:22,924 --> 00:07:27,882 Well you can see that what MATLAB does is that after a calculation, like the sine, 77 00:07:27,882 --> 00:07:33,47 it gives you an answer. And it's going to tell you that, well you 78 00:07:33,47 --> 00:07:37,480 may not want to see every detail of every calculation. 79 00:07:37,480 --> 00:07:41,832 So what you can do is you type it in. If you end the last line with a 80 00:07:41,832 --> 00:07:51,40 semicolon, nothing gets typed actually, but the calculations are still performed. 81 00:07:51,40 --> 00:07:56,144 Notice it just performs the calculation, but it did not type them out for you. 82 00:07:56,144 --> 00:07:59,448 So, I'm going to, from now on, use the semicolon, because I don't want to see 83 00:07:59,448 --> 00:08:04,850 all the intermediate, calculations. Now, there's another very cool thing that 84 00:08:04,850 --> 00:08:08,430 saves typing. Remember, use MATLAB a lot at the command 85 00:08:08,430 --> 00:08:12,171 line, not necessarily to, to write a program for. 86 00:08:12,171 --> 00:08:16,389 I'm going to type s i n, and I you can't see it but I'm going to type an up arrow 87 00:08:16,389 --> 00:08:21,51 on my key board and it's going to bring up the previous command that began with s 88 00:08:21,51 --> 00:08:25,880 i b. I'm going to go over here and say y 89 00:08:25,880 --> 00:08:30,98 equals and hit return, and now the variable y if I just type it I can see 90 00:08:30,98 --> 00:08:35,312 it's values. You can see it has stored all the values 91 00:08:35,312 --> 00:08:40,886 of this previous calculation that I did before, and I can plot it. 92 00:08:40,886 --> 00:08:44,933 So I'm going to bring up the plot command, I type pl up-arrow and there it 93 00:08:44,933 --> 00:08:50,744 is, and it doesn't look much like a sine wave, but I can fix that. 94 00:08:50,744 --> 00:08:55,172 So, I'm going to redefine x, I may say X and I am going, just going to go up 95 00:08:55,172 --> 00:08:59,996 arrow. There is X label, hit up arrow again, and 96 00:08:59,996 --> 00:09:06,66 there is my definition for X. I am going to add an argument here. 97 00:09:06,66 --> 00:09:11,344 0.01 colon, so if you have an intermediate value in the colon notation 98 00:09:11,344 --> 00:09:17,623 here, now the increment, instead of being 1 between the elements of the vector, now 99 00:09:17,623 --> 00:09:23,800 it's going to be 0.01. And I don't really want to show you all 100 00:09:23,800 --> 00:09:28,399 of those, so I'm going to type a semicolon out there and there's x. 101 00:09:28,399 --> 00:09:33,126 I'm going to recalculate the sine function. 102 00:09:33,126 --> 00:09:37,506 [SOUND] So, I'm going to be over the same interval, but now much more finely 103 00:09:37,506 --> 00:09:42,324 represented in the plot command, gives me something that looks very much like a 104 00:09:42,324 --> 00:09:46,953 sine. So, now you can see that you can really 105 00:09:46,953 --> 00:09:51,24 do some very simple calculations or complicated calculations, at the 106 00:09:51,24 --> 00:09:56,228 keyboard, getting plots and seeing what they look like. 107 00:09:56,228 --> 00:10:01,234 Now I want to show you something else that's very, going to be very important. 108 00:10:01,234 --> 00:10:10,880 suppose I want to plot 1 over x. So let's define y equals to 1 slash x. 109 00:10:10,880 --> 00:10:16,592 Do you think this is going to work? And one is a scalar, don't forget that x 110 00:10:16,592 --> 00:10:21,304 is a vector, and a scalar divided by a vector doesn't work very well, and you 111 00:10:21,304 --> 00:10:27,930 get that same command again, guess what you type to fix this? 112 00:10:27,930 --> 00:10:32,252 If I just hit an up arrow now, it's just going to bring up the previous line. 113 00:10:32,252 --> 00:10:36,889 And I'm going to go over, type my dot, like I should have. 114 00:10:36,889 --> 00:10:42,159 And I'm going to put a semi-colon out here because I certainly don't want to 115 00:10:42,159 --> 00:10:48,421 see all these values of y here. Let's see, there's .01 times 10, so 116 00:10:48,421 --> 00:10:54,422 there's 1000 elements there. And I don't really want to see them all. 117 00:10:54,422 --> 00:10:59,900 So I'll do that, and now I'll bring up my plot command, and there it is. 118 00:10:59,900 --> 00:11:05,600 It looks like a hyperbola. Well, those of you who know what I've 119 00:11:05,600 --> 00:11:12,737 follow what I just did, know that y of one is 1 over 0, and guess what math lab 120 00:11:12,737 --> 00:11:22,462 says it is, it is indeed infinity. So the way to access any element of a 121 00:11:22,462 --> 00:11:27,64 vector is you give the name of the vector, and in parenthesis you put the 122 00:11:27,64 --> 00:11:33,418 element you want to extract. If I wanted to extract the first 10 123 00:11:33,418 --> 00:11:38,649 elements, guess what I use. I use the colon notation again, this time 124 00:11:38,649 --> 00:11:43,473 always, since it's trying to index y, it always starts, the increments should be 125 00:11:43,473 --> 00:11:47,910 1. So 1 colon 10 over 5 and in that lab, all 126 00:11:47,910 --> 00:11:54,200 indexes for arrays start at 1, not 0 like they do in C. 127 00:11:54,200 --> 00:12:00,666 So this gives me the first 10 elements and it should be one over X, going on a 128 00:12:00,666 --> 00:12:07,874 very, fine way, a detailed way, and see it has explicitly knows infinity, and 1 129 00:12:07,874 --> 00:12:17,378 over 0.01 is 100, etc. So the plot command is smart enough, and 130 00:12:17,378 --> 00:12:23,160 if it sees infinity it doesn't plot it at all. 131 00:12:23,160 --> 00:12:26,451 So that's why this plot, the vertical scale here, starts at 100. 132 00:12:26,451 --> 00:12:30,419 And by the way all plots are auto-scaled, it's going to find the scale that makes 133 00:12:30,419 --> 00:12:34,922 things fit. and you can change those if you look at 134 00:12:34,922 --> 00:12:39,358 the details of how the Plot command works. 135 00:12:39,358 --> 00:12:44,320 So, the getting help is easy for a given command like plot. 136 00:12:44,320 --> 00:12:46,560 How do I change the axes, things like that. 137 00:12:46,560 --> 00:12:50,613 You type like plot. And you will get something, that's, that 138 00:12:50,613 --> 00:12:54,934 you can see. It's got lots and lots of detail here. 139 00:12:54,934 --> 00:13:00,610 which you should go through when you're. First getting started with MATLAB. 140 00:13:00,610 --> 00:13:08,382 Now, suppose I want to try something I want to calculate something that looks 141 00:13:08,382 --> 00:13:16,0 like oh, let's say, x squared divided by sin of x. 142 00:13:16,0 --> 00:13:21,302 So y equals x dot up arrow two. I think you now know what the dot means, 143 00:13:21,302 --> 00:13:29,158 that means I'm going to take the vector x, and, turn by turn, compute the square. 144 00:13:29,158 --> 00:13:34,404 I'm going to divide that by sine of x, which is about the same thing, but I'm 145 00:13:34,404 --> 00:13:41,377 going to do it anyway. And notice I have to do the dot again, so 146 00:13:41,377 --> 00:13:50,149 it's dot slash, and I don't want to have it all typed out at me. 147 00:13:50,149 --> 00:13:56,783 And so let's see what y is in first ten elements, and you can see that x equals 148 00:13:56,783 --> 00:14:05,822 0, we get 0 divided by 0, and MATLAB has said that's not a number. 149 00:14:05,822 --> 00:14:09,530 NAN means not a number. And so Matlab can take care of these 150 00:14:09,530 --> 00:14:12,730 calculations, it doesn't care if the things goes to infinity, if you divide 0 151 00:14:12,730 --> 00:14:16,70 by 0, we're still going to return an answer. 152 00:14:16,70 --> 00:14:20,630 And that's one of the very interesting things about MATLAB. 153 00:14:20,630 --> 00:14:24,473 So plot, I can plot these two things, and it looks kind of screwy at the places 154 00:14:24,473 --> 00:14:29,890 where the sine function is going to zero. You can see it's going to start to blow 155 00:14:29,890 --> 00:14:34,660 up, but the plot is still there. Okay. 156 00:14:34,660 --> 00:14:39,470 So this is how you divide a vector by another vector. 157 00:14:39,470 --> 00:14:45,718 You use the dot slash notation. Now the other interesting thing about 158 00:14:45,718 --> 00:14:51,420 MATLAB is that it loves complex numbers. It doesn't care. 159 00:14:51,420 --> 00:14:57,615 So if you type square root of minus 2, it's going to tell you that that is 0 160 00:14:57,615 --> 00:15:04,535 plus 1.4142 times i. They use i for the square root of minus 161 00:15:04,535 --> 00:15:07,862 1. I can't change it to j. 162 00:15:07,862 --> 00:15:12,150 However, what's also very interesting about MATLAB is that the variable i is 163 00:15:12,150 --> 00:15:17,902 predefined, in this case to be 28 because I've written a program before. 164 00:15:17,902 --> 00:15:22,816 If I say i equals the square root of minus 1, that turns out to be the 165 00:15:22,816 --> 00:15:26,482 default. When you first run MATLAB, that's 166 00:15:26,482 --> 00:15:30,827 going to be the default that I use. There's a special symbol that they use, 167 00:15:30,827 --> 00:15:35,457 but you see what happened to me. I ran a program before where I used i as 168 00:15:35,457 --> 00:15:43,220 an index and I, being the square root of minus 1, got redefined by my program. 169 00:15:43,220 --> 00:15:48,533 There's a special symbol, 1i, Which is an illegal variable name that is reserved in 170 00:15:48,533 --> 00:15:54,836 Mat lab to be the square root of -1. And also we want j as defined, which is 171 00:15:54,836 --> 00:15:59,674 also the square root of -1. So I would tend to use 1j. 172 00:15:59,674 --> 00:16:06,933 So I'm going to define y to be equal to the exponent of 1j times 2 times pi times 173 00:16:06,933 --> 00:16:14,73 a vector, f, which I'm going to say runs from 0 to point, in terms of steps, of 174 00:16:14,73 --> 00:16:23,388 101 to, 2. Let's see, I notice I didn't have to 175 00:16:23,388 --> 00:16:29,778 define a immediate variable. I can just type this vector inside, and 176 00:16:29,778 --> 00:16:34,352 everything's going to work fine, so I do that and if I did plot y. 177 00:16:34,352 --> 00:16:40,242 Y is complex and what its going to do is plot the imaginary part versus the real 178 00:16:40,242 --> 00:16:42,582 part. And that's a circle. 179 00:16:42,582 --> 00:16:46,565 So I can also plot, the real part of y, which is a cosine. 180 00:16:46,565 --> 00:16:59,160 And notice I did not give it an x argument. 181 00:16:59,160 --> 00:17:03,315 What it's doing, it's just plotting it against the index into the vector. 182 00:17:03,315 --> 00:17:07,914 And this is a way of getting plots going very quickly, not worrying about the 183 00:17:07,914 --> 00:17:14,940 details of a plot when you're trying to debug or get something just to work. 184 00:17:14,940 --> 00:17:20,930 And of course you can do the imaginary part. 185 00:17:20,930 --> 00:17:24,840 And there's the sine wave that's coming out of the complex exponential. 186 00:17:24,840 --> 00:17:28,20 But notice, it, the thing I want to emphasize is it did calculations with 187 00:17:28,20 --> 00:17:31,380 complex numbers just like they were real numbers. 188 00:17:31,380 --> 00:17:35,230 It really doesn't care. So, this is very handy for us, because 189 00:17:35,230 --> 00:17:40,54 here's our complex exponential signal, we use that a lot in this course, and it's 190 00:17:40,54 --> 00:17:44,446 very easy to type it in and use it just like It was a, a real variable that you 191 00:17:44,446 --> 00:17:50,880 would, in most other programming languages. 192 00:17:50,880 --> 00:17:53,876 So I think you can see why MATLAB is so useful. 193 00:17:53,876 --> 00:17:57,770 Learn, play with it, learn its commands, and one other thing before I leave, is I 194 00:17:57,770 --> 00:18:03,954 want to show you how to write, a loop. So the For command exists in MATLAB, and 195 00:18:03,954 --> 00:18:08,269 I'm just going to say i equals one colon ten. 196 00:18:08,269 --> 00:18:12,253 And I think, I forgot the equals, equals one colon ten. 197 00:18:12,253 --> 00:18:15,75 And I think you know what it's going to do. 198 00:18:15,75 --> 00:18:18,141 It's a loop. It's going to start with i equal to one, 199 00:18:18,141 --> 00:18:23,53 and wind up at a value of ten, incrementing by one every time. 200 00:18:23,53 --> 00:18:31,504 And I'm going to say that Z and I is equal to I up arrow T. 201 00:18:31,504 --> 00:18:37,394 So I'm going to put in the squares, just kind of a silly program, and I've been, 202 00:18:37,394 --> 00:18:45,326 the way that the for loop is terminated is with the end statement. 203 00:18:45,326 --> 00:18:52,228 And it has set up a vector z, which is going to have 10 elements equal in it, 204 00:18:52,228 --> 00:18:59,250 equal to the squares. I could have done this much more simply. 205 00:18:59,250 --> 00:19:11,867 Let's say, for x equals 1 colon 10. Does exactly the same thing, except I 206 00:19:11,867 --> 00:19:17,87 didn't square it, and now if I do x.up there 2, now I get the same thing as z, 207 00:19:17,87 --> 00:19:22,655 and I, could've either written a loop or I could've used, used the special 208 00:19:22,655 --> 00:19:30,401 notation in Matlab. And I think it's pretty clear that 209 00:19:30,401 --> 00:19:34,528 writing the loop is not that great a deal. 210 00:19:34,528 --> 00:19:43,710 Now, suppose I wanted to find this, the average of the x's. 211 00:19:43,710 --> 00:19:46,910 Okay. There's a special command that will do 212 00:19:46,910 --> 00:19:51,294 that called, sum, and that will sum it all up. 213 00:19:51,294 --> 00:19:56,113 And you can divide by the length of x, and I think it's pretty clear what the 214 00:19:56,113 --> 00:20:01,244 length is going to do. And that's the average of the squares of 215 00:20:01,244 --> 00:20:06,190 the first 10 integers. You can also do some other very important 216 00:20:06,190 --> 00:20:10,472 things like this that are matrix-like things. 217 00:20:10,472 --> 00:20:14,925 There's a mean function which will be exactly the same thing as what I just 218 00:20:14,925 --> 00:20:20,50 typed, and of course it gives exactly the same answer. 219 00:20:20,50 --> 00:20:24,146 There are many, many built in functions ready for you to use, and you just have 220 00:20:24,146 --> 00:20:30,198 to go in there and explore MATLAB and find out, to find out what they are. 221 00:20:30,198 --> 00:20:34,293 There's many many tutorials on lab, online, for helping you on getting into 222 00:20:34,293 --> 00:20:39,636 MATLAB and get started, but I think you can see why we use it a lot. 223 00:20:39,636 --> 00:20:44,472 And you are, will be asked to use it to work some of the homework problems. 224 00:20:44,472 --> 00:20:47,456 because. I need some numeric answers 225 00:20:47,456 --> 00:20:51,334 occassionally. Hope you enjoy using matlab in this 226 00:20:51,334 --> 00:20:53,673 course and later on.