ee 4314 lab 1 handout control systems simulation with ... lab 1 handout fall 2012.pdf · ee 4314...

17
09/04/12 − Page 1 EE 4314 Lab 1 Handout Control Systems Simulation with MATLAB and SIMULINK Fall 2012 1. Lab Information This is a take-home lab assignment. There is no experiment for this lab. You will study the tutorial in the next section and do the examples to learn the basics of MATLAB and Simulink for control systems simulation. You will need use to MATLAB, Simulink, and the control systems toolbox which are available in some of the OIT managed computer labs of UTA. Also, you can use your own installation of MATLAB. After studying the examples, you will work on the problems in the Lab Report section on your own and are required to submit a report by uploading it via EE4314 Blackboard. Please check the information about the assignment policy in the Lab Report section.

Upload: others

Post on 19-Mar-2020

15 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: EE 4314 Lab 1 Handout Control Systems Simulation with ... Lab 1 Handout Fall 2012.pdf · EE 4314 Lab 1 Handout Control Systems Simulation with MATLAB and SIMULINK Fall 2012 1. Lab

09/04/12 − Page 1

EE 4314 Lab 1 Handout Control Systems Simulation with MATLAB and SIMULINK

Fall 2012

1. Lab Information

This is a take-home lab assignment. There is no experiment for this lab. You will study the tutorial

in the next section and do the examples to learn the basics of MATLAB and Simulink for control

systems simulation. You will need use to MATLAB, Simulink, and the control systems toolbox which

are available in some of the OIT managed computer labs of UTA. Also, you can use your own

installation of MATLAB.

After studying the examples, you will work on the problems in the Lab Report section on your own

and are required to submit a report by uploading it via EE4314 Blackboard. Please check the

information about the assignment policy in the Lab Report section.

Page 2: EE 4314 Lab 1 Handout Control Systems Simulation with ... Lab 1 Handout Fall 2012.pdf · EE 4314 Lab 1 Handout Control Systems Simulation with MATLAB and SIMULINK Fall 2012 1. Lab

09/04/12 − Page 2

2. MATLAB and Simulink Tutorial

2.1. MATLAB Basics

The main elements of the interface are:

- Command Window

- Current Directory/Folder

- Workspace Window

- Command History Window

- Start Button

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

Page 3: EE 4314 Lab 1 Handout Control Systems Simulation with ... Lab 1 Handout Fall 2012.pdf · EE 4314 Lab 1 Handout Control Systems Simulation with MATLAB and SIMULINK Fall 2012 1. Lab

09/04/12 − Page 3

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

If you do the following:

>> x = 0 : 1 : 10;

A vector called x of length 11 will be defined.

>> y = -1: 1 : 9;

Another vector called y of length 11 will be

defined.

>> plot(x,y)

As a result you will get the plot y versus x as

shown on the right.

Page 4: EE 4314 Lab 1 Handout Control Systems Simulation with ... Lab 1 Handout Fall 2012.pdf · EE 4314 Lab 1 Handout Control Systems Simulation with MATLAB and SIMULINK Fall 2012 1. Lab

09/04/12 − Page 4

To put labels on the axes:

>> grid on;

>> xlabel('Time (sec)');

>> ylabel('Distance (meter)');

