NoteSet 1 v1
NoteSet 1 v1
Section 0: Introduction
1
Dr. Brett Freidkes University of South Florida
1.1 Variables
• From previous math classes, you have learned that a variable is a letter that
represents a number.
𝑥 = 3; 𝑦=4
2
Dr. Brett Freidkes University of South Florida
Figure 1: General assignment command in coding, where the variable name is assigned to an expression (Note: this may be a
number or equation in terms of previously defined variables). The equal sign’s role is to assign the right side to the left side.
o Note: this does NOT work both ways. For example, the following
below is not equivalent to what we wrote in Figure 1.
4 = 𝑝𝑟𝑒𝑠𝑠𝑢𝑟𝑒
Figure 2: General idea of variable assignment. A variable name becomes equal to the expression on the right side.
• After you assign a variable, it is stored in the computer’s memory and may be
used later in the code.
• Below shows a code where we assign the variable x to the number 3 and the
variable y to the number 4.
3
Dr. Brett Freidkes University of South Florida
o Note that I will box in any code in these Note Sets with a gray box, so
you are familiar with where to look.
o If the gray box has a dashed outline in red, then that means that
something about the code is incorrect.
x = 3
y = 4
z = x+y
Figure 3: The right side of the equation is computed and then assigned to the left variable.
• If the variables x and y are not defined, then trying to run the code will fail.
Below shows an example of that (assume this is a new code and ignore the
one above).
o Since y is not defined at all, the software gets confused because it is
trying to assign z to the sum of x and y. If it doesn’t know y, then it
can’t finish executing the code.
4
Dr. Brett Freidkes University of South Florida
x = 3
z = x+y
o In the example below, the final instance of x will be the one that is
stored within the computer for future use, i.e., x = 10.
x = 3
x = -5
x = 17
x = 10
x = 3
x = x+1
5
Dr. Brett Freidkes University of South Florida
• To further comment on this, the right side is always computed first based
on what MATLAB currently knows.
o Figure 4 shows this concept.
Figure 4: The right side is computed first based on what MATLAB knows. Then, the left side becomes defined/redefined.
• Perhaps a goal for you is to find the area of a circle, which we should
remember as the following:
𝐴 = 𝜋𝑟 2
• How would we write a code where we assign the diameter first and then find
the area?
o First, we define the diameter variable
o Then, we assign the radius based off the diameter originally assigned,
i.e., we divide the diameter by 2.
o Lastly, we use the radius in the equation for area.
o We will discuss mathematical operations in more detail later on in this
Note Set.
diameter = 4
radius = diameter/2
area = pi*radius^2
• There are some rules regarding naming the variable. Be cautious because
your code will not run if names are incorrectly defined.
6
Dr. Brett Freidkes University of South Florida
6𝑠𝑖𝑑𝑒𝑠_𝑐𝑢𝑏𝑒
6𝑠𝑖𝑑𝑒𝑠 𝑐𝑢𝑏𝑒
𝑠𝑖𝑛 = 10
• The error message for the above code will be discussed in more detail when
we get to NS#4.
7
Dr. Brett Freidkes University of South Florida
o 4) No dots (periods)
▪ The dot is a very important tool in MATLAB for matrix algebra
and organizing variables into structures (future topics).
▪ Therefore, it is not allowed in variable names as the software will
become confused when seeing it in a place it’s not supposed to
be in.
▪ This also applies to nearly every symbol that is listed on the same
key as a number on your keyboard (@, #, $, &, etc.)
• The rules described above are so important as your code will not run at all
when you break them.
o Luckily, if you make these mistakes, MATLAB will tell you where/why
there is an error.
• In addition to these hard rules, there are also general suggestions for variable
naming.
o Why do we need suggestions?
▪ Well, if you work at a company in the future, it is best to write a
code that is easy to understand for other readers.
▪ Lastly, all codes will end up with some bugs in it. If your code is
easily understandable, then debugging does not become as
tedious!
o The following are some recommendations that will make your coding
style better and digestible for others.
o 2) If you are using a variable in a small area of the code (for example,
during loops which we will cover later), then we should use smaller,
disposable names. These are typically called “dummy variables.”
▪ Good names: ii, n, loop
▪ Bad names: loopCounter, iteratationVariable, tempVariable
o 4) Avoid long variable names, as they will get annoying to type, may
lead to typos, and may be confusing.
▪ The general idea is to make your variable name long enough to
be descriptive, but short enough to be memorable.
• Good names: averageStress, isLightOn
• Bad names:
averageStressInPartThatIsConnectedToShaft,
isTheLightOnInsideTheHouse
• Important note: variables are case-sensitive! Meaning that x and X are not
the same thing!
o Capital versus noncapital letters are different.
o Below shows an example of an incorrect code.
9
Dr. Brett Freidkes University of South Florida
x = 4
y = 3
z = x + Y %note the capital Y makes this code fail
• At this point, we will shift gears and begin introducing the user interface (UI) of
MATLAB, i.e., what are you able to do as a user and how do we get started?
10
Dr. Brett Freidkes University of South Florida
o Once you are happy, you can click “apply” and the MATLAB window
behind the Preferences window should change colors.
11
Dr. Brett Freidkes University of South Florida
• Now that we have changed our colors, let’s discuss what we see when we open
up MATLAB for the first time.
• Figure 8 shows the general layout of MATLAB. We will go over each region
in more detail now.
Figure 8: The MATLAB environment (from version 2023a). Note that the Editor Window may not be present upon first opening
MATLAB.
• Navigation Ribbon:
o This is located at the top of the window. As seen below in Figure 9,
there are several tabs that you can click.
o If you click a new tab, like “Editor” for instance, a whole new set of
options relating to the tab name will appear.
o We will most often be in the “Editor” and “Home” tabs.
• Editor:
o This is the location where you will type out codes. You can save these
codes for future use.
o To open the Editor Window, go to Home in the Navigation Ribbon and
click “New Script.” (a “script” is another word for code).
▪ This is shown below in Figure 9.
12
Dr. Brett Freidkes University of South Florida
• After clicking New Script, the Editor Window will appear. This is where you
will type the majority of your codes.
o Lines written in the Editor Window may be saved. Most file types have
a specific filename extension (music files may be .mp3 or word
documents are .doc for example). MATLAB code files will be saved as
.m files.
Figure 10: Code written in the Editor Window. Note that all text after a percentage sign is a comment that MATLAB does not
recognize. Note that the squiggles underneath the equal signs are warnings (more on this when we discuss the Command Window
next).
13
Dr. Brett Freidkes University of South Florida
o As seen in Figure 11, you can click the Run button to execute the code.
▪ If the file isn’t saved, then you will be prompted to save it. The
default directory ends up being the Current Folder location (more
on this in a few sections).
14
Dr. Brett Freidkes University of South Florida
Figure 12: Use %% to create sections! Very useful for separating various code parts. Note the squiggly warning signs are gone...I
wonder why...hmm. More on this shortly.
• Command Window:
o This is the main window in MATLAB that is used for many different
tasks.
15
Dr. Brett Freidkes University of South Florida
Figure 13: Top: Editor Window with a code written. Bottom: The Command Window output after the code runs.
o That’s all well and good, but isn’t it annoying to see every single line
of code written?
▪ For example, in Figure 13, we may not care about x and y
showing in the output: maybe we only care about z. How do we
suppress the things we don’t care about?
o Compare Figure 14 to Figure 13: with the semi-colon present at the end
of the x and y lines of code, they do not show up in the Command
Window!
▪ If there isn’t a semi-colon, MATLAB shows a warning (this is
the squiggly warning mentioned earlier in Figure 10!)
▪ In this case, the warning is that the line of code is not suppressed.
Try it for yourself.
16
Dr. Brett Freidkes University of South Florida
▪ Codes with warnings will still run, but seeing the little lines may
be annoying at times.
Figure 14: Using a semi-colon to suppress the x and y lines from showing up in the Command Window.
• You can also output things from the Editor without an equal sign. Figure 15
shows the case where we actually suppressed the equation z = x+y but instead
just typed the letter z at the end.
o MATLAB will see this and output the value of z to the Command
Window.
17
Dr. Brett Freidkes University of South Florida
Figure 15: Single variables may be output directly to the Command Window by not including a semi-colon.
18
Dr. Brett Freidkes University of South Florida
Figure 16: "help sin" typed into the Command Window yields the documentation for the pre-defined sin function.
4) Additional Notes
• It is important to note that the Command Window is not the location for typing
codes. The appropriate location for longer codes is in the Editor Window, as
described previously.
• If you ever want to clear the contents within the Command Window – meaning
that you don’t necessarily want to delete saved variables, but all you want to
do is clean up the window so it isn’t being overrun by text – you can type clc.
o clc stands for clear Command Window
o It will remove everything in the Command Window and present a blank
slate.
19
Dr. Brett Freidkes University of South Florida
Figure 17: Prompt to change the Working Folder directory to match the location of the code itself.
• Workspace
o The Workspace shows you all of the variables that MATLAB currently
recognizes and has stored in its memory.
o Figure 18 shows the code we typed earlier in the Editor window and the
Workspace after we ran the code.
▪ If we didn’t run the code, MATLAB would have no idea what x,
y, and z are.
Figure 18: The Workspace and the variables that MATLAB currently has stored for the script shown on the left in the Editor
window.
20
Dr. Brett Freidkes University of South Florida
21
Dr. Brett Freidkes University of South Florida
• Note that parentheses are your friend in MATLAB. For example, see the code
below where the two equations will produce different answers.
o The answer y1 will be:
1 1
𝑦1 = = = 0.25
2+2 4
𝑦2 = 1/2 + 2 = 2.5
a = 2;
y1 = 1/(a+2); %this will be 1/(2+2) = 1/4 = 0.25
y2 = 1/a+2; %this will be 1/2 + 2 = 2.5
• Similarly, this is true for negative signs when using exponents. The two
equations below will output different answers.
𝑦1 = −22 = −4
𝑦2 = (−2)2 = 4
• The moral of the story is that parentheses are required if you want to perform
math operations properly!
Example #3
x = 3;
y = 4;
z = -x^2 + (-y)^2
22
Dr. Brett Freidkes University of South Florida
• The first term does NOT have parentheses, so we square 3 and then include
the negative sign.
• The second term shows the negative sign getting squared, so it is now
positive!
𝑧 = −32 + (−4)2 = −9 + 16 = 7
Example #4
• You will have the concept of floor and ceil in your first homework.
• This trips up students sometimes...don’t let it trip you up! What you do is in
the name!
o If I give you a number and ask you to take the floor of it, it means to
round the number down! No matter what! Towards the floor.
𝑓𝑙𝑜𝑜𝑟(7.1) = 7
𝑓𝑙𝑜𝑜𝑟(7.5) = 7
𝑓𝑙𝑜𝑜𝑟(7.99999999) = 7
o Similarly, the ceiling command ceil rounds all numbers up! Towards
the ceiling.
𝑐𝑒𝑖𝑙(7.1) = 8
𝑐𝑒𝑖𝑙(7.5) = 8
𝑐𝑒𝑖𝑙(7.00000001) = 8
23
Dr. Brett Freidkes University of South Florida
• The only exception is when you take the floor/ceiling of whole numbers.
o If we have a whole number, like 4 for example, its floor and ceiling
are equal!
▪ We have an integer and therefore, rounding isn’t necessary.
𝑓𝑙𝑜𝑜𝑟(4) = 4
𝑐𝑒𝑖𝑙(4) = 4
• As soon as you add a little bit more, then the ceiling and floor outputs will
be different.
24
Dr. Brett Freidkes University of South Florida
1) In which area of the MATLAB layout would you typically write longer
codes?
a. Workspace
b. Command Window
c. Editor Window
d. Navigation Ribbon
4) Which variable names will MATLAB accept? (Note: There may be more
than one answer)
a. x
b. Xx
c. 1X
d. xX
e. xy
f. variable name
g. loop_number
25
Dr. Brett Freidkes University of South Florida
5) What will the code below output in the Command Window? Do not type into
MATLAB.
x = 3;
y = 6;
z = y/(x+3) + y/x+1
a) 4
b) 2.5
c) Nothing will appear in the Command Window
d) 3
6) What will the code below output in the Command Window? Note multiple
things will appear in the Command Window, so each answer choice will
show multiple items appearing in order.
y = 4
y = 6;
y = y + 1
Y
a) 4, 6, 7, 7
b) 4, 6, 7, error
c) 4, 5, error
d) 4, 7, error
e) 4, 6, 5, 5
26
Dr. Brett Freidkes University of South Florida
1) In which area of the MATLAB layout would you typically write longer
codes?
a. Codes aren’t written in the Workspace; this is where variables are
stored.
b. Command Window should be reserved for smaller codes, viewing the
Editor outputs, help definitions, etc.
c. Editor Window is where longer codes should be.
d. The navigation ribbon is for running codes, saving files, and more.
Code cannot be typed here.
4) Which variable names will MATLAB accept? (Note: There may be more
than one answer)
a. Good
b. Good
c. Bad (can’t start with a number)
d. Good
i. Bad form since it isn’t CamelCase, but it still works.
e. Good
f. Bad (no spaces)
27
Dr. Brett Freidkes University of South Florida
g. Good
5) What will the code below output in the Command Window? Do not type into
MATLAB.
x = 3;
y = 6;
z = y/(x+3) + y/x+1
𝑦 𝑦 6 6
+ +1= + +1=1+2+1=4
𝑥+3 𝑥 3+3 3
6) What will the code below output in the Command Window? Note multiple
things will appear in the Command Window, so each answer choice will
show multiple items appearing in order.
y = 4
y = 6;
y = y + 1
Y
We are looking for what the Command Window shows: let’s look line by line.
The first line assigns 4 to y. There is no semi-colon, so this will appear in the
Command Window.
The second line assigns 6 to y; there is no semi-colon, so it will not appear in the
Command Window; however, y is no longer 4. It is now assigned to 6.
The third line assigns y to y+1. Since y is currently 6, it’s new assignment will be
7. There is no semi-colon, so we will see it in the Command Window.
The fourth line is asking to output capital Y, so we will get an error since Y is not
defined.
28