1 00:00:07,390 --> 00:00:12,060 PROFESSOR: This is [? Doctor ?] MATLAB, Lecture 6, debugging. 2 00:00:12,060 --> 00:00:15,090 We will use the function Fibbo2 to demonstrate 3 00:00:15,090 --> 00:00:17,350 some basic debugging. 4 00:00:17,350 --> 00:00:19,830 The first thing you want to do when you debug 5 00:00:19,830 --> 00:00:22,910 is set a breakpoint at an interesting line. 6 00:00:22,910 --> 00:00:26,400 We'll set it at the very first interesting line 7 00:00:26,400 --> 00:00:28,450 of the function. 8 00:00:28,450 --> 00:00:32,369 Since Fibbo2 is not in the current folder nor in the path, 9 00:00:32,369 --> 00:00:35,110 MATLAB pops up this download box and asks us 10 00:00:35,110 --> 00:00:38,476 whether we should add it to the path or simply change folder. 11 00:00:38,476 --> 00:00:41,110 We're going to change folder. 12 00:00:41,110 --> 00:00:44,031 So now that we've changed folder, 13 00:00:44,031 --> 00:00:45,820 MATLAB was willing to put a breakpoint. 14 00:00:45,820 --> 00:00:51,840 And now if we run Fibbo2 with 3, we 15 00:00:51,840 --> 00:00:54,530 will get stopped right here with the green arrow 16 00:00:54,530 --> 00:00:56,530 on the breakpoint. 17 00:00:56,530 --> 00:00:58,950 Notice that at this point, n is defined 18 00:00:58,950 --> 00:01:03,410 to be 3 because that was the input we get gave Fibbo. 19 00:01:03,410 --> 00:01:11,040 F is still not defined, and v is not defined either. 20 00:01:11,040 --> 00:01:14,940 After we do this one line, F will be defined. 21 00:01:14,940 --> 00:01:16,820 How do we do this one line? 22 00:01:16,820 --> 00:01:22,620 We have to either click on Step, or Step In, or Step Out, 23 00:01:22,620 --> 00:01:23,160 or Continue. 24 00:01:23,160 --> 00:01:24,826 All these things are slightly different. 25 00:01:24,826 --> 00:01:26,680 I'm going to use Step, in this case. 26 00:01:26,680 --> 00:01:30,750 So Step means to do this line and step to the next line. 27 00:01:33,820 --> 00:01:37,630 So now it did that and F has been initialized. 28 00:01:37,630 --> 00:01:42,659 Let's see-- yes, F is 1 1. 29 00:01:42,659 --> 00:01:43,450 Here is the helper. 30 00:01:46,282 --> 00:01:50,260 The helper is the function that actually 31 00:01:50,260 --> 00:01:53,240 does most of the calculation. 32 00:01:53,240 --> 00:01:58,770 If we step over this one, as well, we're almost done. 33 00:01:58,770 --> 00:02:03,840 Now F is already the full 3 vector, 34 00:02:03,840 --> 00:02:06,660 and we will accept the last term, 35 00:02:06,660 --> 00:02:13,280 Fn, and put it into v. Let's Step, 36 00:02:13,280 --> 00:02:15,060 and now we're about to end. 37 00:02:15,060 --> 00:02:17,760 Before we end, we can check that v is 2. 38 00:02:17,760 --> 00:02:19,290 So 2 is the value that's going to be 39 00:02:19,290 --> 00:02:22,320 returned in the next step. 40 00:02:22,320 --> 00:02:25,890 We can Step again, and now this arrow out 41 00:02:25,890 --> 00:02:28,590 means that we're about to leave the function when 42 00:02:28,590 --> 00:02:29,750 doing nothing else. 43 00:02:29,750 --> 00:02:32,160 And if we take one more Step, we're out. 44 00:02:32,160 --> 00:02:35,539 The answer is 2, and we're not in the debugging mode anymore. 45 00:02:38,440 --> 00:02:41,380 We never seem to have entered helper. 46 00:02:41,380 --> 00:02:45,010 And the reason for that was that we stepped over this line 47 00:02:45,010 --> 00:02:47,410 rather than stepping into it. 48 00:02:47,410 --> 00:02:49,350 Let's do that again. 49 00:02:49,350 --> 00:02:52,855 I'll move the breakpoint and put it here. 50 00:02:52,855 --> 00:02:53,920 And now I'll run it. 51 00:02:53,920 --> 00:02:56,860 I can't run the function by pressing this green arrow 52 00:02:56,860 --> 00:03:00,830 because I'm not giving it any input when I just run it. 53 00:03:00,830 --> 00:03:04,360 If I run it, I'll get an error. 54 00:03:04,360 --> 00:03:05,410 Now n is not defined. 55 00:03:08,050 --> 00:03:11,360 And if I step over right now, here's 56 00:03:11,360 --> 00:03:13,160 my error-- not enough input arguments 57 00:03:13,160 --> 00:03:15,210 because n was not defined. 58 00:03:15,210 --> 00:03:16,740 OK, so let's not run it this way. 59 00:03:16,740 --> 00:03:18,720 Let's run it this way, but this time I'll 60 00:03:18,720 --> 00:03:21,240 give it 5 as an input. 61 00:03:21,240 --> 00:03:23,240 OK, so I put my breakpoint here. 62 00:03:23,240 --> 00:03:24,840 I'm stopped right here. 63 00:03:24,840 --> 00:03:27,600 And now I'm going to not step over-- 64 00:03:27,600 --> 00:03:29,240 Step means step over. 65 00:03:29,240 --> 00:03:31,894 I'll Step Into. 66 00:03:31,894 --> 00:03:37,420 So Step Into means to step into this function call. 67 00:03:37,420 --> 00:03:39,930 So now we're inside the function helper. 68 00:03:39,930 --> 00:03:42,977 Notice that this green arrow tells us where we came from-- 69 00:03:42,977 --> 00:03:45,310 sorry, this non-green arrow tells us where we came from. 70 00:03:45,310 --> 00:03:49,410 And this green arrow tells us where we currently are. 71 00:03:49,410 --> 00:03:51,840 We can see all of these in the stack. 72 00:03:51,840 --> 00:03:52,940 Here's the base. 73 00:03:52,940 --> 00:03:58,430 This is where we called Fibbo2 here. 74 00:03:58,430 --> 00:04:02,980 Here's Fibbo2-- that's what this non-green arrow is. 75 00:04:02,980 --> 00:04:06,990 And here's Fibbo2/helper, that's this location right here. 76 00:04:06,990 --> 00:04:08,860 We can switch to whatever we want. 77 00:04:08,860 --> 00:04:10,427 So we can switch to the base. 78 00:04:10,427 --> 00:04:11,760 Now we're not here and not here. 79 00:04:11,760 --> 00:04:13,700 We're actually on the base. 80 00:04:13,700 --> 00:04:15,450 And we can see who the variables are here. 81 00:04:15,450 --> 00:04:16,980 There are not that many variables. 82 00:04:16,980 --> 00:04:18,570 Specifically, n is not a variable, 83 00:04:18,570 --> 00:04:21,110 and v is not a variable, and F is not a variable 84 00:04:21,110 --> 00:04:22,920 when we're out here. 85 00:04:22,920 --> 00:04:26,370 Let's go back to Fibbo2/helper. 86 00:04:26,370 --> 00:04:29,930 So this F, of course, is not going to be true. 87 00:04:29,930 --> 00:04:38,620 Step Over this and now it's going to kill helper again. 88 00:04:38,620 --> 00:04:41,010 Let's go once in, just to see how this looks. 89 00:04:41,010 --> 00:04:45,330 So if we go in, now the stack is becoming 90 00:04:45,330 --> 00:04:47,010 more and more complicated. 91 00:04:47,010 --> 00:04:51,240 There are two entries of Fibbo2/helper because helper 92 00:04:51,240 --> 00:04:54,770 called helper again. 93 00:04:54,770 --> 00:04:57,940 n at this point is 4. 94 00:04:57,940 --> 00:05:00,370 So in fact, it's probably going to call it 95 00:05:00,370 --> 00:05:04,930 one more time because the number of elements in F is still 2. 96 00:05:04,930 --> 00:05:07,450 We have to wait until we reach down to 2. 97 00:05:07,450 --> 00:05:14,400 So even if we Step In again here, 98 00:05:14,400 --> 00:05:16,390 now we're in helper three times. 99 00:05:16,390 --> 00:05:20,050 Take a look-- where's my stack? 100 00:05:20,050 --> 00:05:22,930 At this point, n is still 3. 101 00:05:22,930 --> 00:05:24,280 It will happen once more. 102 00:05:28,465 --> 00:05:32,240 And now it's going to all start collapsing back. 103 00:05:32,240 --> 00:05:36,780 So now I've called n with 3. 104 00:05:36,780 --> 00:05:39,310 Now I'm starting to come back out of it. 105 00:05:39,310 --> 00:05:42,130 So F is going to start growing. 106 00:05:42,130 --> 00:05:44,975 So helper has been called, n is 3. 107 00:05:44,975 --> 00:05:49,050 So now I'm going to put F3 equals F2 plus F1. 108 00:05:52,186 --> 00:05:53,860 Now this is a little bit boring. 109 00:05:53,860 --> 00:05:57,310 So I can use Step Out to step out of a function call. 110 00:05:57,310 --> 00:06:03,350 So this basically runs until the current element of stack 111 00:06:03,350 --> 00:06:03,870 goes out. 112 00:06:03,870 --> 00:06:04,520 It will return. 113 00:06:04,520 --> 00:06:09,260 So if I Step Out once, I'll have two functions-- well, 114 00:06:09,260 --> 00:06:11,970 four functions left-- twice helper and then 115 00:06:11,970 --> 00:06:13,840 Fibbo2 and base. 116 00:06:13,840 --> 00:06:18,450 So Step Out-- let's look at the stack again. 117 00:06:18,450 --> 00:06:21,960 Let's Step Out again. 118 00:06:21,960 --> 00:06:23,660 Let's look at the stack again-- 119 00:06:23,660 --> 00:06:24,590 good. 120 00:06:24,590 --> 00:06:29,970 At this point, F is growing and n is increasing. 121 00:06:29,970 --> 00:06:33,845 And each time, I'm filling up the relevant F. So now 122 00:06:33,845 --> 00:06:36,700 I'm going to fill up F5-- 123 00:06:36,700 --> 00:06:41,550 Step, return. 124 00:06:41,550 --> 00:06:43,400 Now I'm done with this function. 125 00:06:46,680 --> 00:06:52,440 And I'm exiting the function, so now I'm done here. 126 00:06:52,440 --> 00:06:56,210 Now all the data is done. 127 00:06:56,210 --> 00:06:57,950 I can Step. 128 00:06:57,950 --> 00:06:59,910 And if you're bored with this, you just 129 00:06:59,910 --> 00:07:03,300 want to finish the function, you can just call Continue. 130 00:07:03,300 --> 00:07:05,490 And it will run through the rest of the lines 131 00:07:05,490 --> 00:07:06,845 unless it hits a breakpoint. 132 00:07:12,670 --> 00:07:13,980 Now let's do this again. 133 00:07:13,980 --> 00:07:17,190 While I'm here-- now I'm here again. 134 00:07:17,190 --> 00:07:18,240 I'm in the command line. 135 00:07:18,240 --> 00:07:19,290 I'm inside this function. 136 00:07:19,290 --> 00:07:20,850 I can evaluate all kinds of stuff. 137 00:07:20,850 --> 00:07:25,170 I can look at F. I can look at F1 plus F2. 138 00:07:25,170 --> 00:07:28,470 I can evaluate all kinds of things and even change things. 139 00:07:28,470 --> 00:07:32,360 I can do silly stuff like F1 equals 3. 140 00:07:32,360 --> 00:07:35,047 And now F will be starting with 3 and 1. 141 00:07:35,047 --> 00:07:36,630 In the Fibonacci sequence, the results 142 00:07:36,630 --> 00:07:38,819 will be a very odd one, indeed. 143 00:07:38,819 --> 00:07:39,860 Let's see how this works. 144 00:07:39,860 --> 00:07:45,070 I'm going to put a breakpoint here, press continue-- 145 00:07:52,420 --> 00:07:53,900 oh, because I ran it. 146 00:07:53,900 --> 00:08:00,530 Let me call Fibbo5, now change this to 3, 147 00:08:00,530 --> 00:08:02,990 and now press Continue. 148 00:08:02,990 --> 00:08:05,780 OK, so now what is F? 149 00:08:05,780 --> 00:08:09,430 F is a Fibonacci sequence but it's not the Fibonacci sequence 150 00:08:09,430 --> 00:08:11,120 because it starts with 3 1. 151 00:08:11,120 --> 00:08:13,350 And then there a 4, which is the sum of 3 and 1; 152 00:08:13,350 --> 00:08:17,550 and then a 5, which is 4 and 1; and then a 9, which is 4 and 5, 153 00:08:17,550 --> 00:08:19,310 et cetera. 154 00:08:19,310 --> 00:08:23,425 I changed my Fibonacci sequence while it was running. 155 00:08:23,425 --> 00:08:24,800 And then that changed, of course, 156 00:08:24,800 --> 00:08:28,514 the rest of the operation. 157 00:08:28,514 --> 00:08:29,930 There's another type of breakpoint 158 00:08:29,930 --> 00:08:31,960 that you can do that's called a conditional breakpoint. 159 00:08:31,960 --> 00:08:33,020 So let's do this one. 160 00:08:33,020 --> 00:08:36,440 You notice that we had to wait until we knew that n was 2. 161 00:08:36,440 --> 00:08:41,250 We had to go Step In, Step In again and again. 162 00:08:41,250 --> 00:08:42,150 This can get boring. 163 00:08:42,150 --> 00:08:45,280 So what we can do, if you right click, 164 00:08:45,280 --> 00:08:48,480 you can set a conditional breakpoint. 165 00:08:48,480 --> 00:08:51,230 So I want to stop it on this line only 166 00:08:51,230 --> 00:08:54,500 if, let's say n equals 3. 167 00:08:57,790 --> 00:09:02,260 So now let me exit debug mode. 168 00:09:02,260 --> 00:09:06,116 I'm going to call Fibbo with 15, for example. 169 00:09:10,100 --> 00:09:11,660 First it stops here. 170 00:09:11,660 --> 00:09:13,980 I'll tell it to Continue. 171 00:09:13,980 --> 00:09:15,050 And now it stops here. 172 00:09:15,050 --> 00:09:17,830 Now notice this is when n equals 3. 173 00:09:17,830 --> 00:09:20,250 We started with n equals 15. 174 00:09:20,250 --> 00:09:22,250 So that means that our stack should be huge. 175 00:09:22,250 --> 00:09:25,950 Indeed, our stack is huge. 176 00:09:25,950 --> 00:09:27,790 We have Fibbo2 calling the helper, which 177 00:09:27,790 --> 00:09:28,814 calls the helper, and the helper, 178 00:09:28,814 --> 00:09:30,855 and the helper-- all these call the helper again, 179 00:09:30,855 --> 00:09:33,360 and again, and again until n goes down to 3. 180 00:09:33,360 --> 00:09:35,420 Now it's going to start popping back up again. 181 00:09:35,420 --> 00:09:41,310 All this time F hasn't been changed because this line has 182 00:09:41,310 --> 00:09:42,840 never been reached. 183 00:09:42,840 --> 00:09:46,190 But now it has reached for the first time. 184 00:09:46,190 --> 00:09:49,451 And if I click Continue, it won't stop at this breakpoint 185 00:09:49,451 --> 00:09:49,950 again. 186 00:09:49,950 --> 00:09:54,220 It only stops there once when n is 3. 187 00:09:54,220 --> 00:09:55,100 And now it ends. 188 00:09:55,100 --> 00:09:58,510 Now it knows we're ready, that v is whatever it is-- 189 00:09:58,510 --> 00:10:00,170 610. 190 00:10:00,170 --> 00:10:01,100 And it's done. 191 00:10:01,100 --> 00:10:06,340 One more Step-- it's almost done. 192 00:10:06,340 --> 00:10:08,970 One more Step, Continue-- and it's done. 193 00:10:08,970 --> 00:10:11,860 610 is the answer. 194 00:10:11,860 --> 00:10:14,990 And this is how you can use the debugger.