>> legend('Distance from point

A to B');

>> title('Distance Plot');

The result will be as shown on the right.

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 Y-axis.

- 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;

A vector of length 21.

>> y = -1 : 0.1 : 1;

>> Z = exp(-1*(x'*y));

Z is a 21x21 matrix.

>> mesh(x,y,Z)

As a result you will get the plot on the right.

Page 5: EE 4314 Lab 1 Handout Control Systems Simulation with ... Lab 1 Handout Fall 2012.pdf · EE 4314 Lab 1 Handout Control Systems Simulation with MATLAB and SIMULINK Fall 2012 1. Lab

09/04/12 − Page 5

>> contour(x,y,Z)

As a result you will get the plot on the right.

Contour(Z) is a contour plot of matrix Z treating

the values in Z as heights above a plane.

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

Page 6: EE 4314 Lab 1 Handout Control Systems Simulation with ... Lab 1 Handout Fall 2012.pdf · EE 4314 Lab 1 Handout Control Systems Simulation with MATLAB and SIMULINK Fall 2012 1. Lab

09/04/12 − Page 6

>> 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 6

2 4

>> Y*Z

ans =

6 14

7 8

Element by element power VS matrix power

>> Z^2

ans =

11 14

7 18

>> Z.^2

ans =

9 4

1 16

Inverse, transpose and multiplication example

>> inv(Z)

ans =

0.4000 -0.2000

-0.1000 0.3000

>> inv(Z)*Y'

ans =

Page 7: EE 4314 Lab 1 Handout Control Systems Simulation with ... Lab 1 Handout Fall 2012.pdf · EE 4314 Lab 1 Handout Control Systems Simulation with MATLAB and SIMULINK Fall 2012 1. Lab

09/04/12 − Page 7

-0.2000 0.6000

0.8000 0.1000

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

Page 8: EE 4314 Lab 1 Handout Control Systems Simulation with ... Lab 1 Handout Fall 2012.pdf · EE 4314 Lab 1 Handout Control Systems Simulation with MATLAB and SIMULINK Fall 2012 1. Lab

09/04/12 − Page 8

th =

0.4636

r =

2.2361

>> [x, y] = pol2cart(th, r)

x =

2

y =

1

2.6. 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 s

s2 2s10

>> Num = [1 0];

>> Den = [1 2 10];

>> sys = tf(Num,Den)

Transfer function:

s

--------------

s^2 + 2 s + 10

Page 9: EE 4314 Lab 1 Handout Control Systems Simulation with ... Lab 1 Handout Fall 2012.pdf · EE 4314 Lab 1 Handout Control Systems Simulation with MATLAB and SIMULINK Fall 2012 1. Lab

09/04/12 − Page 9

Simulate the impulse response of

G s

>> impulse(sys)

The result is shown on the right.

Simulate a step response of the same system

>> step(sys)

The result is shown on the right.

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)

The result is shown on the right.

Page 10: EE 4314 Lab 1 Handout Control Systems Simulation with ... Lab 1 Handout Fall 2012.pdf · EE 4314 Lab 1 Handout Control Systems Simulation with MATLAB and SIMULINK Fall 2012 1. Lab

09/04/12 − Page 10

Example 6: Partial Fraction Expansion

Suppose that you want to write

G s s

s2 2s10 in pole-residue form of

skps

r

ps

r

ps

rsG

n

n

2

2

1

1 . You can use residue() command to solve the 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 you can now rewrite G(s) as

G s 0.5 0.1667i

s1 3i0.50.1667i

s1 3i.

Example 7: First Order ODE Solver

Given 02 xx , plot the time response of x(t) from t = 0 – 10 for x(0) = 5. You will first open up

m-file editor to create the function. First, click on the “New” button and type the following function

code. Then, save the function as “myfunction.m” in your current working directory.

function dx = myfunction(t,x)

dx = -2*x;

Note that the name of the m-file has to be the same as the name that you use inside the code after

“function” statement. Also, when you call that function, it should be in the MATLAB current directory

(unless you add its directory path to MATLAB path from MATLAB>>File>>Set Path).

>> [t,x] = ode23(@myfunction, [0 10], 5);

>> plot(t,x)

>> grid on

>> xlabel('time (seconde)');

>> ylabel('Amplitude');

>> title('Simple ODE Solver');

Page 11: EE 4314 Lab 1 Handout Control Systems Simulation with ... Lab 1 Handout Fall 2012.pdf · EE 4314 Lab 1 Handout Control Systems Simulation with MATLAB and SIMULINK Fall 2012 1. Lab

09/04/12 − Page 11

The result is:

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

s2 2s10 and then simulate and plot a step response of this

transfer function.

Step1: Launch Simulink by typing simulink in

the command window. Then, open a new blank

simulink model.

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 on the

right.

Page 12: EE 4314 Lab 1 Handout Control Systems Simulation with ... Lab 1 Handout Fall 2012.pdf · EE 4314 Lab 1 Handout Control Systems Simulation with MATLAB and SIMULINK Fall 2012 1. Lab

09/04/12 − Page 12

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 on the right.

Step7: Run the simulation, by pressing the play

button or go to >>Simulation>>Start.

Step8: Double-click the “Scope” block to view the

simulation result.

Step9: Press the auto-scale button (binocular) on

the scope window and get a nicely scaled plot as

shown on the right.

Step10: If you want to save the data shown on the

scope, you can click on the parameters button of the

scope window and setup the scope such that it saves

the plot data in the workspace. In the History tab, you

should check “Save data to workspace” option and

give it a variable name and select the “Array” format

as shown in the picture on the right. After that, all the

plots on the scope get saved when the simulation

stops. The first column of the output matrix provides

the time data and the other columns provide the

corresponding signal data. Then, you can plot the

signals with respect to time using the plot command.

For instance, the following command plots the second

column of “output” (signal data) versus the first

column (time data).

>> plot(output(:,1),output(:,2))

Step11: You can save the data in the workspace to a file by selecting all the workspace variables of

interest, right clicking on them, and choosing “Save As…” This saves them together as a .mat file

which is the file format that MATLAB uses for matrices. You can later import this data into the

workspace using the “Import data” button on the Workspace window or the “load” command. Note that

when you close MATLAB, you loose the data in the workspace.

Page 13: EE 4314 Lab 1 Handout Control Systems Simulation with ... Lab 1 Handout Fall 2012.pdf · EE 4314 Lab 1 Handout Control Systems Simulation with MATLAB and SIMULINK Fall 2012 1. Lab

09/04/12 − Page 13

Example 9: Simulating an ODE Model using Simulink

You will create a Simulink model for

tfxxx 4.1

using continuous blocks. And you will use a user defined function block to handle arbitrary f(t). In

this example, you 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.

Page 14: EE 4314 Lab 1 Handout Control Systems Simulation with ... Lab 1 Handout Fall 2012.pdf · EE 4314 Lab 1 Handout Control Systems Simulation with MATLAB and SIMULINK Fall 2012 1. Lab

09/04/12 − Page 14

Step8: Connect the terminals as show in the picture below.

Step9: You can now set the Embedded MATLAB Function block to reflect any function. In this

example, you can use f(t) = sin(t).

Step10: You can set the initial value of the integrators by double clicking the integrator block. In

this example, you can leave them at 0.

Step11: Run the simulation, by pressing the play button or go to >>Simulation>>Start.

Step12: Now go to MATLAB command

window, notice 2 new variables in the

workspace “simout” and “tout”. You can use

these variables as normal MATLAB variables.

Plot of “simout” versus “tout” is shown in the

picture on the right.

Page 15: EE 4314 Lab 1 Handout Control Systems Simulation with ... Lab 1 Handout Fall 2012.pdf · EE 4314 Lab 1 Handout Control Systems Simulation with MATLAB and SIMULINK Fall 2012 1. Lab

09/04/12 − Page 15

3. Lab Report

Submit your individual report for the following four problems via EE4314 Blackboard

(https://elearn.uta.edu) in one of the following file formats: .doc, .docx, or .pdf. Your report should

include –if any– your formulations, listings of MATLAB code, pictures of Simulink block diagrams,

and resulting graphs for each problem. Make sure that you show all your work and provide all the

information needed to make it a standalone and self-sufficient submission. Have an appropriate report

format for your submission. This lab handout is a good example as to how you should format your

report. Make sure that you include the following information in your report:

- Report title, your name, ID number, lab section, report due date for you.

- Answers to the problems with your

o Mathematical derivations and formulations. It is highly recommended that you use the

Equation Editor of Microsoft Word, MathType, or a similar editor to write your

equations. You can also scan handwritten equations and merge them into your report.

o Pictures of Simulink block diagrams and property windows of important blocks in the

simulation.

o Listings of MATLAB codes with inline comments and explanation in text.

o Pictures of data plots with appropriate axis labeling, titles, and clearly visible axis

values. Screen printing is not well accepted.

- Comments –if any– to let us know how we can make your learning experience better in this

lab.

Below is the assignment policy:

This assignment is due 9/19/2012 by 11:59 pm.

You must upload a single file in .doc, .docx, or .pdf format via the “Lab Assignments” link at

EE4314 Blackboard (https://elearn.uta.edu).

Late reports will get 20% deduced score from the normal score for each late day (0-24 hr)

starting right after the due date and time. For example, a paper that is worth 80 points and is 2

days late (24hr – 48hr) will get 80 – 80 × 2 × (20/100) = 48 points. A paper that is late for 5 or

more days will get 0 score.

You will have two chances of attempt to submit your report via Blackboard and only the last

submission will be considered.

Grading is out of 100 points and that includes 20% (20 points total) for the format. For

example, part (b) of Problem 1 is 10 points and 2 points of that is for the format of your answer.

A nice format refers to a clear, concise, and well organized presentation of your work.

Page 16: EE 4314 Lab 1 Handout Control Systems Simulation with ... Lab 1 Handout Fall 2012.pdf · EE 4314 Lab 1 Handout Control Systems Simulation with MATLAB and SIMULINK Fall 2012 1. Lab

09/04/12 − Page 16

Problem 1 (25 pts). Let

(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), ζ = 2 is the damping ratio (dimensionless) and ωn = 8 (rad/s) is the natural frequency.

a) (15 pts) Use ode23() to simulate the time response of the system from t = 0 to t = 5 s with zero

input (i.e. f(t) = 0), and initial conditions of (0.4 m, 0.2 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 . 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 = 5 s where

and

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

Problem 2 (25 pts). For the system in equation (1) with ζ = 2, ωn = 8 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 . Indicate which

variable the plot shows: .

d) (6 pts) Using lsim command, plot the time response of the system where f is a sinusoidal wave of

amplitude 1.2 m/s2 and 0.75 Hz frequency for .

Problem 3 (25 pts). For the system in equation (1) with ζ = 2 and ωn = 8 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 .

b) (7 pts) Simulate the time response of the system from t = 0 to t = 5 s where with f(t) as a sinusoidal function of 1.2 m/s

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

Page 17: EE 4314 Lab 1 Handout Control Systems Simulation with ... Lab 1 Handout Fall 2012.pdf · EE 4314 Lab 1 Handout Control Systems Simulation with MATLAB and SIMULINK Fall 2012 1. Lab

09/04/12 − Page 17

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 = 1 and falls back to zero at t = 2.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.

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 and compare it with the step response obtained in Problem 2-c.

t=1 t=2.5

10

0

(a)

t=1

1.2

1

0 t=1.1 t=1.3 t=1.4

1.0

(b)