# Lesson 1.4: MATLAB as a Calculator

[MUSIC] Okay, we know how to use the command
window, and the workspace, and the command history. Now let’s do a more
interesting calculation. Let’s calculate the number of
kilometers in a light year, which is the distance that a light
beam will travel in a year. The speed of light is equal to 300
thousand kilometers per second, which we can put into
to remind me that the units are kilometers per second. You can’t include units when you
assign a value to a variable. The value is just a number. Second, note that it’s okay to use the
underscore as part of a name in MATLAB. And third,
if you look at the workspace up here, you’ll see that speed_kps
is listed above x and y. That’s because variables are listed
in alphabetical order, not in the order that they’re created. Okay.
Back to the calculation. To compute a light year in
kilometers we multiply the speed by the number of seconds in a year. First let’s put the number of
seconds in a year into a variable. I want to call it, uh, year_sec. That’s equal to the number
of days in a year, which is 365——
we’ll forget about leap year—— times——and we use the asterisk for times—— the number of hours in a day,
which is 24, times the number of minutes in an hour,
which is 60, times the number of seconds in a minute,
which is also 60. And there’s the number
of seconds in a year. So, to calculate the light year,
let’s do this. light_year_km, to remind me
that this is kilometers, is equal to year_sec times speed_kps. And there you are. The answer, 9.4608 e + 12
might be a little mysterious. MATLAB has written the answer
using scientific notation. The number after the letter e is the power
of 10 that you multiply the part before the e by. So 9.4608 e plus 12 is the same
as 9.4608 times 1 trillion. Well that’s prove that by
entering it the long way. So let’s see here: 9.4608 times—— Let’s see, a trillion is a one with 12 zeroes, so one two three four five six
seven eight nine ten eleven twelve. There, sure enough, 9.4608e+12. Oh, and by the way, “ans” means answer. It’s the variable MATLAB creates to
hold values that are typed in or calculated without being
assigned to any other variable. Now we can see it over
here in the workspace. It’s first, because it starts with an “a”. Let’s do one more calculation
involving the speed of light. The distance from the earth to the sun is
149.6 times ten to the sixth kilometers. Let’s use our skill with scientific
notation to assign that, OK, to a variable. [CLICKS] So earth to sun in kilometers is 150 times 10 to the 6th power. So how many minutes does it take
light to get here from the sun? That’s what we want to calculate. First let’s calculate the number of
seconds by dividing the distance in kilometers,
by the speed in kilometers per second. Like this. [CLICKS]. There, looks like it takes 500 seconds. And then we convert that to minutes,
by dividing by 60. [CLICKS] 8.3333 minutes. Okay, we’ve got a lot of
variables in our workspace. If we need to stop working, and
close MATLAB, it’d be a shame to lose ’em. You know, like if it’s time to break for
lunch, or for dinner, or for breakfast, or whenever you do MATLAB. No problem. We can save them for
when we come back later. We save them in a file simply by
the giving the command Save, like this: [CLICKS] There. It’s all saved. And there’s a message telling
us where it was saved. What happened was MATLAB has
copied everything in the workspace into a file called MATLAB.mat, and
you can see it over here on the left. It just showed up. This file is called imaginatively enough,
a “mat-file”. When we restart MATLAB,
we can get all our variables back by copying them from that mat-file into
the workspace with a command called load. Let’s prove it. First let’s clear out
the workspace with a clear command. [CLICKS]. It’s all gone. And just to make sure that
we’re not pulling some kind of trick because the assignment
statements are in the command window, I’m going to clear that
with a clc command. I’ve used that once before. There.
It’s all clear. And if we try to check the value
of the last variable we created, we’ll get a red reprimand. Let’s do that. [CLICKS] Let’s make a new
variable called Minnie, by assigning a value to it. [CLICKS]. Now we have just Minnie in the workspace. But we can get our other variables back,
with the load command. [CLICKS] And there they all are. And note that Minnie is still there. Okay lets take a minute or two to talk
about MATLAB’s rules for variable names. First of all you’ve noticed that we can use
underscore. We talked about that in fact. And you can use uppercase.
Minnie’s a great example, and speaking of uppercase,
the uppercase can go anywhere. Lets make lets make mickey equal nine. And now I’m going to make a mickey
with a capital C in the middle, and I’ll make him equal to five. If you’ll look over here in the workspace, you’ll see these are two
separate variables. The fact that this is a capital C,
makes this variable different from the one with the name
that has only lowercase C there. You can also have digits in the name. Let’s try x45 equals sixty-seven. And, well, but
you can’t have them just anywhere. [CLICKS] There. You see red, so
there’s something wrong with that. And you can’t have other characters. I mean you can’t have any characters
other than letters. So, if we did this, notice that’s already red. Red’s never good. So we’ll try. Pound x. That’s no good. Dollar sign x equals 7. That doesn’t work. And you can’t start with an underscore. Already, there you go. You have to stick to the rules that
the characters in a variable name can only be letters, underscores, or digits, and
The length is controlled. You can’t have more than 63 characters. So if you’re planning on having a variable
that had 64 or more characters, I got bad news for you. You can’t do it, but
I have never come close to that. I, I . . . Maybe I’ve used 25 characters, but
I doubt it. Okay this desktop has
gotten very cluttered. Let’s clear the screen with clc.
We’ve used that before. By the way, clc stands for
“Clear command window.” The workspace is cluttered too. Let’s get rid of that. Now, much better. And you know you can
clear the history too. Just come down here and
hit Clear Command History. It says, “Are you sure?” There. Now everything’s nice and clear. And while we’re on the subject of clutter. Let’s give a command. [CLICKS] There I’m imagining
that I’m supposed to set the absolute value of zero in Centigrade,
which I’ve done. And the value shows up
here in the workspace. It also shows up here on the screen. MATLAB echoes the value that
was assigned to the variable. Well, a lot of the time we
don’t want to see that. We just want to assign it and
have nothing happen. Let’s do another assignment. What I’m doing is
setting absolute zero in Fahrenheit by using the formula. And, but this time I’ll put
a semi-colon on the end. When I hit return nothing’s echoed back. But over here in the work space
something actually happened. Just to emphasize that, let’s do one more. Sticking with the temperature theme, I’m using paper ignition,
Fahrenheit equals 451. And there. Again, the assignment took place, but there was not this echo over
here cluttering up the screen. There’s also another use for
the semicolon. It’ll allow you to put multiple commands. On one line. Let me show you that. So I write x equals 42, then a semicolon, and then I just keep on typing and
no enter. Now I’m going to hit the enter. Look over at the right. The command x equals 42 is executed. So is the command y equals 87. There’s no echo. We had these semicolons, but also,
we separated the first command from the second command with a semicolon
on the same line. You can put as many statements as you want
on a line, as long as you separate them with semicolons, or you can
substitute a comma for that purpose. :et’s watch this. I’m going to put
a comma here instead of a semicolon and leave this part the same. We were able to do two different
commands again, the same two commands. The only difference is
this time we got the echo. So the semicolon’s doing two things here. One it’s ending this command, and allowing
us to begin another one on the same line. And secondly,
it’s silencing MATLAB’s echo. Here, the a comma ends one command and allows us to begin a second
command on the same line. But it allows the echo. And, not only can you have
multiple commands on one line, you can have one command
on multiple lines. Let me show you what I’m talking about. Let’s use a really long
variable name here. For some reason, I just have these
long variable names in my mind. [CLICKS]. So we’ve gotten to this point and
I want to multiply that times other variable
called another_long_one. But there just doesn’t
seem to be room here. So what I’m going to do is hit three dots. I don’t know if you can see this. Let me magnify this a little bit.
The dots turn blue. They’re special. Those three dots,
three periods, three full stops, whatever you like to call them, allows me to continue this one
statement on the next line. So I’m going to hit enter, and
down here, the blinker is still blinking. Nothing has happened. No error. No echo. And I’m going to type
the rest of my command. [CLICKS] There. It’s as if we typed this whole thing, from here, not counting these three
dots, all the way to here, on one line. And you can continue as many lines as you
want if you keep using those three dots on each line except, of course, the last one.>>[MUSIC] [APPLAUSE]

