0% found this document useful (0 votes)
30 views28 pages

NoteSet 1 v1

Uploaded by

lianchu517
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views28 pages

NoteSet 1 v1

Uploaded by

lianchu517
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Dr.

Brett Freidkes University of South Florida

Note Set (NS) #1: Programming Concepts


Introduction to MATLAB and Coding Fundamentals

Contents (You can click these!)


Section 0: Introduction.................................................................................................................................. 1
Section 1: Coding Fundamentals................................................................................................................... 2
1.1 Variables ...................................................................................................................................... 2
1.1.1 Variable Names and Syntax ......................................................................................................... 6
Section 2: MATLAB User Interface (UI)........................................................................................................ 10
2.1 Changing Colors ................................................................................................................................ 10
2.2 MATLAB’s Environment ..................................................................................................................... 12
2.3 Mathematical Operations ................................................................................................................. 21
Section 3: Multiple Choice .......................................................................................................................... 25

Section 0: Introduction

• Hello, world! Welcome to Programming Concepts.


o The goal of this course is to introduce you to the basics of coding and
how to implement/write code in MATLAB.
o MATLAB stands for “Matrix Laboratory”, as it was created for solving
linear algebra problems, i.e., math involving matrices (we will discuss
this later in the course).

• These Note Sets – which I will abbreviate as NS in future documents – will


feature three general sections (most of the time):
o Section 1: Concepts without software
▪ These will mostly be general topics that may be applied to most
other coding languages.

o Section 2: Concepts with software (MATLAB)


▪ We will introduce the software more and more with each NS,
with the goal of applying the concepts discussed in the previous
section and earlier Note Sets.

1
Dr. Brett Freidkes University of South Florida

o Section 3: Multiple Choice


▪ Examples will be scattered throughout the entire Note Set, but I
will include an additional section with some questions for
additional help.

• If you have little to no programming experience, this course at first may be


difficult as coding requires a different sense of logic compared to other
courses you’ve previously had prior.
o My goal is to try and improve your sense of logic to take on a
challenging problem and simplify it by coding.

Section 1: Coding Fundamentals

1.1 Variables

• As engineers, it is a daily occurrence that we will deal with numbers in some


fashion, and therefore, we will deal with variables.

• From previous math classes, you have learned that a variable is a letter that
represents a number.

𝑥 = 3; 𝑦=4

• Similarly, in MATLAB, we will assign numbers to variables. The general idea


is that we will assign an expression to a variable name.

o Figure 1 shows the general idea, where we:


▪ Type a variable name (which does not need to be just a single
letter)
▪ Include an equal sign to tell the software that you are assigning
something to the variable name.
▪ Finally, include what you would like that variable to be assigned
to.

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 = 𝑝𝑟𝑒𝑠𝑠𝑢𝑟𝑒

o In algebra, these are equivalent since x = y is the same as y = x.


However, in coding, the software does not do this. It will always
assign the right-side to the left side.
▪ And since 4 is a number, we cannot use it as a variable; thus, the
program will show an error.

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.

Example #1: Variable Assignment

• 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.

• The variable z is assigned to the sum of x and y.


o Since x and y are defined, then z will be assigned to 3+4, which means
that z is ultimately assigned to 7.

x = 3
y = 4
z = x+y

• What is happening here?


o The computer will calculate the right side and find its value.
o Then, that value is assigned to z.
o This is seen in Figure 3.

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

• The error that appears is self-explanatory and seen as:

Unrecognized function or variable 'y'.

• What about if we reassign a variable like we do in the code below?


o The software will read from top to bottom, so if we reassign a variable
name, its value will be the most recent one (i.e., lowest).

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

• The next point is extremely important!


o What if we define x based on a previous value of x?
o This is shown below.
o We start with assigning 3 to x.
o Then, the next line says to assign 𝑥 + 1 to 𝑥. Do not let the fact that
two x’s are used confuse you!
▪ If the left side was y, I don’t believe there would be confusion. It
would just mean that y would be 4, right?

o But for this, we are essentially overwriting the original x (equal to 3)


with a new expression (which happens to be equal to 4…the software
does not care what is on the right, it will just follow what you want).

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.

Example #2: Finding the Area of a Circle

• 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

1.1.1 Variable Names and Syntax

• There are some rules regarding naming the variable. Be cautious because
your code will not run if names are incorrectly defined.

o 1) Never begin a variable name with a number.

6
Dr. Brett Freidkes University of South Florida

▪ The software will get confused because it expects a number if it


sees it first but then it receives letters…not good.
▪ Below is a bad variable name example.

6𝑠𝑖𝑑𝑒𝑠_𝑐𝑢𝑏𝑒

o 2) Do not put spaces anywhere in the variable name.


▪ The software expects a new operation after a space (or multiple
spaces).
▪ Below is the same variable name as above, except without the
line (the “underscore”). This is double bad since it not only starts
with a number, but there is a space present.

