ECE311 Lab 0: Introduction to Matlab and Simulink

$30.00 $15.00

Download Details:

  • Name: Lab-0-mie71n.zip
  • Type: zip
  • Size: 404.22 KB

Description

Rate this product

1 Purpose
The purpose of this laboratory is to introduce you to MATLAB and Simulink as tools
for systems analysis. MATLAB is used primarily for numerical computations, while
Simulink provides a block diagram style representation of systems for analysis, simulation, and design. Please attach a single MATLAB script for each separate task. This
will not only simplify the work of the TAs in grading your work but will also help you
with building the habit of having scripts that automate your work. Furthermore save
these files so that they can be a useful reference for you in the future assignment and
courses. At the end of this file you will find a list with all the files you are required to
submit.
2 Matlab scripts
Read the MATLAB guide about how to create scripts here!
3 Linear Algebra
MATLAB is heavily based on linear algebra for doing its computations. In MATLAB,
matrices are entered row by row. The end of a row is indicated by a “;” or a carriage
return. For example:
1 A = [ 1 2 3 ; 4 5 6 ; 7 8 9 ]
produces the matrix:
A =


1 2 3
4 5 6
7 8 9

 (1)
Individual rows and columns can be extracted from A. For example, A(:, 1) and
A(1, 🙂 give the first column and row of A, respectively.
• How would you select the third column?
1
• Without trying on MATLAB, what do you think would be the result of A(2,2)?
Check the result on MATLAB.
3.1 Inverse of a Matrix and Solution of Linear Equations:
You will create and upload a unique script for all the calculations you operate
in this section 3.1. Name this file ex 3 1.m and add comments where required.
MATLAB provides a command to compute the inverse of an n x n matrix A, namely
inv(A). However, if the objective is to solve a linear system of equations Ax = b, there is
a better alternative than using x = inv(A) * b, namely A\b. The MATLAB operation
“\” is called mldivide or matrix left divide. This is illustrated in the following calculation.
1. Save the following code to a file and name it inv matrix.m. This is a script file
which you can run in MATLAB. You can use the lines below as an example
of how your submitted files should look like.
1 n = 5 0 0; % dimen si on o f the ma trix
2 Q = o r t h (randn (n , n) ) ;
3 d = l o g s p a c e (0 , −10 ,n) ;
4 A = Q∗ di a g (d) ∗Q’ ;
5 x = randn (n , 1 ) ; % the s o l u t i o n o f the equ a ti on , known t o
compare methods
6 b = A∗x ;
7 ti c , y = in v (A) ∗b ; t o c % s o l v e e q u a ti o n with in v and time
8 e r r i n v = norm(y−x ) % compute e r r o r
9 r e s i n v = norm(A∗y−b)
10 pause
11 ti c , z = A\b ; t o c % s o l v e e q u a ti o n with ml di vi d e and time
12 e r r ml = norm( z−x ) % compute e r r o r
13 res m ; = norm(A∗z−b)
Consult the MATLAB documentation to understand what each line
means. Then run inv matrix in MATLAB and note the outputs.
2. Similarly, there is an operation called mrdivide or matrix right divide, A/B, which
has the effect of solving the equation XA = B with respect to X. This method
can be used only when A and B have the same number of columns. In this exercise
we will compute the solution of the equation AX = B using all the three comands
inv, mldivide, and mrdivide, in a similar way as above.
Define A = randn(4, 4) and also X = randn(4, 4). Find the solution to the
equation AX = B using the commands inv, mldivide, and mrdivide. In order
to use mrdivide successfully, notice that solving AX = B with respect to X is the
same as XT AT = BT were AT denotes the transpose of A.
Be sure that the code used for the comparison of the three results is included in
the submitted file ex 3 1.m. For square matrices with low dimension, all three
calculations should give comparable accuracy.
2
3.2 Eigenvalues and Eigenvectors
You will create and upload a unique script for all the calculations you operate
in this section 3.2. Name this file ex 3 2.m and add comments where required.
Of great importance in the analysis of linear systems are the notions of eigenvalues and
eigenvectors. A scalar λ is an eigenvalue and a non-zero vector x is an eigenvector of a
linear transformation A if Ax = λx. If A is a matrix, then eigenvalues and eigenvectors
are only defined if A is square.
1. Let A be given by
A =


7 2 −3
4 6 −4
5 2 −1

 (2)
We will use this A for items 1 to 4 in this part. Use the MATLAB command
[V, D]=eig(A) to determine the eigenvalues and eigenvectors of A.
2. For manual computation of eigenvalues, usually you determine the characteristic
polynomial of A given by det(sI − A). Then you find the roots of the characteristic polynomial, i.e., find solutions of the characteristic equation det(sI − A) = 0.
Determine the characteristic polynomial of A using the command poly(A), and
determine the eigenvalues by applying the command roots to the resulting polynomial. Compare the answer with those from 1.
3. The eigenvalue-eigenvector equations can be written as one matrix equation
AV = V D, (3)
with D a diagonal matrix consisting of the eigenvalues of A. From the results
of point 1, determine norm(AV-VD). Show more significant digits in MATLAB
using the command format LONG before the expression you evaluate. From this
determine the exact values of the eigenvalues and eigenvectors (up to a scalar
multiple). Now verify that AV − V D = 0.
4. Recall that if the eigenvalues of A are distinct, the eigenvectors are linearly independent. In that case, the V matrix computed using eig is invertible and that
V
−1AV = D. This process is called diagonalizing A. Check that the matrix
in (2) can be diagonalized. Write one line in the comments justifying why it is
diagonalizable or not.
5. When A has repeated eigenvalues, it may not be possible to diagonalize A. Suppose
A has λ as a repeated eigenvalue, then det(λI − A) = 0 and the number of times
λ repeats as roots is called its algebraic multiplicity. Thus the following matrix
A =

1 1
0 1 
(4)
3
has 1 as its only eigenvalue with algebraic multiplicity 2. Determine by hand
calculation the eigenvector corresponding to the eigenvalue 1, and verify there is
only one independent eigenvector. Report this eigenvector in the comments. Try
using eig on this A. Does the resulting [V,D] satisfy AV = V D? Is V invertible?
Report the answers in the comments.
6. When A cannot be diagonalized, one can transform it to a form called the Jordan
(canonical) form. The MATLAB command is jordan. Find the Jordan form for
the matrix:
A =


0 4 3
0 20 16
0 −25 −20

 (5)
4 Ordinary Differential Equations and Transfer Functions
Control systems studied in this course are modelled by in the time domain by state
equation of the form:
x˙ = Ax + Bu
y = Cx + Du (6)
or by input/output models of the form
y
(n)
(t)+a1y
(n−1)(t)+a2y
(n−2)(t)+· · ·+any(t) = b0u
(m)
(t)+· · ·+bmu(t), with m ≤ n
(7)
In the state equation, the transfer matrix from u to y is given by:
G(s) = C(sI − A)
−1B + D
In this course, we focus on single-input single-output (SISO) systems, i.e., u and y are
scalar-valued. In this case, the transfer function G(s) is a scalar-valued proper rational
function. In the case of an input/output model, the transfer function G(s) is given by
G(s) = b0s
m + b1s
m−1 + · · · + bm
s
n + a1s
n−1 + · · · + an
(8)
MATLAB provides tools to analyze such linear systems. We illustrate some of these
tools using a second order system.
4.1 ODE and transfer functions
You will create and upload a unique script for all the calculations you operate
in this section 4.1. Name this file ex 4 1.m and add comments where required.
You will need to upload the required plots as well. Consider a second order system
described by the differential equation
y¨ + 2 ˙y + 4y = 4u
4
The transfer function from the input u to the output y for this system is given by
G(s) = 4
s
2 + 2s + 4
(9)
To model this as a state equation, let
x(t) = 
y(t)
y˙(t)

Then the state x satisfies the differential equation
x˙ =

0 1
−4 −2

x +

0
4

u (10)
y =

1 0
x (11)
MATLAB provides a data object which conveniently describes the state space system
(6). It is created by the command sys=ss(A,B,C,D).
1. Create the sys object corresponding to the second order system described by (10)-
(11).
2. One can study the response of the second order system due to particular inputs
such as a step (to get the step response), or the response due to nonzero initial
conditions with no input, or generally with nonzero initial conditions and inputs.
Here, determine the step response using the command [Y,T,X]=step(sys), where
sys is the object you created using the ss command. Use plot(T,X) to plot the
trajectories of both states. Save the plot1 as .png file and name it plot 4 2.png.
3. Use the command [Y, T, X]= initial (sys, x0) to determine the response to
initial condition x0 = 
0
1

and zero input. Again plot the state responses and
save it as a .png file and name it plot 4 3.png.
4. In general, the system may have both nonzero initial conditions as well as inputs.
For example, the commands:
1 t = 0 : 0 . 0 1 : 2 0 ;
2 u=s i n ( t ) ;
define an input vector u whose components are given by the values of sin(t) at
various times specified by the time vector t. Use [Y,t,X]=lsim(sys,u,t,x0) to
produce the response of the second order system due to the initial condition and
sinusoidal input defined above. Plot the state trajectories and save it as a .png
file and name it plot 4 4.png.
1 We highly discourage using the screenshot function of your OS. Use instead the MATLAB builtin commands for exporting plots as images. For example MATLAB documentation provides useful
information. A simple way to save your plot is (in the plot window): File -> Export as. In MATLAB
you can control virtually everything about your plots, try for example to add a meaningful legend,
it will be useful for the next labs. If you have problems contact your lab TAs.
5
5. If you are given a state space description, the command ss2tf generates the numerator and denominator polynomials of the transfer function associated with the
state space system. Use the ss2tf command to produce the transfer function for
the systems described by (10)-(11). The function returns two vectors with the coefficients of the numerator and denominator polynomials, properly ordered. Verify
that it is the same as (9).
5 Introduction to Simulink
Simulink is a MATLAB package which provides a graphical user interface to build systems using blocks from its library. Parameters that are set or changed in MATLAB are
passed along to Simulink, allowing them to complement each other.
To start Simulink, type simulink in the MATLAB command window, or click Simulink
in the menu. This will bring up the Simulink Start Page. Click on Blank Model to create a new blank model. Click on the Library Browser (the icon with 4 squares) to
bring up the Simulink Library Browser. In this window, you will find the blocks that
Simulink provides, as well as the toolboxes that have been installed. The blocks used
most frequently in this course can be found under Continuous, Math Operations, Sinks,
Sources. Scan through each of these categories to familiarize yourself with the blocks
provided.
5.1 Simulate and visualize data using Simulink
You will create and upload all the Simulink model files .slx you used for this
part of the lab. Save a different model for all the points listed below.
1. There are several different ways you can simulate a state space linear system in
Simulink. The simplest is to use the State-Space block, if you are mainly interested in the input/output behaviour. Bring in the state space block and use the
same (A, B, C, D) matrices as in (10)-(11). Connect to the input port a unit step.
Change the start time of the step to 0. Connect the output port to a scope. Click
the “simulate” button and examine the output on the scope. Save the scope plot
as a .png file and name it plot 5 1.png. Save your simulink model and name it
model 5 1.slx.
2. The disadvantage of the state space block is that you can only access the inputs
and outputs. One trick to overcome this limitation is to change the C matrix in the
state space block to the identity so that the outputs are the states. Now connect
the states to a gain matrix C. Note that by default, the gain matrix multiplies the
input element-wise. Under block parameters, you need to change “Multiplication”
to matrix multiplication. Finally connect the output to a scope and check that
the simulation gives the same output as in 1. Connect the states from the output
of the state space block to another scope, and check that one of the 2 signals
displayed is the same as the output y. Save the scope plot as a .png file and name
it plot 5 2.png. Save your simulink model and name it model 5 2.slx.
6
3. To represent the signals in a state space system in terms of more basic components,
start a new model and bring in the integrator block. The input to the integrator
block is ˙x = Ax + Bu and the output is x. The right hand side for ˙x requires you
to put in some gain matrices and a sum block so that the output of the sum block,
x˙, satisfies the state equation. The output of the integrator block, x, can now be
connected to the gain matrix C, whose output goes into a scope. With the input
a unit step, check that the output on the scope is again the same as those in 1.
Save the scope plot as a .png file and name it plot 5 3.png. Save your simulink
model and name it model 5 3.slx.
4. To completely expose the system graphically, you will build a Simulink model using
individual states as the signals. In a new Simulink model, bring in 2 integrator
blocks. For the system described by (10)-(11), the input to the first integrator
block should be ˙x2 . Its output is x2 = ˙x1, which is the input to the second
integrator block. Using scalar gains, complete the Simulink model so that the step
response shown in a scope is again the same as that at point 1. Save the scope plot
as a .png file and name it plot 5 4.png. Save your simulink model and name it
model 5 4.slx.
6 Submission
Be sure to submit a unique zip file with all the files listed below:
• ex 3 1.m
• ex 3 2.m
• ex 4 1.m, plot 4 2.png, plot 4 3.png and plot 4 4.png
• ex 5 1.slx and plot 5 1.png
• ex 5 2.slx and plot 5 2.png
• ex 5 3.slx and plot 5 3.png
• ex 5 4.slx and plot 5 4.png