1
00:00:15,000 --> 00:00:18,000
Hi. I'm Doug Baldwin.
2
00:00:18,000 --> 00:00:22,400
In this video I'm going to introduce something called local variables,
3
00:00:22,400 --> 00:00:26,066
including why you would use them and how to do it in Matlab.
4
00:00:26,066 --> 00:00:29,233
Local variables are an important programming idea
5
00:00:29,233 --> 00:00:33,966
that can make it easier for you to write programs and for other people to read them.
6
00:00:33,966 --> 00:00:37,366
This video introduces them through some examples
7
00:00:37,366 --> 00:00:41,233
that use Matlab's command window and a user-defined function,
8
00:00:41,233 --> 00:00:46,133
so before you watch this you should be comfortable writing arithmetic
expressions in Matlab
9
00:00:46,133 --> 00:00:51,000
and defining functions. Assuming you are, let's get started!
10
00:00:51,000 --> 00:00:51,200
11
00:00:51,200 --> 00:00:55,666
To start to appreciate local variables, let's use Matlab's command window
12
00:00:55,666 --> 00:01:00,033
and the quadratic formula to find the places where a quadratic polynomial is 0.
13
00:01:00,033 --> 00:01:06,533
But this isn't the nice tidy sort of polynomial you find in textbooks, this is an ugly one from the wild:
14
00:01:06,533 --> 00:01:15,200
93.65 x squared plus 153 and 11/17ths x minus 968.2.
15
00:01:15,200 --> 00:01:19,900
Fortunately, the nice thing about mathematical laws is that they apply in the wild
16
00:01:19,900 --> 00:01:23,766
as well as in textbooks, so we can still use the quadratic formula,
17
00:01:23,766 --> 00:01:27,366
and find the x values that make this expression zero by calculating
18
00:01:27,366 --> 00:01:33,500
minus b plus or minus the square root of b squared minus 4 a c all over 2 a.
19
00:01:33,500 --> 00:01:40,633
The hardest part is entering awkward numbers like 93.65 or 153 and 11/17ths
20
00:01:40,633 --> 00:01:45,466
in the places that need them without making mistakes, and this is where variables come in.
21
00:01:45,466 --> 00:01:50,000
Indeed, just as mathematicians use the variables a, b, and c
22
00:01:50,000 --> 00:01:53,966
to name the arbitrary numbers that might appear in a quadratic polynomial,
23
00:01:53,966 --> 00:01:58,366
we'll do the same in Matlab. Here's how:
24
00:01:58,366 --> 00:02:03,966
To create a local variable in Matlab, you just need to say what value it stands for.
25
00:02:03,966 --> 00:02:10,733
You do this by writing a statement that consists of the variable's name, an equal sign, and the value.
26
00:02:10,733 --> 00:02:13,933
Such statements are known as "assignment statements."
27
00:02:13,933 --> 00:02:18,733
For example, to define a to stand for the number 93.65,
28
00:02:18,733 --> 00:02:25,100
I write a equals 93.65 and press enter.
29
00:02:25,100 --> 00:02:30,266
Like other Matlab statements, assignment statements print the value they generate,
30
00:02:30,266 --> 00:02:33,800
which in this example was just 93.65.
31
00:02:33,800 --> 00:02:38,133
You can suppress this output by ending the command with a semicolon.
32
00:02:38,133 --> 00:02:42,933
Continuing on, I'll define b using another assignment statement.
33
00:02:42,933 --> 00:02:46,633
Matlab doesn't directly understand fraction notation,
34
00:02:46,633 --> 00:02:51,200
but the value on the right side of an assignment statement can be any Matlab expression,
35
00:02:51,200 --> 00:02:55,800
so I'll write one here that evaluates to 153 and 11/17ths
36
00:02:55,800 --> 00:03:00,033
without having to figure out its decimal form for myself.
37
00:03:00,033 --> 00:03:04,766
Finally I'll define c, this time using a semicolon at the end of the statement
38
00:03:04,766 --> 00:03:08,733
because I don't really need Matlab to print c's value.
39
00:03:08,733 --> 00:03:12,633
Now I could write out the quadratic formula in Matlab notation,
40
00:03:12,633 --> 00:03:17,800
using my a, b, and c variables, and find out where that polynomial is 0.
41
00:03:17,800 --> 00:03:23,200
But before I rush in, there's one more way local variables can make it easier for me:
42
00:03:23,200 --> 00:03:27,300
I'm going to need the "square root of b squared minus 4 a c"
43
00:03:27,300 --> 00:03:30,900
part of the formula twice, once for each x value,
44
00:03:30,900 --> 00:03:35,000
and rather than type it two times I can store its value in another variable
45
00:03:35,000 --> 00:03:38,666
and just use that variable in the rest of the formula.
46
00:03:38,666 --> 00:03:42,966
Since the quantity b squared minus 4 a c is called the "discriminant"
47
00:03:42,966 --> 00:03:47,300
in the quadratic formula, and the new variable stands for its square root,
48
00:03:47,300 --> 00:03:51,100
I'll name the variable "rootD," and define it like so:
49
00:03:51,100 --> 00:03:59,233
rootD equals square root b squared minus 4 times a times c.
50
00:03:59,233 --> 00:04:03,500
Notice that I'm using the values of variables a, b, and c to do this.
51
00:04:03,500 --> 00:04:07,133
Like all variables, you use a local variable's value
52
00:04:07,133 --> 00:04:11,200
by simply writing the variable's name wherever you want to use the value.
53
00:04:11,200 --> 00:04:15,200
Now I'm finally ready to evaluate the quadratic formula.
54
00:04:15,200 --> 00:04:19,866
Thanks to the local variables I set up, there's really not much typing needed,
55
00:04:19,866 --> 00:04:22,966
and thus less chance of making typing mistakes.
56
00:04:22,966 --> 00:04:28,833
For the first x value, I simply ask for minus b minus rootD all over 2 times a,
57
00:04:28,833 --> 00:04:36,500
and for the second x value I ask for minus b plus rootD all over 2 times a.
58
00:04:36,500 --> 00:04:41,500
This quadratic formula example showed how to use local variables in the command window,
59
00:04:41,500 --> 00:04:44,500
but they're also useful in function definitions.
60
00:04:44,500 --> 00:04:50,133
In fact, the phrase "local variable" makes more sense in that context than in the command window.
61
00:04:50,133 --> 00:04:55,100
As an example, suppose we wanted to write a function that finds the volume of a cone
62
00:04:55,100 --> 00:04:59,366
from its height, h, and the angle, theta, at its apex.
63
00:04:59,366 --> 00:05:03,200
If you look up the textbook formula for the volume of a cone you'll find it
64
00:05:03,200 --> 00:05:08,866
in terms of the height and base radius: volume = one third pi r squared h.
65
00:05:08,866 --> 00:05:12,333
Of course we have the apex angle, not the radius,
66
00:05:12,333 --> 00:05:16,500
but with a little trigonometry we can calculate the radius from the angle:
67
00:05:16,500 --> 00:05:21,633
the cone's axis and any radial line form a right triangle with the cone's side,
68
00:05:21,633 --> 00:05:26,100
and that triangle has angle theta over 2 opposite the radial line.
69
00:05:26,100 --> 00:05:29,966
Thus r over h is the tangent of theta over 2,
70
00:05:29,966 --> 00:05:34,033
and so r equals h times the tangent of theta over 2.
71
00:05:34,033 --> 00:05:39,533
Plugging this into the textbook formula for volume gives us a formula we could use in our function,
72
00:05:39,533 --> 00:05:45,733
namely volume equals pi times h tangent theta over 2 squared times h all over 3,
73
00:05:45,733 --> 00:05:52,200
or, in slightly simpler form, pi h cubed tangent squared theta over 2 all over 3.
74
00:05:52,200 --> 00:05:56,333
However, if we just write this formula into the function,
75
00:05:56,333 --> 00:06:00,200
other people reading it won't know where it comes from or if it's even right;
76
00:06:00,200 --> 00:06:05,233
in fact we probably won't remember those things either for more than a week after we write the function.
77
00:06:05,233 --> 00:06:10,700
So the function will be much easier to understand if we break the calculation into two steps,
78
00:06:10,700 --> 00:06:17,300
the first calculating the radius from theta, and the second using that radius in the textbook volume formula.
79
00:06:17,300 --> 00:06:22,400
The key to connecting the two steps is, not surprisingly, a local variable.
80
00:06:22,400 --> 00:06:29,100
Putting these ideas to work, let's call the local variable "radius," since it stands for the cone's radius.
81
00:06:29,100 --> 00:06:34,466
The statement that defines it says radius equals h times tangent theta over 2,
82
00:06:34,466 --> 00:06:40,500
and let's suppress output since it's generally nicer for users if functions don't gratuitously display things.
83
00:06:40,500 --> 00:06:43,633
Now the next statement computes the actual volume,
84
00:06:43,633 --> 00:06:48,333
using the standard formula pi times radius squared times h divided by 3,
85
00:06:48,333 --> 00:06:52,500
and we're all done. Now we should save the function and test it.
86
00:06:52,500 --> 00:06:59,466
A simple test case would be a cone with height 1 and a 90 degree (i.e., pi over 2 radian) apex angle;
87
00:06:59,466 --> 00:07:04,066
I picked this cone because half the apex angle is 45 degrees,
88
00:07:04,066 --> 00:07:08,500
so the radius equals the height, i.e., r and h are both 1,
89
00:07:08,500 --> 00:07:14,766
so the volume is quick to calculate in my head, namely pi over 3 or about 1.047.
90
00:07:14,766 --> 00:07:18,333
And sure enough, that's what our function produces.
91
00:07:18,333 --> 00:07:24,966
You now know almost everything there is to know about local variables, except why they're called "local."
92
00:07:24,966 --> 00:07:30,466
While all variables serve the same central purpose of providing names that stand for values,
93
00:07:30,466 --> 00:07:34,000
different kinds of variable differ in their details.
94
00:07:34,000 --> 00:07:38,100
The key difference between local variables and others is that local variables
95
00:07:38,100 --> 00:07:44,500
can only be used in the function or command window that defines them -- they are "local" to that environment.
96
00:07:44,500 --> 00:07:48,500
For example, defining a local variable named "x" in some function
97
00:07:48,500 --> 00:07:52,333
does not cause "x" in the command window, or in any other function,
98
00:07:52,333 --> 00:07:55,800
to stand for the value it stands for in that first function.
99
00:07:55,800 --> 00:08:02,433
In fact, having a local "x" in one function doesn't even require that there be variables named "x" anywhere else at all.
100
00:08:02,433 --> 00:08:06,500
The flip side of this is that the command window and various functions
101
00:08:06,500 --> 00:08:10,700
can all have local variables with the same name without interfering with each other.
102
00:08:10,700 --> 00:08:16,733
For example, if you define local variable "y" in the command window to stand for 3,
103
00:08:16,733 --> 00:08:20,500
and local variable "y" in some function to stand for 5,
104
00:08:20,500 --> 00:08:27,000
calling the function from the command window won't change what "y" stands for in the command window (it will always be 3),
105
00:08:27,000 --> 00:08:31,800
while uses of "y" in the function will unambiguously mean 5.
106
00:08:31,800 --> 00:08:38,633
Notice that local variables are in some sense more local than functions' argument variables or result variables,
107
00:08:38,633 --> 00:08:44,000
since you cannot use local variables to communicate information between a function and its caller.
108
00:08:44,000 --> 00:08:49,333
While argument variables have their values automatically defined during function calls,
109
00:08:49,333 --> 00:08:54,333
and the values of result variables are given back to the caller during function return,
110
00:08:54,333 --> 00:08:57,300
local variables do none of these things.
111
00:08:57,300 --> 00:09:02,300
Of particular note, this means that you must always define local variables' values
112
00:09:02,300 --> 00:09:05,633
via assignment statements before you use them;
113
00:09:05,633 --> 00:09:12,133
having an expression that uses a local variable before the statement that defines it is an error.
114
00:09:12,133 --> 00:09:15,966
This is the end of this video on local variables in Matlab.
115
00:09:15,966 --> 00:09:22,400
You should now know how to use local variables to name important or often-used values in a function or program,
116
00:09:22,400 --> 00:09:26,500
and how doing so can make your code easier to write and understand.
117
00:09:26,500 --> 00:09:29,766
You know the syntax for defining a local variable,
118
00:09:29,766 --> 00:09:36,100
namely the variable's name, an equal sign, and an expression whose value the variable will stand for.
119
00:09:36,100 --> 00:09:40,500
So go out and use local variables in your own programming, and good luck!