Setting up a MathWorks account and installing MATLAB
To participate in the Labs portion of this course, you need to set-up a MathWorks account. (Since Michigan State University has an institutional license, this should be at absolutely no charge to you.) The process is fairly simple:
- First, visit the MSU-licensing page on MathWorks.
- You will click the "Sign-in to get Started" button, this button will raise the MSU log-in page, and you will need to log-in using your MSU NetID and Password. For more information concerning your NetID, please visit the MSU NetID webpage.
- This should return you to a screen asking you to associate the institutional license to a MathWorks account. If you have previously used MATLAB before, you can log-in using your existing MathWorks account. Otherwise please click on "Create a MathWorks Account" and follow the instructions there.
- Once your account is created, the MSU institutional license will be automatically applied to it. Make sure to remember / write down your MathWorks account information.
- With the MathWorks Account information, you should be able to log-in to MATLAB Online. You will be able to complete the Lab assignments with MATLAB Online.
- (Optional, but recommended) Once you have a MathWorks Account set up, you can download MATLAB according to the type of computer you have (Windows, Mac, or Linux). A MATLAB installation on recent (bought in the past 3 years) laptop typically runs faster than the online version; so if you have enough disk space we do recommend the installation.
- When the download completes, you can begin the installation process following the steps on MathWorks' webpage.
Important information: in Step 8 of MathWorks' installation guide, you are asked to "Specify Products to Install". For this class, you only need to install the base MATLAB product which will take approximate 1 GB of harddisk space. You can feel free to de-select all the other Toolboxes and Toolkits (the full installation can take more than 10 GB).
Important information: since you have already created a MathWorks account, you can use that to log-in in Step 5 of MathWorks' installation guide; in Step 7 you will want to select the MSU institutional license which should be listed in the dialog box.
MATLAB Cheat Sheet
Basic Syntax
Vectorized Arithmetic
To allow MATLAB to efficiently compute values taking advantage of vectorization, as a rule of thumb for this course we will use the period ('.') notation with the multiplication, division, and exponentiation operators.
- Addition:
1+2
(outputs3
) - Subtraction:
3-2
(outputs1
) - Multiplication:
2 .* 3
(outputs6
) - Division:
6 ./ 2
(outputs3
) - Exponentiation
2.^3
(outputs8
)
You must explicitly include the multiplication symbol between all variables. So you cannot write 2x
, and must write 2 .* x
instead.
Function definitions
To define a inline function, you need to specify the independent variables using the @
notation. To define the function $f(x) = x^3 - 2 x^2 - 3x - 5$ one would type
f = @(x) x.^3 - 2 .* x.^2 - 3 .* x - 5
After defining the function you can evaluate it: f(0)
would output the value -5
.
Built-in functions
Some mathematical functions and constants are available in MATLAB.
- Trig functions:
sin
,cos
,tan
,csc
,sec
, andcot
take their usual meanings, and take radians (not degrees) as their arguments. Sotan(pi.*/4)
will output1
. - The mathematical constant $\pi$ is available as
pi
. - Inverse trig functions:
asin
for inverse sine,acos
for inverse cosine,atan
for inverse tan. (If you are not familiar with with the inverse trig functions, we will learn about it in Section 6.6 of the course.) (Caveat: MATLAB also defines the inverse secant, cosecant, and cotangent functionsasec
,acsc
, andacot
; the range of these functions as defined by MATLAB is different from what you learn in our course. So use those functions with care.) - Logarithms:
log
for natural base logarithm, which in our class we denote using $\ln$; andlog10
for the base-10 logarithm which in our class we write as $\log$. (If you are not familiar with $\ln$ and $\log$, we will discuss them in Sections 6.2 and 6.4 of our class.) - The mathematical constant $e$ is not defined explicitly as a number, but you can access it through the natural exponential function
exp
. What in our class we will write as $e^x$ you can write in MATLAB asexp(x)
. In particular,exp(1)
gives the value of the constant $e$.
Plotting commands
Plotting a function
If you are given a function that has been defined in MATLAB, for example f = @(x) x.^2 + 1
, you can plot it using the fplot
command: fplot(f, [-3,4])
would plot the function f
over the range $-3 \leq x \leq 4$.
If you want to superimpose two plots on the same set of axis, you can issue hold on
(which tells MATLAB to put subsequent graphs on the same set of axes) and hold off
(which tells MATLAB to put subsequent graphs on a new set of axes). So
fplot(f, [-3,4])
hold on
fplot(g, [-3,4])
hold off
fplot(h, [-4.5])
will produce two graphs. The first showing the plots of the functions f
and g
on the same set of axes, and the second showing only the plot of h
.
Plotting data series
To plot a list of X-values against a list of Y-values, you can use the plot
command.
xvalues = [0 1 2 3 4 5 6 7]
yvalues = [1 5 4 2 3 6 9 0]
plot(xvalues, yvalues)