1 00:00:07,660 --> 00:00:09,556 PROFESSOR: This is Dr. [? MATLAB, ?] lecture 2 00:00:09,556 --> 00:00:13,350 5, scripts and functions. 3 00:00:13,350 --> 00:00:17,090 The previous lecture we saw the function FtoC that 4 00:00:17,090 --> 00:00:20,461 converts Fahrenheit to Celsius. 5 00:00:20,461 --> 00:00:22,169 Let's take another look at that function. 6 00:00:25,592 --> 00:00:26,720 Here it is. 7 00:00:26,720 --> 00:00:29,130 It's a very simple function. 8 00:00:29,130 --> 00:00:30,700 I've changed it around a little bit, 9 00:00:30,700 --> 00:00:32,960 but it accepts Fer as input. 10 00:00:32,960 --> 00:00:34,690 Cel is output. 11 00:00:34,690 --> 00:00:37,550 And this is how we write it. 12 00:00:37,550 --> 00:00:39,660 I could have, of course, written this as a script. 13 00:00:39,660 --> 00:00:44,000 And it would have been a much worse idea. 14 00:00:44,000 --> 00:00:45,160 Let's try it as a script. 15 00:00:45,160 --> 00:00:46,785 I'll comment this out. 16 00:00:46,785 --> 00:00:48,250 And I'll comment this out. 17 00:00:48,250 --> 00:00:55,920 And now if I save it, I can change directory to Scripts. 18 00:00:55,920 --> 00:01:03,775 I can say Fer equals 80 and now call my FtoC. 19 00:01:03,775 --> 00:01:07,840 And now if I look at Cel, it equals the answer. 20 00:01:07,840 --> 00:01:10,160 The problem with this-- 21 00:01:10,160 --> 00:01:12,660 there are several problems with this. 22 00:01:12,660 --> 00:01:15,370 One is that I need to understand the inner working, 23 00:01:15,370 --> 00:01:19,870 my inner notation of the script in order to use it. 24 00:01:19,870 --> 00:01:22,000 I need to know that Fer is an input. 25 00:01:22,000 --> 00:01:24,520 I need to know that Cel is the output. 26 00:01:24,520 --> 00:01:26,520 So I need to know that before I call the script, 27 00:01:26,520 --> 00:01:30,540 I have to assign 80 into Fer. 28 00:01:30,540 --> 00:01:33,876 After I call the script, I get the answer inside Cel. 29 00:01:33,876 --> 00:01:35,000 I need to know all of that. 30 00:01:35,000 --> 00:01:36,375 And of course, most functions are 31 00:01:36,375 --> 00:01:38,120 going to be much more complicated 32 00:01:38,120 --> 00:01:40,950 than this one-liner. 33 00:01:40,950 --> 00:01:44,180 Another problem is that, obviously since it requires 34 00:01:44,180 --> 00:01:47,210 me to write into Fer and write into Cel, 35 00:01:47,210 --> 00:01:50,050 it will overwrite Fer and Cel. 36 00:01:50,050 --> 00:01:52,320 And particularly, it will overwrite Cel. 37 00:01:52,320 --> 00:02:02,630 So if I have an important number in Cel and now I call FtoC, 38 00:02:02,630 --> 00:02:05,240 this important number is overwritten. 39 00:02:05,240 --> 00:02:07,850 There's nothing I can do about it. 40 00:02:07,850 --> 00:02:10,580 Except for changing this script, there's 41 00:02:10,580 --> 00:02:12,890 no way I can make it not overwrite Cel. 42 00:02:15,510 --> 00:02:17,670 Now most functions have all kinds 43 00:02:17,670 --> 00:02:21,030 of intermediary variables. 44 00:02:21,030 --> 00:02:24,510 So ind1 and there's some assignment 45 00:02:24,510 --> 00:02:29,910 that goes into it there, and another one, 46 00:02:29,910 --> 00:02:33,549 and so on, which are needed for the calculation. 47 00:02:33,549 --> 00:02:36,090 These are of course fake ones, but imagine that you had these 48 00:02:36,090 --> 00:02:38,070 and now you would call these. 49 00:02:38,070 --> 00:02:41,270 Sorry, you would call FtoC again. 50 00:02:41,270 --> 00:02:47,070 You would be littered by these variables 51 00:02:47,070 --> 00:02:50,222 that you don't know where they came from. 52 00:02:50,222 --> 00:02:51,930 You don't care about them because they're 53 00:02:51,930 --> 00:02:54,210 some partial result inside the script. 54 00:02:54,210 --> 00:02:56,520 And also they might have overwritten one 55 00:02:56,520 --> 00:02:57,270 of your variables. 56 00:02:57,270 --> 00:02:59,790 So this could be really a disaster. 57 00:02:59,790 --> 00:03:02,700 This is where scoping is important. 58 00:03:02,700 --> 00:03:04,590 When you put this inside a function, 59 00:03:04,590 --> 00:03:08,100 even if you keep all the littering the same, 60 00:03:08,100 --> 00:03:10,365 now you call this function-- 61 00:03:10,365 --> 00:03:13,000 let's clear my variables first. 62 00:03:13,000 --> 00:03:14,640 Let's look who's here. 63 00:03:14,640 --> 00:03:17,300 No one's here. 64 00:03:17,300 --> 00:03:21,800 And now I'm going to call FtoC with 80. 65 00:03:21,800 --> 00:03:25,310 Again the answer, and the only variable that exists, 66 00:03:25,310 --> 00:03:28,010 is this automatic variable called ans. 67 00:03:28,010 --> 00:03:30,110 Ans is an automatic variable that 68 00:03:30,110 --> 00:03:33,230 is generated when a value is returned, 69 00:03:33,230 --> 00:03:34,790 but it's not assigned. 70 00:03:34,790 --> 00:03:38,410 So for example, if I do 1 plus 2, there is also an ans. 71 00:03:38,410 --> 00:03:40,370 And ans equals 3. 72 00:03:40,370 --> 00:03:42,090 This is useful if, for example-- 73 00:03:42,090 --> 00:03:44,894 notice that here I forgot to actually assign this 74 00:03:44,894 --> 00:03:45,560 into a variable. 75 00:03:45,560 --> 00:03:49,330 So I can now save it by saying, x equals ans. 76 00:03:49,330 --> 00:03:52,680 And now my answer is saved into x. 77 00:03:52,680 --> 00:03:57,110 Again, using functions does not litter my work space 78 00:03:57,110 --> 00:04:00,270 with partial results. 79 00:04:00,270 --> 00:04:03,460 You see ind1, ind2 are contained inside this function, 80 00:04:03,460 --> 00:04:06,510 and we do not see them outside. 81 00:04:06,510 --> 00:04:10,974 And they make it unnecessary to know the inner workings 82 00:04:10,974 --> 00:04:11,640 of the function. 83 00:04:11,640 --> 00:04:14,910 I don't need to know what the inner name of the input 84 00:04:14,910 --> 00:04:17,730 variable is and what the inner name of the output variable is. 85 00:04:17,730 --> 00:04:22,170 I just need to know that FtoC accepts an input variable 86 00:04:22,170 --> 00:04:23,800 and returns a value. 87 00:04:23,800 --> 00:04:26,108 I don't need to know the name of those variables. 88 00:04:30,700 --> 00:04:32,770 Scope also allows us to write what's 89 00:04:32,770 --> 00:04:35,395 called recursive functions. 90 00:04:35,395 --> 00:04:39,190 A classic example of recursive function 91 00:04:39,190 --> 00:04:41,390 is the function that creates the Fibonacci sequence. 92 00:04:41,390 --> 00:04:42,320 So let's try this. 93 00:04:45,610 --> 00:04:48,110 We already had one Fibonacci so let's do Fibbo2. 94 00:04:51,780 --> 00:04:54,740 And it accepts an n. 95 00:04:54,740 --> 00:04:55,960 The base case, of course. 96 00:05:00,220 --> 00:05:01,670 We need to return a value. 97 00:05:05,420 --> 00:05:07,130 And if it's not less than three, then 98 00:05:07,130 --> 00:05:14,570 we need to return the sum of the two lower Fibbonacci numbers. 99 00:05:18,350 --> 00:05:22,090 This is a horrible way of creating the Fibonacci sequence 100 00:05:22,090 --> 00:05:24,820 because it involves an exponentially large amount 101 00:05:24,820 --> 00:05:27,710 of function calls. 102 00:05:27,710 --> 00:05:29,000 But it works. 103 00:05:29,000 --> 00:05:32,810 And the reason it works is because the Fibbo in here, 104 00:05:32,810 --> 00:05:35,780 the variables inside it, are also 105 00:05:35,780 --> 00:05:39,080 disconnected from the Fibbo here and the Fibbo here. 106 00:05:39,080 --> 00:05:42,180 All these functions are called one inside the other, 107 00:05:42,180 --> 00:05:45,020 but they don't overwrite the variables. 108 00:05:45,020 --> 00:05:47,240 So the v inside the inner Fibbo is 109 00:05:47,240 --> 00:05:51,620 different from the v inside the outer Fibbo and so on. 110 00:05:51,620 --> 00:05:52,810 So let me save this. 111 00:06:11,125 --> 00:06:12,540 OK, so this works. 112 00:06:12,540 --> 00:06:17,290 Now notice that if I try it with 25, it takes a bit of time. 113 00:06:17,290 --> 00:06:19,540 And 35 is almost too long to run. 114 00:06:19,540 --> 00:06:22,030 I'm not going to even wait for this to run. 115 00:06:22,030 --> 00:06:25,382 This is calling 2 to the power 35 function calls, which is 116 00:06:25,382 --> 00:06:26,590 a little bit too many for me. 117 00:06:26,590 --> 00:06:29,760 And I'm going to cancel it-- ctrl c, cancel. 118 00:06:29,760 --> 00:06:31,660 And you can see here-- 119 00:06:31,660 --> 00:06:33,870 let me show you the trace-- 120 00:06:33,870 --> 00:06:37,790 all the function calls when it was stopped. 121 00:06:37,790 --> 00:06:39,000 It was stopped here. 122 00:06:39,000 --> 00:06:40,472 But this was called from this line, 123 00:06:40,472 --> 00:06:41,930 and this was called from this line, 124 00:06:41,930 --> 00:06:43,325 and this was called from here. 125 00:06:43,325 --> 00:06:45,680 This is all the same line calling itself over and over 126 00:06:45,680 --> 00:06:51,470 again, each time lowering n by 1, until eventually n is 2. 127 00:06:51,470 --> 00:06:53,232 And then it can return the value. 128 00:07:00,847 --> 00:07:03,180 It's that it keeps forgetting the value of the Fibonacci 129 00:07:03,180 --> 00:07:05,520 sequence when it needs it again. 130 00:07:05,520 --> 00:07:09,450 To fix that, let us keep a variable that will actually 131 00:07:09,450 --> 00:07:12,260 hold the Fibonacci sequence. 132 00:07:12,260 --> 00:07:15,060 So we initialize it with 1 1, which is the starting point. 133 00:07:15,060 --> 00:07:17,580 And now we will call a helper file-- 134 00:07:24,949 --> 00:07:25,990 helper function, that is. 135 00:07:29,130 --> 00:07:34,410 And we will return the value that is needed. 136 00:07:34,410 --> 00:07:39,070 The helper function will guarantee that Fn is defined. 137 00:07:39,070 --> 00:07:41,800 Let's see how we do this. 138 00:07:41,800 --> 00:07:43,080 Here is the helper function. 139 00:07:43,080 --> 00:07:47,130 It doesn't have any return value, but it has an input. 140 00:07:47,130 --> 00:07:53,320 And all it does is checks if the number of elements in F 141 00:07:53,320 --> 00:07:54,360 is less than 10. 142 00:07:54,360 --> 00:07:58,630 And if it is, then it calculates the required one. 143 00:07:58,630 --> 00:08:05,020 So first it calls helper with n minus 1, 144 00:08:05,020 --> 00:08:14,655 and then it says that Fn equals Fn minus 1, plus Fn minus 2. 145 00:08:14,655 --> 00:08:16,630 And then it's done. 146 00:08:16,630 --> 00:08:18,220 So the helper function guarantees 147 00:08:18,220 --> 00:08:20,740 that the Fibonacci sequence is updated up 148 00:08:20,740 --> 00:08:26,560 to n, this variable F, and then we just return the one we need. 149 00:08:26,560 --> 00:08:29,580 So by this way, this assignment only 150 00:08:29,580 --> 00:08:34,340 happens once for every n, which makes it linear and end. 151 00:08:34,340 --> 00:08:36,580 Now we don't need all of this stuff. 152 00:08:36,580 --> 00:08:38,960 The only thing we do need is this final end. 153 00:08:38,960 --> 00:08:40,520 Let me show you here. 154 00:08:40,520 --> 00:08:44,940 We want this function here. 155 00:08:44,940 --> 00:08:46,410 Sorry, we need another end. 156 00:08:46,410 --> 00:08:52,670 We want this function here to be embedded inside this function 157 00:08:52,670 --> 00:08:54,800 because what that does is it makes 158 00:08:54,800 --> 00:08:58,384 this variable F visible both for this function 159 00:08:58,384 --> 00:08:59,300 and for this function. 160 00:08:59,300 --> 00:09:01,350 So this F and this F are the same variable. 161 00:09:01,350 --> 00:09:05,692 And the hover text says so-- 162 00:09:05,692 --> 00:09:11,254 the scope of variable F spans multiple functions. 163 00:09:11,254 --> 00:09:12,420 So let's see how that works. 164 00:09:12,420 --> 00:09:14,740 I'll save it. 165 00:09:14,740 --> 00:09:19,000 We call Fibbo2 is 3 and the answer is correct. 166 00:09:19,000 --> 00:09:22,670 4, 5, and 35-- 167 00:09:22,670 --> 00:09:26,750 no problem, 45-- no problem, 55-- 168 00:09:26,750 --> 00:09:29,500 no problem. 169 00:09:29,500 --> 00:09:31,120 This is a simple example of what's 170 00:09:31,120 --> 00:09:33,940 called dynamic programming. 171 00:09:33,940 --> 00:09:38,710 And this is a way of reducing an exponential recursive 172 00:09:38,710 --> 00:09:42,000 implementation into a linear one. 173 00:09:42,000 --> 00:09:45,890 Now, you can have more than one input variable, 174 00:09:45,890 --> 00:09:48,510 and you can have more than one output. 175 00:09:48,510 --> 00:09:52,880 But I suggest that you investigate that on your own.