6𝑠𝑖𝑑𝑒𝑠 𝑐𝑢𝑏𝑒

o 3) Do not name a variable using a predefined MATLAB command.


▪ MATLAB has predefined mathematical functions and tools that
can be leveraged to make coding easier.
▪ For example, you may use trigonometric functions in MATLAB,
like sin to calculate the sine of an angle in radians.
▪ MATLAB may be unhappy with your assigning a number to the
word sin.

𝑠𝑖𝑛 = 10

▪ If you try to run this code, it will work.


▪ However, if you want to use the sin predefined function later,
MATLAB is expecting you to refer to the variable you originally
defined…so it will get mad.
• Below shows an example of a code that will not work.
• Note the % sign is the beginning of a comment that
MATLAB will ignore. More on this later.

sin = 10 %naming a variable "sin"


x = sin(pi) %pre-defined trig operation

• 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.

▪ Additionally, you want to make it easier for you in the future,


since you may forget what you wrote years prior.

▪ 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 1) If you have a variable that will be used continuously in your code


(i.e., it covers a “large scope”), then the variable should have more
specific/meaningful names.
▪ Good names: airPressure, force, airTemp
▪ Bad names: p, F, T
• There are many variables in engineering that relate to
multiple things (is p referring to pressure? Density? The p-
value in statistics?)
8
Dr. Brett Freidkes University of South Florida

• In some cases, these aren’t the worst. If the code is


commented well and it’s clear what the code is
accomplishing, it may be easier to use just a letter.
o Example: g may be the gravitational constant. If you
are doing something where this is obvious, it is
probably cleaner to stick with g instead of writing
gravity as the variable name.

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 3) CamelCase is a type of text where the first letter of your variable is


lowercase and any subsequent words in the variable are capitalized for
readability purposes.
▪ Good names: pressureOutput, circleVolume, airTemp
▪ Bad names: Pressureoutput, circlevolume, AirTemp

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

o 5) Don’t use negating words, like “Not” or “No”. It may become


confusing when trying to understand what a variable means.
▪ Good names: isGood, isMax, error
▪ Bad names: isNotGood, isNotMax, noError
• What would be a better name for “isNotMax”?

• 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

Unrecognized function or variable 'Y'.

Section 2: MATLAB User Interface (UI)

• 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?

2.1 Changing Colors

• First, I highly recommend changing the default colors in MATLAB. Why?


o MATLAB’s default white may be very bright and may hurt your eyes
over time.
o It looks way cooler with different colors (obviously the most important
reason to change it).
• To change colors, there is a little gear that says “Preferences” located in the
top toolbar (seen boxed in below in Figure 5).

Figure 5: Preferences location in the top toolbar.

o After clicking “Preferences,” you should see several submenus on the


left side. Click “Colors”. See Figure 6 below.
▪ In the center area, uncheck “Use system colors.”
▪ I recommend following the colors I have set below. For example,
white text on a black background, green comments, red syntax
errors, etc.

10
Dr. Brett Freidkes University of South Florida

Figure 6: Colors submenu.

o Then, go to Programming Tools on the left (under Colors)...see Figure


7
▪ Uncheck “Autofix Highlight” on top right
▪ Change color of “Automatically Highlight” on bottom left to a
darker gray color
• This will highlight variables and I’ve found that a gray
color on black looks best

Figure 7: Color preferences.

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

2.2 MATLAB’s Environment

• 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

Figure 9: How to open the Editor Window.

• 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.

• Below shows a code written in the Editor Window.


o Note that a percentage sign % is used to create “comments.”
o Comments are bits of text that provide context on what your code is
doing (useful for your future self and others to read your work).
o MATLAB DOES NOT RECOGNIZE THESE LINES WHEN THE
CODE RUNS.
▪ This means that comments do not abide by the same syntax rules
as your typical codes (this is a good thing).
o Figure 10 shows a code to find the area of a rectangle.

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).

• How do we run this code now that it is typed in the Editor?

13
Dr. Brett Freidkes University of South Florida

o If we look to the Navigation Ribbon, we can click the “Editor” tab to


see the different commands associated with it.
▪ Note, this ribbon will only be present if the Editor Window is
open.

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).

Figure 11: How to Run a code typed in the Editor window.

• Another useful thing about the editor window:


o Imagine you are doing a homework assignment that has 3 problems.
o You may want to use 3 separate .m files so you can run them each
separately: that may become tedious and disorganized over time.

o Instead, split your code into sections.


o Sections are separate areas of code that may be run individually without
impacting each other.

o To create a section, you use two percentage signs, i.e., %%


o Figure 12 shows this below.
▪ Note the blue line that separates the sections. In some MATLAB
versions, sections may be highlighted for better viewing (they
got rid of this in more recent versions which is so frustrating, I
could complain about this forever).
▪ Click “Run Section” at the top to only run the code within the
section you are currently in.

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.

1) The Command Window shows the output of a script written in the


