Laboratory 1: MATLAB & SIMULINK - report due September 24 in

advertisement
EE 4314: Lab 1 – Systems Programming with MATLAB and
SIMULINK
Location: NH 250
Date and Time: Sept 3, 5:30 - 7:20 PM and Sept 5, 5:30 - 7:20 PM
Report Due: Sept 24th, 2013 in class.
Objective: To familiarize students with basic Matlab operations that are useful for
Control Systems course such as 2D plot, ODE solver and Simulink.
Assignment: After the lab, students will work on the problems at the end of the handout
and are required to turn in a written report to the instructor that includes their answers to
the problems along with the associated MATLAB code listings, graphs, and Simulink
block diagrams. Students are also required to follow the guidelines for lab reports posted
online at the course webpage whenever applicable.
1.1 Matlab Basics
The main elements of the interface are:
the command window
the workspace window (variable window)
the command history window
the file information window
-
the start button
1.2 Basic Commands
-
who - list current variables. who lists the variables in the current workspace.
whos - is a long form of WHO. It lists all the variables in the current
workspace, together with information about their size, bytes, class, etc.
help – display help text in command window.
clc - clears the command window and homes the cursor.
clf – clears current figure.
clear - clears variables and functions from memory.
clear all - removes all variables, globals, functions.
save – saves workspace variables to disk. Use “help save” for details.
load – loads workspace variables from disk.
plot – PLOT(X,Y) plots vector Y versus vector X. If X or Y is a matrix, then
the vector is plotted versus the rows or columns of the matrix, whichever line
up.
1.3 Matlab Variables
In general, the matrix is defined in the Matlab command interface by input from the
keyboard and assigned a freely chosen variable name.
>> x = 1.00
A 1x1 matrix will be assigned to the variable x.
>> y = [ 1 5 3 ]
A row vector of length 3 will be defined.
>> z = [ 3 1 2; 4 0 5];
A 2x3 matrix will be defined.
An element of the matrix can be accessed by using the index (), for example:
>> z(2,3)
ans =
5
: can be used as an index to refer to the whole row or column, for example:
>> z(:,1)
ans =
3
4
Example 1: Simple 2D Plot
PLOT(X,Y) plots vector Y versus vector X. If X or Y is a matrix, then the vector is
plotted versus the rows or columns of the matrix, whichever line up.
>> x = 0 : 1 : 10;
>> y = -1: 1 : 9;
>> plot(x,y)
(a vector of length 11 will be defined)
(another vector of length 11 will be defined)
As a result we will get
>>
>>
>>
>>
>>
grid on;
xlabel('Time (sec)');
ylabel('Distance (meter)');
legend('Distance from point A to B');
title('Distance Plot');
The result will be
List of useful plot commands include:
xlim - gets the x limits of the current axes.
ylim - gets the y limits of the current axes.
semilogx() - is the same as plot(), except a logarithmic (base 10) scale is used
for the X-axis.
semilogy() - is the same as plot(), except a logarithmic scale is used for the Yaxis.
plot3(x,y,z) – similarly to plot(), plot3() plots a line in 3-space through the
points whose coordinates are the elements of x, y and z.
Example 2: 3D Plot
>>
>>
>>
>>
x = -1 : 0.1 : 1;
y = -1 : 0.1 : 1;
Z = exp(-1*(x'*y));
mesh(x,y,Z)
As a result we will get
(a vector of length 21)
(Z is a 21x21 matrix)
>> contour(x,y,Z)
As a result we will get
Contour(Z) is a contour plot of matrix Z treating the values in Z as heights above a plane.
1.4 Vector Operations
There are 2 types of vector operations in Matlab
1) Element by element operations
.+ element by element addition operation
.- element by element subtraction operation
.* element by element multiplication operation
.^ element by element power operation
./ element by element division operation
2) Matrix operations
' matrix transpose
* matrix multiply
^ matrix power
eye(N) is the N-by-N identity matrix.
inv(X) is the inverse of the square matrix X.
det(X) is the determinant of the square matrix X.
trace(X) is the sum of the diagonal elements of X, which is also the sum of the
eigenvalues of X.
Example 3: Matrix Operations
>> X = eye(2)
X =
1
0
0
1
>> Y = [ 1 3 ; 2 1]
Y =
1
3
2
1
>> Z = [ 3 2; 1 4]
Z =
3
2
1
4
Element by element multiplication VS matrix multiplication
>> Y.*Z
ans =
3
2
>> Y*Z
ans =
6
7
6
4
14
8
Element by element power VS matrix power
>> Z^2
ans =
11
7
>> Z.^2
ans =
9
1
14
18
4
16
Inverse, transpose and multiplication example
>> inv(Z)
ans =
0.4000
-0.1000
>> inv(Z)*Y'
ans =
-0.2000
0.8000
-0.2000
0.3000
0.6000
0.1000
1.5 Complex Numbers
An imaginary can be defined using the i , j.
>> a = 2 + i
a =
2.0000 + 1.0000i
Useful commands for complex number operations include:
-
real(X) is the real part of X.
imag(X) is the imaginary part of X.
conj(X) is the complex conjugate of X.
abs(X) is the absolute value of the elements of X. When X is complex, abs(X)
is the complex modulus (magnitude) of the elements of X.
angle(X) returns the phase angles, in radians, of a matrix with complex
elements.
cart2pol(X,Y) transforms corresponding elements of data stored in Cartesian
coordinates X,Y to polar coordinates (angle TH and radius R).
pol2cart(TH,R) transforms corresponding elements of data stored in polar
coordinates (angle TH, radius R) to Cartesian coordinates X,Y.
Example 4: Imaginary Number Operations
>> real(a)
ans =
2
>> imag(a)
ans =
1
>> conj(a)
ans =
2.0000 - 1.0000i
>> abs(a)
ans =
2.2361
>> angle(a)
ans =
0.4636
>> [th,r] = cart2pol(real(a), imag(a))
th =
0.4636
r =
2.2361
>> [x, y] = pol2cart(th, r)
x =
2
y =
1
2.1 Linear Systems / Control Systems Toolbox
At the MATLAB command prompt, type “help control” to learn about the control
systems toolbox. Some useful commands include:
-
tf(num, den) - creates a continuous-time transfer function with numerator(s)
num and denominator(s) den.
ss(a,b,c,d) - creates the continuous-time state-space model
impulse() - calculates the unit impulse response of a linear system.
step() - calculates the unit step response of a linear system.
lsim() - simulates the time response of continuous or discrete linear systems to
arbitrary inputs.
residue(b,a) - finds the residues, poles, and direct term of a partial fraction
expansion of the ratio of two polynomials, b(s) and a(s).
-
ode23() - solves initial value problems for ordinary differential equations.
Dsolve () – solves differential equations symbolically.
Example 5: Time responses of a linear system
Create a system with transfer function Gs 
>> Num = [1 0];
>> Den = [1 2 10];

