WEBVTT

00:00:07.690 --> 00:00:09.970
YOSSI FARJOUN: This is
Dr. MATLAB, Lecture 1--

00:00:09.970 --> 00:00:13.540
Using MATLAB for the First Time.

00:00:13.540 --> 00:00:15.340
When you turn MATLAB
on for the first time,

00:00:15.340 --> 00:00:18.920
you'll be greeted with a
window that looks like this.

00:00:18.920 --> 00:00:20.980
The first thing we do
is close all the windows

00:00:20.980 --> 00:00:24.370
that we don't need initially--

00:00:24.370 --> 00:00:28.420
Current Directory, Workspace,
and Command History.

00:00:28.420 --> 00:00:30.740
We're left with
a command window.

00:00:30.740 --> 00:00:32.150
This is the command prompt.

00:00:32.150 --> 00:00:38.901
And this is the flashing cursor
where our input will be placed.

00:00:38.901 --> 00:00:40.650
The first thing we
want to do is try using

00:00:40.650 --> 00:00:42.630
MATLAB as a giant calculator.

00:00:42.630 --> 00:00:46.635
So for example, we can ask
MATLAB to calculate 1 plus 1.

00:00:46.635 --> 00:00:48.540
The answer is 2.

00:00:48.540 --> 00:00:51.350
So we type what
we want, 3 plus 4.

00:00:51.350 --> 00:00:55.800
Hit Enter and we
get the answer--

00:00:55.800 --> 00:01:04.503
3 times 4, 1 plus 2 times
3, 2 to the power of 4,

00:01:04.503 --> 00:01:09.050
5 to the power of 3, et cetera.

00:01:09.050 --> 00:01:12.920
We can also ask whether
two numbers are the same,

00:01:12.920 --> 00:01:15.000
or whether one is
greater than the other.

00:01:15.000 --> 00:01:18.760
So to see if 4 is greater
than 5, we type this.

00:01:18.760 --> 00:01:20.280
This will tell us
that the answer

00:01:20.280 --> 00:01:24.140
is no, 4 is not greater than 5.

00:01:24.140 --> 00:01:26.240
We can ask is 4 less than 5.

00:01:26.240 --> 00:01:28.629
The answer is yes,
4 is less than 5.

00:01:33.320 --> 00:01:36.470
Sometimes we need a function
and expression, not just

00:01:36.470 --> 00:01:37.740
an operator.

00:01:37.740 --> 00:01:39.357
So for example, to
find the remainder,

00:01:39.357 --> 00:01:40.690
we need to use the function rem.

00:01:40.690 --> 00:01:44.000
Here is the remainder of
15 when divided by 4--

00:01:44.000 --> 00:01:45.200
rem(15,4).

00:01:45.200 --> 00:01:48.170
The answer is 3.

00:01:48.170 --> 00:01:50.480
To find out more
about a function,

00:01:50.480 --> 00:01:52.430
we use the help command.

00:01:52.430 --> 00:01:57.350
help rem will tell us everything
we need to know about rem--

00:01:57.350 --> 00:01:59.080
the remainder after division.

00:01:59.080 --> 00:02:03.080
There's a bit of an
explanation, some conventions.

00:02:03.080 --> 00:02:04.220
And the "see also"--

00:02:04.220 --> 00:02:07.190
this is perhaps one of the most
important parts of the help

00:02:07.190 --> 00:02:09.860
file-- is to help you
find other commands that

00:02:09.860 --> 00:02:12.560
are useful and related
to this command.

00:02:12.560 --> 00:02:15.340
I encourage you to try to find
the difference between mod

00:02:15.340 --> 00:02:15.840
and rem.

00:02:18.930 --> 00:02:20.400
Other than calculating
expressions,

00:02:20.400 --> 00:02:22.950
we can also assign their
value into variables.

00:02:22.950 --> 00:02:26.270
So for example, if I want to
create a variable named x,

00:02:26.270 --> 00:02:29.200
I type x equals and then
the value that I want.

00:02:29.200 --> 00:02:35.130
So for example, x=1 will create
a variable called x and put 1

00:02:35.130 --> 00:02:37.480
as the value into it.

