1 00:00:07,690 --> 00:00:09,970 YOSSI FARJOUN: This is Dr. MATLAB, Lecture 1-- 2 00:00:09,970 --> 00:00:13,540 Using MATLAB for the First Time. 3 00:00:13,540 --> 00:00:15,340 When you turn MATLAB on for the first time, 4 00:00:15,340 --> 00:00:18,920 you'll be greeted with a window that looks like this. 5 00:00:18,920 --> 00:00:20,980 The first thing we do is close all the windows 6 00:00:20,980 --> 00:00:24,370 that we don't need initially-- 7 00:00:24,370 --> 00:00:28,420 Current Directory, Workspace, and Command History. 8 00:00:28,420 --> 00:00:30,740 We're left with a command window. 9 00:00:30,740 --> 00:00:32,150 This is the command prompt. 10 00:00:32,150 --> 00:00:38,901 And this is the flashing cursor where our input will be placed. 11 00:00:38,901 --> 00:00:40,650 The first thing we want to do is try using 12 00:00:40,650 --> 00:00:42,630 MATLAB as a giant calculator. 13 00:00:42,630 --> 00:00:46,635 So for example, we can ask MATLAB to calculate 1 plus 1. 14 00:00:46,635 --> 00:00:48,540 The answer is 2. 15 00:00:48,540 --> 00:00:51,350 So we type what we want, 3 plus 4. 16 00:00:51,350 --> 00:00:55,800 Hit Enter and we get the answer-- 17 00:00:55,800 --> 00:01:04,503 3 times 4, 1 plus 2 times 3, 2 to the power of 4, 18 00:01:04,503 --> 00:01:09,050 5 to the power of 3, et cetera. 19 00:01:09,050 --> 00:01:12,920 We can also ask whether two numbers are the same, 20 00:01:12,920 --> 00:01:15,000 or whether one is greater than the other. 21 00:01:15,000 --> 00:01:18,760 So to see if 4 is greater than 5, we type this. 22 00:01:18,760 --> 00:01:20,280 This will tell us that the answer 23 00:01:20,280 --> 00:01:24,140 is no, 4 is not greater than 5. 24 00:01:24,140 --> 00:01:26,240 We can ask is 4 less than 5. 25 00:01:26,240 --> 00:01:28,629 The answer is yes, 4 is less than 5. 26 00:01:33,320 --> 00:01:36,470 Sometimes we need a function and expression, not just 27 00:01:36,470 --> 00:01:37,740 an operator. 28 00:01:37,740 --> 00:01:39,357 So for example, to find the remainder, 29 00:01:39,357 --> 00:01:40,690 we need to use the function rem. 30 00:01:40,690 --> 00:01:44,000 Here is the remainder of 15 when divided by 4-- 31 00:01:44,000 --> 00:01:45,200 rem(15,4). 32 00:01:45,200 --> 00:01:48,170 The answer is 3. 33 00:01:48,170 --> 00:01:50,480 To find out more about a function, 34 00:01:50,480 --> 00:01:52,430 we use the help command. 35 00:01:52,430 --> 00:01:57,350 help rem will tell us everything we need to know about rem-- 36 00:01:57,350 --> 00:01:59,080 the remainder after division. 37 00:01:59,080 --> 00:02:03,080 There's a bit of an explanation, some conventions. 38 00:02:03,080 --> 00:02:04,220 And the "see also"-- 39 00:02:04,220 --> 00:02:07,190 this is perhaps one of the most important parts of the help 40 00:02:07,190 --> 00:02:09,860 file-- is to help you find other commands that 41 00:02:09,860 --> 00:02:12,560 are useful and related to this command. 42 00:02:12,560 --> 00:02:15,340 I encourage you to try to find the difference between mod 43 00:02:15,340 --> 00:02:15,840 and rem. 44 00:02:18,930 --> 00:02:20,400 Other than calculating expressions, 45 00:02:20,400 --> 00:02:22,950 we can also assign their value into variables. 46 00:02:22,950 --> 00:02:26,270 So for example, if I want to create a variable named x, 47 00:02:26,270 --> 00:02:29,200 I type x equals and then the value that I want. 48 00:02:29,200 --> 00:02:35,130 So for example, x=1 will create a variable called x and put 1 49 00:02:35,130 --> 00:02:37,480 as the value into it. 50 00:02:37,480 --> 00:02:40,410 I can also put the result of an expression into a variable. 51 00:02:40,410 --> 00:02:41,160 Here's y=2*3. 52 00:02:43,847 --> 00:02:44,680 Of course, that's 6. 53 00:02:44,680 --> 00:02:47,825 So it calculates the 6 and puts 2 times 3 into it. 54 00:02:47,825 --> 00:02:51,080 So now y is 6 and x is 1. 55 00:02:51,080 --> 00:02:54,675 And I can use x and y if they are numbers. x+y, 56 00:02:54,675 --> 00:02:56,610 the answer is 7. 57 00:02:56,610 --> 00:02:58,750 If I try to use a variable that doesn't yet exist, 58 00:02:58,750 --> 00:03:02,880 for example z+y, I get an error-- 59 00:03:02,880 --> 00:03:05,660 undefined function or variables. 60 00:03:05,660 --> 00:03:08,450 Sometimes, you're looking for a command 61 00:03:08,450 --> 00:03:09,810 but you don't know the command. 62 00:03:09,810 --> 00:03:11,601 You know that you're looking for something, 63 00:03:11,601 --> 00:03:13,700 bu you just don't know what it's called in MATLAB. 64 00:03:13,700 --> 00:03:16,425 In that case, you should be using lookfor 65 00:03:16,425 --> 00:03:18,050 to find the command you're looking for. 66 00:03:18,050 --> 00:03:22,790 So for example, lookfor square will give you 67 00:03:22,790 --> 00:03:27,350 all the commands that have the word square in their help file. 68 00:03:27,350 --> 00:03:31,346 Of course, I was looking for, in this case, square root. 69 00:03:31,346 --> 00:03:31,970 And here it is. 70 00:03:31,970 --> 00:03:33,800 I can either click on this and that 71 00:03:33,800 --> 00:03:37,580 will give me the help file of the square root. 72 00:03:37,580 --> 00:03:41,630 Or I can just type help sqrt. 73 00:03:41,630 --> 00:03:44,850 And that will give the same. 74 00:03:44,850 --> 00:03:47,950 Square root finds the square of the elements. 75 00:03:47,950 --> 00:03:51,960 So if I do sqrt(4), I get 2. 76 00:03:51,960 --> 00:03:54,120 Notice that I get complex results 77 00:03:54,120 --> 00:03:57,030 if I get a non-positive input. 78 00:03:57,030 --> 00:04:05,400 So sqrt(-3) is the square root of three times i, which is, 79 00:04:05,400 --> 00:04:06,720 of course, very nice to know. 80 00:04:06,720 --> 00:04:15,240 So 2i^2 should give me -4, and indeed, 2i squared gives me -4. 81 00:04:15,240 --> 00:04:17,399 On the command line, there is a useful trick 82 00:04:17,399 --> 00:04:19,300 involving the "up" button. 83 00:04:19,300 --> 00:04:21,690 Sometimes, you're looking for something 84 00:04:21,690 --> 00:04:27,740 or you're typing something and you make a typo, like this. 85 00:04:27,740 --> 00:04:29,250 I've swapped the L and the E. Now 86 00:04:29,250 --> 00:04:30,900 I could retype everything from scratch, 87 00:04:30,900 --> 00:04:33,030 or I can press the up key-- 88 00:04:33,030 --> 00:04:40,820 up arrow-- and then the left arrow until I can delete the E, 89 00:04:40,820 --> 00:04:42,440 and put it in the right place. 90 00:04:42,440 --> 00:04:47,720 Now when I hit Enter and I get the help info. 91 00:04:47,720 --> 00:04:49,280 With the up arrow and the down arrow, 92 00:04:49,280 --> 00:04:53,300 I can browse through the history of all the commands that I did. 93 00:04:57,340 --> 00:05:00,200 If I have a longish expression on the command line 94 00:05:00,200 --> 00:05:01,910 and I want to erase the whole thing, 95 00:05:01,910 --> 00:05:04,040 I could start erasing it. 96 00:05:04,040 --> 00:05:11,360 Or I could press Control-C. 97 00:05:11,360 --> 00:05:13,040 That's probably enough for today. 98 00:05:13,040 --> 00:05:18,030 To exit MATLAB, I either look for the Quit MATLAB here 99 00:05:18,030 --> 00:05:23,580 or I can Apple-Q. Or from the command line, I can type exit.