>> sys = tf(Num,Den)
Transfer function:
s
-------------s^2 + 2 s + 10
Simulate an impulse response of the Gs
>> impulse(sys)
The result is:

Simulate a step response of the same system
>> step(sys)
The result is
s
s  2s  10
2
Now, lets simulate the time response of the above system using different input function.
>> t = 0 : 0.1 : 10;
>> u = sin(2.*t);
>> lsim(sys,u,t)
Example 6: Partial Fraction Expansion
Suppose that we want to write Gs 
Gs  
s
in pole-residue form of
s  2s  10
2
rn
r1
r2


 k s  . We can use residue() command to solve the
s  p1 s  p2
s  pn
problem.

>> Num = [1 0];
>> Den = [1 2 10];
>> [r,p,k] = residue(Num, Den)
r =
0.5000 + 0.1667i
0.5000 - 0.1667i
p =
-1.0000 + 3.0000i
-1.0000 - 3.0000i
k =
[]
It means that we can now rewrite G(s) as Gs 
0.5  0.1667i 0.5  0.1667i
.

s 1 3i
s 1 3i
Example 7: First Order ODE Solver
Given x  2x  0 , plot the time 
response of x(t) from t = 0 – 10 from x(0) = 5.
We will first open up m-file editor to create the function. First, click on the “New” button
and type the following function code.
function dx = myfunction(t,x)
dx = -2*x;
Then, save the function as myfunction.m in your current working directory.
>>
>>
>>
>>
>>
>>
[t,x] = ode23(@myfunction, [0 10], 5);
plot(t,x)
grid on
xlabel('time (seconde)');
ylabel('Amplitude');
title('Simple ODE Solver');
The result is:
2.2 Simulink
Simulink® is an environment for multidomain simulation and Model-Based Design for
dynamic and embedded systems. Simulink programming consists of manipulating or
connecting block diagrams in a graphical user interface environment, and therefore it is
more intuitive and descriptive than the conventional script-based environment of
MATLAB. Simulink can be used to simulate very complex dynamical systems and
evaluate the performance of controllers for such systems.
Go to MATLAB Help and find simulink examples in the Demos tab. Take some time go
over general demos.
Example 8: Transfer Function Simulation using Simulink
Create a simulink model of Gs 
s
and then simulate and plot a step response
s  2s  10
2
of this transfer function.
Step1: Launch Simulink by typing simulink in the command window. Then, open a
 model.
