+ All Categories
Home > Documents > EE 4314: Lab 1 · Web viewAfter the lab, students will work on the problems at the end of the...

EE 4314: Lab 1 · Web viewAfter the lab, students will work on the problems at the end of the...

Date post: 11-May-2018
Category:
Upload: haliem
View: 214 times
Download: 0 times
Share this document with a friend
23
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
Transcript

EE 4314: Lab 1 – Systems Programming with MATLAB and SIMULINK

Location: NH 250Date and Time: Sept 3, 5:30 - 7:20 PM and Sept 5, 5:30 - 7:20 PMReport 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.1Matlab 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.2Basic 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.3Matlab 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; (a vector of length 11 will be defined)>> y = -1: 1 : 9; (another vector of length 11 will be defined)>> plot(x,y)

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 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 we will get

>> 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.4Vector 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 powereye(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 21 4

Element by element multiplication VS matrix multiplication

>> Y.*Zans = 3 6 2 4

>> Y*Zans = 6 14 7 8

Element by element power VS matrix power

>> Z^2ans = 11 14 7 18

>> Z.^2ans = 9 4 1 16

Inverse, transpose and multiplication example

>> inv(Z)ans = 0.4000 -0.2000 -0.1000 0.3000

>> inv(Z)*Y'ans = -0.2000 0.6000 0.8000 0.1000

1.5Complex Numbers

An imaginary can be defined using the i , j.

>> a = 2 + ia = 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.4636r = 2.2361

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

2.1Linear 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+2 s+10

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

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 )= s

s2+2 s+10 in pole-residue form of

G (s )=r1

s−p1+

r2

s−p2+…+

rn

s−pn+k (s )

. We 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 we can now rewrite G(s) as G (s )=0 .5+0. 1667 i

s+1−3i+ 0 .5−0 .1667i

s+1+3 i .

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

s2+2 s+10 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 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. 4 x+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

x+2 ζ ωn x++ωn2 x=f (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 x with time. Plot both variables on the same graph with proper labeling and annotation such that x and x 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 x1=x , x2= x. 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

where x (0 )=0 , x ( 0 )=0 and f ( t )={1 , 0 ≤ t ≤ 4 s0 , 4<t ≤ 10 s . Write the program using m-file(s) that

output a plot of the change of x and x with time. Plot both variables on the same graph with proper labeling and annotation such that x and x 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≤ t ≤10 s. Indicate which variable the plot shows: x , x ,∨ x.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≤ t ≤10 s. 

Problem 3 (25 pts). For the system in equation (1) with ζ = 3.5 and ωn = 10 rad/s,

t = .75 1.75 t = 3.5

10

0

(a)

t = 1

1.21

0 t = 1.1 t = 1.3 t = 1.4

1.0

(b)

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 x , x ,∧ x.b) (7 pts) Simulate the time response of the system from t = 0 to t = 5 s where x (0 )=0.4 , x (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.

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


Recommended