You are on page 1of 4

TEE 451 - Control Systems

Winter 2021

Instructor: Michael McCourt Assignment: Lab 0


Intro to MATLAB

Lab 0 Assignment
While many of you have used MATLAB in previous courses, this introductory lab should help you review
what you know about MATLAB to prepare for this quarter. You are responsible for downloading and
installing MATLAB on your own computer. The instructions are to follow along and try out the lines of
code provided in this document. Make sure that you understand how each line works before moving on
to the next one. I encourage you to try some variations on each line to understand how you can adapt
this code for your own purposes on future labs and homework.
As you work through this, make sure to track your work with two files. The first is a single MATLAB
script (.m-file) with all lines of code that run in this lab. While it is only required that you include the
code shown here, you may include all lines of code you tested while working on this lab. The second file
is a Word document or PDF with the results of each line. You may copy and paste each line from the
MATLAB command window to this file to demonstrate that you ran the files and got the results shown.
You will submit both files on Canvas for the assignment “Lab 0”. Please do not submit these in a zip file,
just provide the two files. This will largely be graded as full credit or no credit.

1 MATLAB Overview
When you open MATLAB, look for the major sections of the window. You should see the Command
Window, the Workspace, and the Current Folder. You can enter commands one at a time into the
Command window. Try “2+2” or “x=5” to see how this works. You can clear all variables by typing
“clear” into the Command Window. You can clear all text from the Command Window by typing “clc”
into the command window. The Workspace shows the variables that have been defined since you opened
the program or the last time you cleared all variables. The Current Folder shows any files that are in the
default folder. You can create files and folders in the current folder as you need.
In the Current Folder pane, you can right click and create a new script. MATLAB scripts are .m files.
They cannot begin with a number and they cannot have spaces or some punctuation symbols in the file
names. If you double click this m-file, it will open in the Editor. In the Editor, you can type in multiple
lines and run them as a batch instead of line by line as in the Command Window. To run a script, you
can click the “Run” button which looks like a green play button. For scripts, comments can be created
by using the percent symbol (%).
While this document represents a basic introduction to MATLAB, there are much better resources
available online. I have included some of them as links at the end of this document.

2 Basic Math
You can do basic math operations in the command window or in a MATLAB script. This includes arith-
metic (addition, subtraction, multiplication, or division), exponents (using the ˆ symbol), and parentheses

page 1 of 4
Control Systems January 2, 2021

or brackets. You can store values into variables using the equal sign. Try some of the lines below to see
how this works. You can try some lines with and without the semicolon at the end to see the difference.

x = 3ˆ2
y = 2.87*5
z = x+y
x2= x+1;

3 Variables
Variables can be given names of arbitrary length but they must not start with a number and they must
not have spaces in them. You can compound multiple words in a variable name by using capital letters
or the underscore symbol. There are some special variables that have specific values such as pi or i. You
should avoid using i or j as variable names as these are used to denote the imaginary number, the square
root of -1.

temperature1 = 98.6
winRate = 50
time_to_impact = 0.01

4 Matrix Math
The name “MATLAB” is actually a contraction of Matrix Laboratory. In MATLAB, all variables are
matrices by default. This makes it easy to define arrays and matrices. The most straightforward way of
entering arrays or matrices is by using the square brackets. The lines below create two arrays a1 and a2
that are row vectors.

a1 = [1 2 3]
a2 = [4 5 6]
a1+a2
a1-a2

With arrays, the most common operations include addition, subtraction, and multiplication. There
are two types of array multiplication: the dot product and the element by element product. To calculate
a dot product, the dimensions of the arrays must be chosen appropriately and the asterisk (*) is used.
The element by element product can be done by inserting a period before the asterisk (.*). Note that the
semicolon below indicates a new line to create a vertical array, also known as a column vector.

a3 = [1 2 3]
a4 = [1 2 3]
a5 = [9; 8; 7]
a4*a5
a3.*a4

Matrices can be entered using the square brackets and the semicolon to specify a new line. Matrices
can be any dimension, but all elements must be entered. You cannot, for example, enter 5 elements on
the first row and 4 elements on the second row. Recall that matrix multiplication is not commutative so
the two multiplications below will give different results.

page 2 of 4
Control Systems January 2, 2021

M1 = [2 1; -3 2]
M2 = [4 -5; 1 2]
M1*M2
M2*M1

It is also possible to do element by element multiplication for matrices as with arrays using the “.*”
notation.

M1.*M2

Other important matrix operations include the transpose using the apostrophe (’) and the inverse (
ˆ(-1) ). These will be used regularly in this class.

M1’
M2ˆ(-1)

5 Function Calls
There are many built in functions in MATLAB that we will use this quarter. To start, you should famil-
iarize yourself with the trig functions (sin, cos, tan), logarithm functions (log, log2, log10), exponential
function (exp), matrix inverse (inv) and square root (sqrt). Try some of the lines below to get familiar
with these functions. Note that “pi” can be used to denote the value of π.

sin(pi/2)
cos(pi/2)
log(0)
log(1)
exp(1)
inv(M1)
sqrt(2)

You should be aware of the “help” command. If you are aware of a function but dont know how to
use it, you can always type “help” followed by the function to see an explanation of how to use it. Get
used to using this help as it is the quickest way to find out how to use a function. You should note that
most functions can be used in several ways once you become familiar with them. Also, the help command
can give similar functions that you may also be interested in.

help sin
help exp

6 Plotting
As engineers, it is important to visualize the results of our work. It is important to be able to plot data
and simulation results. The code below creates a time vector t, the vertical axis variable y, plots the
variables, and labels the figure and axes. Note that the line below defining the time vector t has three
values separated by colons. The first value 0 is the first value, the last value 15 is the final value, and the
middle value is the step size between points.

page 3 of 4
Control Systems January 2, 2021

t = 0:0.1:15;
y = sin(t);
plot(t,y)
title(’Sine Wave’)
xlabel(’Time (s)’)
ylabel(’Signal Amplitude’)

7 Programming Concepts
Most of what you have learned when it comes to controlling the flow of a program also applies in MATLAB.
This includes conditionals to control the flow of a program (if, elseif, else). This also includes both for loops
and while loops for repeating certain actions. The following segments of code show a sample conditional
and for loop. Note that the conditionals and loops are ended by the keyword “end”.

x = 1;

if (x>5)
y=0;
elseif(x>2)
y=1;
elseif(x>0)
y=2;
else
y=3;
end

disp(y)

for y = -10:2:10
disp(y)
end

8 Other Resources
This document is a very brief introduction to MATLAB. The links below are good references for learning
more about this program. As we go through the quarter, you will learn more about using MATLAB as a
tool for analyzing systems and designing feedback controllers.

University of Michigan - MATLAB Basics Tutorial


http://ctms.engin.umich.edu/CTMS/index.php?aux=Basics_Matlab

Kermit Sigmon, University of Florida, MATLAB Primer


http://www.math.toronto.edu/mpugh/primer.pdf

David Houcque, Northwestern University, Introduction to MATLAB for Engineering Students


https://www.mccormick.northwestern.edu/documents/students/undergraduate/introduction-
pdf

page 4 of 4

You might also like