new blank simulink
Step2: Go to >>Simulink>>Continuous, then drag the transfer function block from the
panel and drop it onto the blank model window.
Step3: Double-click the transfer function block to edit the parameters. Enter numerator
coefficient and denominator coefficient as show in the picture below.
Step4: Go to >>Simulink>>Sources, then drag the “Step” block onto the model window.
Step5: Go to >>Simulink>>Sinks, then drag the “Scope” block onto the model window.
Step6: Connect the terminals as show in the picture below.
Step6: Run the simulation, by pressing the play button or go to >>Simulation>>Start.
Step7: Double-click the “Scope” block to view the simulation result (Press the autoscale
button on the scope window if needed).
Step8: Press the auto-scale button (binocular) on the scope window.
The picture below is the step response of the system.
Example 9: Simulating an ODE Model using Simulink
We will create a Simulink model for
x  1.4x  x  f t 
using continuous blocks. And we will use a user defined function block to handle
arbitrary f(t). In this example, we will also demonstrate how to send Simulink variables to
workspace.
Step1: Open a new blank simulink model.
Step2: Go to >>Simulink>>Continuous, then drag the 2 integrator blocks from the panel
and drop it onto the blank model window.
Step3: Go to >>Simulink>>Sources, then a clock block onto the model window.
Step4: Go to >>Simulink>>User-Defined Functions, then a “Embedded MATLAB
Function” block onto the model window.
Step5: Go to >>Simulink>>Math Operations, then the “Sum” block onto the model
window.
Step6: Go to >>Simulink>>Sinks, then drag the “To Workspace” block onto the model
window. Double click the block and select “Array” under Save Format.
Step7: Edit the “Sum” block so that it takes 3 inputs with signs +, −, −. Go to Math
Operations, then drag the “Gain” block to model window. Edit its gain to 1.4.
Step8: Connect the terminals as show in the picture below.
Step9: We can now set the Embedded MATLAB Function block to reflect any function.
In this example, we use f(t) = sin(t).
Step10: We can set the initial value of the integrators by double clicking the integrator
block. In this example, we leave them at 0.
Step11: Run the simulation, by pressing the play button or go to >>Simulation>>Start.
Now go to Matlab command window, notice 2 new variables in the workspace “simout”
and “tout”. We can use these variables as normal Matlab variables. Plot of “simout”
versus “tout” is shown in the picture below.
Lab Report
Submit your solutions to the following problems. Include –if any– your formulation,
MATLAB code, Simulink block diagram, and resulting graphs for each problem. Also
follow the guidelines for lab reports posted online at the course webpage whenever
applicable.
Problem 1 (25 pts). Let
𝑥̈ + 2𝜁𝜔𝑛 𝑥̇ + +𝜔𝑛2 𝑥 = 𝑓
(1)
be a second order dynamical system, in which f = f(t) is the input with unit (m/s2), x =
x(t) is the output in (m), ζ = 3.5 is the damping ratio (dimensionless) and ωn = 10 (rad/s)
is the natural frequency.
a) (15 pts) Use ode23() to simulate the time response of the system from t = 0 to t = 10 s
with zero input (i.e. f(t) = 0), and initial conditions of (0.45 m, 0.21 m/s) for x and the
first time derivative of x, respectively. Write the program using m-file(s) that output a
plot of the change of x and 𝑥̇ with time. Plot both variables on the same graph with proper
labeling and annotation such that x and 𝑥̇ are clearly distinguishable.
(Hint for ode23: You need to use the state space representation of the system to create a
function that returns the derivatives of x. For instance, let 𝑥1 = 𝑥, 𝑥2 = 𝑥̇ . Hence, your
function implementation will return dx such that dx=A*x+B*f. Note that x=[x1; x2] is
a vector of two elements so A is 2×2 and B is 2×1. Note also that ode23 requires a 2×1
vector of initial values.)
b) (10 pts) Use ode23() to simulate the time response of the system from t = 0 to t = 10 s
1, 0 ≤ 𝑡 ≤ 4 𝑠
where 𝑥(0) = 0, 𝑥̇ (0) = 0 and 𝑓(𝑡) = {
. Write the program using m0, 4 < 𝑡 ≤ 10 𝑠
file(s) that output a plot of the change of x and 𝑥̇ with time. Plot both variables on the
same graph with proper labeling and annotation such that x and 𝑥̇ are clearly
distinguishable.
Problem 2 (25 pts). For the system in equation (1) with ζ = 3.5, ωn = 10 rad/s, and zero
initial conditions,
a) (8 pts) Transform the differential equation to frequency domain representation using
Laplace transform. Show the steps of your formulation and indicate what Laplace
transform properties that you use. (You do not need to use MATLAB for this part.)
b) (5 pts) Use the tf command to create the system transfer function. Show your
commands/codes and the transfer function you get.
c) (6 pts) Plot the step response of the system obtained in part (b) for 0 ≤ 𝑡 ≤ 10 𝑠.
Indicate which variable the plot shows: 𝑥, 𝑥̇ , or 𝑥̈ .
d) (6 pts) Using lsim command, plot the time response of the system where f is a
sinusoidal wave of amplitude 1.35 m/s2 and 0.75 Hz frequency for 0 ≤ 𝑡 ≤ 10 𝑠.
Problem 3 (25 pts). For the system in equation (1) with ζ = 3.5 and ωn = 10 rad/s,
a) (8 pts) Create a Simulink model of the 2nd order differential equation where f(t) is a
function generator. Show the block diagram and indicate which signal line is 𝑥, 𝑥̇ , and 𝑥̈ .
b) (7 pts) Simulate the time response of the system from t = 0 to t = 5 s where 𝑥(0) =
0.4, 𝑥̇ (0) = 0.2 with f(t) as a sinusoidal function of 1.2 m/s2 amplitude and 0.75 Hz
frequency. Is the result the same as the result of Problem 2-d, why?
c) (10 pts) Pick two different amplitudes and frequencies for f(t) such as f1(t) and f2(t).
Simulate the time response of the system for both f1 and f2 from t = 0 to t = 5 s with zero
initial conditions. Show by using the simulation results that the response of the system to
f1(t)+f2(t) is the same as the sum of the individual responses to f1(t) and f2(t). What is this
property of the system called?
Problem 4 (25 pts). In Simulink,
a) (7 pts) Using only two Step sources and a summer, create a single pulse signal that
rises to an amplitude of 10 at t = .75 and falls back to zero at t = 3.5 as shown in part (a)
of the following picture. Show the block diagram of your design and the plot of the pulse
signal with clearly visible axes values.
b) (10 pts) Using only Ramp sources and a summer, create the signal shown in part (b) of
the following picture. Show the block diagram of your design and the plot of the signal
with clearly visible axes values.
1.2
1
1.0
10
0
t = .75 t = 3.5
1.75
(a)
0
t = 1 t = 1.1 t = 1.3 t = 1.4
(b)
c) (8 pts) Input the signal in part (b) to the system in Problem 3-a by replacing the
function generator with the summed ramp sources. Apply zero initial conditions. Show
the response of the system for 0 ≤ 𝑡 ≤ 5 𝑠 and compare it with the step response
obtained in Problem 2-c.
Download