00:02:37.480 --> 00:02:40.410
I can also put the result of
an expression into a variable.

00:02:40.410 --> 00:02:41.160
Here's y=2*3.

00:02:43.847 --> 00:02:44.680
Of course, that's 6.

00:02:44.680 --> 00:02:47.825
So it calculates the 6 and
puts 2 times 3 into it.

00:02:47.825 --> 00:02:51.080
So now y is 6 and x is 1.

00:02:51.080 --> 00:02:54.675
And I can use x and y if
they are numbers. x+y,

00:02:54.675 --> 00:02:56.610
the answer is 7.

00:02:56.610 --> 00:02:58.750
If I try to use a variable
that doesn't yet exist,

00:02:58.750 --> 00:03:02.880
for example z+y,
I get an error--

00:03:02.880 --> 00:03:05.660
undefined function or variables.

00:03:05.660 --> 00:03:08.450
Sometimes, you're
looking for a command

00:03:08.450 --> 00:03:09.810
but you don't know the command.

00:03:09.810 --> 00:03:11.601
You know that you're
looking for something,

00:03:11.601 --> 00:03:13.700
bu you just don't know
what it's called in MATLAB.

00:03:13.700 --> 00:03:16.425
In that case, you
should be using lookfor

00:03:16.425 --> 00:03:18.050
to find the command
you're looking for.

00:03:18.050 --> 00:03:22.790
So for example, lookfor
square will give you

00:03:22.790 --> 00:03:27.350
all the commands that have the
word square in their help file.

00:03:27.350 --> 00:03:31.346
Of course, I was looking for,
in this case, square root.

00:03:31.346 --> 00:03:31.970
And here it is.

00:03:31.970 --> 00:03:33.800
I can either click
on this and that

00:03:33.800 --> 00:03:37.580
will give me the help
file of the square root.

00:03:37.580 --> 00:03:41.630
Or I can just type help sqrt.

00:03:41.630 --> 00:03:44.850
And that will give the same.

00:03:44.850 --> 00:03:47.950
Square root finds the
square of the elements.

00:03:47.950 --> 00:03:51.960
So if I do sqrt(4), I get 2.

00:03:51.960 --> 00:03:54.120
Notice that I get
complex results

00:03:54.120 --> 00:03:57.030
if I get a non-positive input.

00:03:57.030 --> 00:04:05.400
So sqrt(-3) is the square root
of three times i, which is,

00:04:05.400 --> 00:04:06.720
of course, very nice to know.

00:04:06.720 --> 00:04:15.240
So 2i^2 should give me -4, and
indeed, 2i squared gives me -4.

00:04:15.240 --> 00:04:17.399
On the command line,
there is a useful trick

00:04:17.399 --> 00:04:19.300
involving the "up" button.

00:04:19.300 --> 00:04:21.690
Sometimes, you're
looking for something

00:04:21.690 --> 00:04:27.740
or you're typing something and
you make a typo, like this.

00:04:27.740 --> 00:04:29.250
I've swapped the
L and the E. Now

00:04:29.250 --> 00:04:30.900
I could retype
everything from scratch,

00:04:30.900 --> 00:04:33.030
or I can press the up key--

00:04:33.030 --> 00:04:40.820
up arrow-- and then the left
arrow until I can delete the E,

00:04:40.820 --> 00:04:42.440
and put it in the right place.

00:04:42.440 --> 00:04:47.720
Now when I hit Enter
and I get the help info.

00:04:47.720 --> 00:04:49.280
With the up arrow
and the down arrow,

00:04:49.280 --> 00:04:53.300
I can browse through the history
of all the commands that I did.

00:04:57.340 --> 00:05:00.200
If I have a longish
expression on the command line

00:05:00.200 --> 00:05:01.910
and I want to erase
the whole thing,

00:05:01.910 --> 00:05:04.040
I could start erasing it.

00:05:04.040 --> 00:05:11.360
Or I could press Control-C.

00:05:11.360 --> 00:05:13.040
That's probably
enough for today.

00:05:13.040 --> 00:05:18.030
To exit MATLAB, I either
look for the Quit MATLAB here

00:05:18.030 --> 00:05:23.580
or I can Apple-Q. Or from the
command line, I can type exit.