## 25 thoughts to “Lesson 1.4: MATLAB as a Calculator”

1. KekilandF1nnB0mbr says:

very helpful. I'm enjoying this course.

2. algrmu says:

"By the way, CLC stands for Clear Command Window."

… eh?

3. Amith Parry says:

Why 45x = 56 is an error ?

4. Dongdong Qin says:

thanks！

5. Callistus Ogoi says:

Thank You!

6. Bassam AlKhaffaf says:

i can't find lesson 1.2 command lecture

7. Kazi Mehdi says:

sure he can type !! damn man

8. DrunkMaleProstitute says:

Thanks Professor.

9. BlueWater Islight says:

Good

10. Andebo Abesha Waza says:

Very helpful lecture. Thank you so much Prof.

11. pratipalsinh dodiya says:

Thank you Sir for this excellent videos

12. abhiram abhi says:

can u explain how to do loan calculator in matlab???

13. Moise Safari Mugisho says:

Hi I am an Engineering student from South Africa and I would like to purchase the book but I do not have the online payment facilities, Is there any other way to purchase the book that does not involve online payment ?

14. phazonruler3000 says:

This guy looks like Stephen King

15. n637 says:

@14.12 What does three dots do?
yet_another_long_variable = a_really_long_variable*… ?

16. Chumlan Kithan1 says:

thank you sir. you explain really well. I just ask you one question. are you in anyway related to Stephen kings.

very useful.Thank u a lot for this.

18. Arpit Jindal says:

19. Nhật Quang Nguyễn says:

20. jennifer boll says:

omg you are right he looks like Stephen King!

21. jennifer boll says:

Sir, thank you very much for your videos! They are helping me a lot : )

22. Kshitij Vengurlekar says:

Thanks 🙂

23. Ujas Modi says:

Can you help me finding lesson 1.3 ?

24. Berhanu Girmay says:

Sir, thank you very much for your helpful tutorial! They are really helping me a lot for space science.

25. Scott Jr says:

okey dockey