Editor Window.
o Figure 13 shows the output of the simple addition code we described
earlier.
▪ The top of the figure is the Editor Window, where the code is
written.
▪ The bottom of the figure is the Command Window, which will
output anything from the Editor Window.

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 This is where the semi-colon comes in. If we place a semi-colon at the


end of a line of code, it will hide that line from the Command Window.

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.

2) The Command Window can be used as a calculator.


o If you want to perform quick calculations, the Command Window may
be used.
o Simply type an expression into the Command Window and click enter.
MATLAB will find the value for you.
o Note that if you just write a calculation (without assigning a variable),
MATLAB will default its assignment to the variable ans.

3) You can access MATLAB help files on the Command Window.


• There are several functions in MATLAB that can be leveraged to make your
coding life easier.
o However, each of these functions has a certain set of rules that need to
be followed to be used accurately.
o If you ever want to learn the proper syntax or rules for how a pre-
defined MATLAB function works, you can type help in the Command
Window, followed by the function of interest.
o Figure 16 shows an example of this for the sin function.

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.

• Current Folder (aka Working Folder)


o This area tells you what MATLAB is looking at on the computer, or
referencing. For example:
▪ Imagine a case where you have recently collected data from an
experiment, and you want to analyze it in MATLAB.
▪ Perhaps all the data is located in a .txt file (i.e., a Notepad file on
a Windows computer).
▪ You would like to import the data from that .txt file into
MATLAB. Where should that file be located on your computer?
▪ It must be in the location that MATLAB is referencing;
otherwise, the software cannot find it and it won’t be able to
upload it.
o This also relates to where the code itself is located.
▪ If you try to run a code and your working folder location does
not match the code’s location, then MATLAB will ask if this is
okay.

19
Dr. Brett Freidkes University of South Florida

▪ Basically, MATLAB typically wants your working folder to


match the code’s location, so it will prompt you to change the
working folder if it doesn’t match the code’s directory.
• The prompt seen below in Figure 17 will pop up.

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.

o If you double click a variable in the Workspace, a new window will


appear that looks similar to Excel. It will display the variable that you
clicked.
▪ For variables that are composed of only a number, this is fairly
redundant. But as variables become more complex (matrices, for
example), then it is convenient to view them in this way to
troubleshoot or examine results.
o As described earlier, typing clc into the Command Window will clear
the Command Window, but not permanently remove the variables that
MATLAB has stored in its memory.
▪ In other words, clc does not delete the variables in the
Workspace.

20
Dr. Brett Freidkes University of South Florida

o To delete the variables in the Workspace, you can:


▪ Click the variable in the Workspace and click Delete on your
keyboard.
▪ Right click the specific variable directly in the Workspace and
click Delete.
▪ Type clear into the Command Window followed by the variable
you want to clear (for example, clear z).
▪ If you want to delete everything in the Workspace, you can
simply type clear into the Command Window.

o Note that these commands can be included in your script.


▪ This means that you may clear variables while your code is
running, which may be useful depending on how your code is
written.

2.3 Mathematical Operations


• Table 1 shows some common mathematical operations and the symbol
associated with using it in MATLAB.
Table 1: Common mathematical operations in MATLAB.

Operation MATLAB Syntax


Addition +
Subtraction -
Multiplication *
Division /
Power ^
Square Root sqrt()
Pi (3.14...) pi
Natural Logarithm (base-e) log(x)
Log base-10 log10(x)
Imaginary Number, √−1 i j
Exponential Function, ex exp(x)
Absolute value, |x| abs(x)
Floor (rounds down) floor(x)
Ceiling (rounds up) ceil(x)

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

o The answer y2 will be:

𝑦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

y1 = -2^2; %this will be -4


y2 = (-2)^2; %this will be 4

• The moral of the story is that parentheses are required if you want to perform
math operations properly!

Example #3

• What is the value of z in the code below?

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

Figure 19: Image of floor and ceiling commands.

• 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

Section 3: Multiple Choice

Exercise 3.1: Multiple Choice (Answers at end of document)

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

2) If I wanted to delete all variables that are currently stored in MATLAB, I


should type:
a. clc
b. clear
c. delete all
d. remove

3) Which symbol should be used to create a comment in MATLAB?


a. ;
b. @
c. #
d. %

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

Answers: Multiple Choice

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.

2) If I wanted to delete all variables that are currently stored in MATLAB, I


should type:
a. clc clears the Command Window; it does NOT remove variables
stored
b. clear is the way to remove variables
c. delete is a command in MATLAB, but it doesn’t remove variables
i. Note that clear all will remove all the variables, just like clear
does
d. remove is not a command in MATLAB

3) Which symbol should be used to create a comment in MATLAB?


a. ; suppresses lines from being shown in the Command Window
b. @ does something we haven’t seen yet related to functions
c. # is not anything that I’m aware of
d. % is the answer
i. Note that %% creates a section

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

The answer is 4 (choice a).

𝑦 𝑦 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.

Therefore, our answer is d: 4, 7, error.

28

You might also like