Docsity

Basic Data Structure - Introduction to MATLAB’s | MEM 255, Study notes of Mechanical Engineering

Toggle sidebar
bg1
MEM 255: Introduction to MATLAB
Professor Kwatny
Contents
MEM 255: Introduction to MATLAB........................................................................................................ 1
Contents......................................................................................................................................................... 1
Basics ............................................................................................................................................................. 2
Command Window ..................................................................................................................................... 2
m-files – scripts and functions.................................................................................................................... 2
Basic Data Structures – vectors, matrices and polynomials...................................................................... 3
Vectors................................................................................................................................................... 3
Matrices ................................................................................................................................................. 4
Example:................................................................................................................................................ 5
Polynomials ........................................................................................................................................... 5
Basic Plotting ................................................................................................................................................ 6
Simple Line Plots ....................................................................................................................................... 6
Frequency Domain Modeling ...................................................................................................................... 7
Complex Numbers...................................................................................................................................... 7
Transfer Functions..................................................................................................................................... 8
Creating Transfer Functions .................................................................................................................. 8
Converting between Forms.................................................................................................................... 8
Laplace Transforms ............................................................................................................................... 9
Analysis Tools............................................................................................................................................ 9
Root Locus............................................................................................................................................. 9
Bode..................................................................................................................................................... 10
Nyquist ................................................................................................................................................ 10
SISO Tool & LTI Viewer.................................................................................................................... 11
State Space Models..................................................................................................................................... 13
Converting between State Space and Transfer Function Objects............................................................ 14
Computing Step Responses........................................................................................................................ 15
Interconnections ......................................................................................................................................... 16
Eigenvalues & Eigenvectors ...................................................................................................................... 17
Chain of Three Inertias............................................................................................................................ 17
Antenna Positioning System..................................................................................................................... 19
Solving Ordinary Differential Equations ................................................................................................. 20
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Basic Data Structure - Introduction to MATLAB’s | MEM 255 and more Study notes Mechanical Engineering in PDF only on Docsity!

MEM 255: Introduction to MATLAB

Professor Kwatny

  • MEM 255: Introduction to MATLAB Contents
  • Contents.........................................................................................................................................................
  • Basics
    • Command Window
    • m-files – scripts and functions....................................................................................................................
    • Basic Data Structures – vectors, matrices and polynomials
      • Vectors...................................................................................................................................................
      • Matrices
      • Example:................................................................................................................................................
      • Polynomials
  • Basic Plotting
    • Simple Line Plots
  • Frequency Domain Modeling
    • Complex Numbers
    • Transfer Functions.....................................................................................................................................
      • Creating Transfer Functions
      • Converting between Forms....................................................................................................................
      • Laplace Transforms
    • Analysis Tools
      • Root Locus.............................................................................................................................................
      • Bode.....................................................................................................................................................
      • Nyquist
      • SISO Tool & LTI Viewer
  • State Space Models
    • Converting between State Space and Transfer Function Objects
  • Computing Step Responses........................................................................................................................
  • Interconnections
  • Eigenvalues & Eigenvectors
    • Chain of Three Inertias
    • Antenna Positioning System.....................................................................................................................
  • Solving Ordinary Differential Equations

Basics

These notes provide a short guide to the minimum MATLAB knowledge required for MEM 255.

There are on-line tutorials in the MEM lab. Of course, there is no substitute for spending time

using the software.

  • MATLAB Basics (Command window, vectors, functions, plotting, polynomials, matrices, complex

numbers, printing, m-files, help)

  • Modeling (representing transfer functions/state space models, creating LTI objects with ss, tf, zpk,

ssdata, tfdata, zpkdata)

  • Laplace transforms (basic tools laplace, ilaplace, residue)
  • Polynomial/Rational Function manipulation (poly, roots, conv, residue)
  • Conversions (ss, tf , zp2tf, tf2ss,ss2zp, ss2tf)
  • Block Diagram manipulation, (series, parallel, cloop, feedback)
  • Time response (step,impulse, lsim)
  • Frequency response (rlocus,bode, nyquist)
  • System Analysis Combo’s (sisotool, ltiview)
  • State space manipulation (performing similarity transformations, eig, canon)

Command Window

When you start MATLAB, the Command Window opens. A command can be typed at the

prompt, and it is executed upon pressing Enter. A MATLAB session generally consists of

entering and executing a sequence of commands. When you exit MATLAB the entire history of

the session is lost unless you specifically save it using the function diary.

To get help –

1. If know the name of the command you are interested in simply type

help name

at the prompt. If you don’t know, use a lookfor with a keyword (you may get

more help than you want).

2. To browse for functions in various categories go to the ‘help window’ from the help

menu.

3. If you are on-line, complete up to date help and detailed documentation is available from

the MathWorks web site. Simply choose ‘help desk’ from the help menu.

m-files – scripts and functions

An m-file, or script file, is a simple text file where you can place a sequence of MATLAB

commands. When the file is run (type the file name in the command window and press Enter),

MATLAB reads the commands and executes them. All m-file names must end with the extension

'.m' (e.g. plot.m). If you create a new m-file with the same name as an existing m-file, MATLAB

will choose the one that appears first in the path order (help path for more information). To see

if a filename.m exists, type help filename at the MATLAB prompt. You can create m-files

with any text editor. The editor ‘medit’ supplied with MATLAB is a good one.

When entering a MATLAB command such as roots, plot, or step, an m-file with inputs and

outputs is executed. These m-files are similar to subroutines in programming languages in that

they have inputs (parameters that are passed to the m-file), outputs (values that are returned from

the m-file), and a body of commands that can

contain local variables. MATLAB calls these m-files functions. You can write your own

functions using the function command.

Matrices

Matrices can be defined in a similar way to vectors, with rows separated by semicolons.

EDU» A=[1 2 3;4 5 6;7 8 9]

A =

EDU» B=[0,1,0;0,0,1]

B =

Matrices of compatible dimensions can be multiplied

EDU» B*A

ans =

4 5 6 7 8 9

They can b raised to powers

EDU» A^

ans =

468 576 684 1062 1305 1548 1656 2034 2412

Element by element multiplication of square matrices is accomplished by the .* or .^ operator.

For example

EDU» A.^

ans =

1 8 27 64 125 216 343 512 729

cubes each element of A.

Matrices can be transposed

EDU» B'

ans =

0 0 1 0 0 1

Table 1. Functions to create matrices

Function Returns

diag matrix with specified diagonal elements/or extracts diagonal entries

eye identity matrix

ones matrix filled with ones

rand matrix filled with random numbers

zeros matrix filled with zeros

linspace row vector of linear spaced elements

logspace row vector of logarithmically spaced elements

Example:

eye(4)

ans =

Polynomials

A polynomials can be represented by a vector containing its oredered coefficient list. The

polynomial

4 3 2 s + 2 s + 4 s + s + 3

is defined

EDU» poly1=[1 2 4 1 3]

poly1 =

1 2 4 1 3

and the polynomial

2 s + 1

is defined

EDU» poly2=[1 0 1]

poly2 =

Certain MATLAB functions interpret n + 1 dimensional vectors as -th order polynomials.

These include polyval, roots, conv and deconv. Polyval can be used to evaluate

polynomials, roots determines the roots of the given polynomial, conv multiplies two

polynomials, and deconv divides two polynomials.

n

EDU» conv(poly1,poly2)

ans =

1 2 5 3 7 1 3 EDU» [x,R]=deconv(poly1,poly2)

x =

R =

Notice that this last result states

4 3 2 2 2 2

2 4 3 2 3 1 1

s s s s s s s s s

        • − = + + +

The characteristic polynomial of a square matrix can be obtained

EDU» x=poly(A)

x =

Frequency Domain Modeling

Complex Numbers

Note that in MALAB, both i and j are predefined as − 1. For instance compute

j^

ans =

i^(3/2)

ans =

-0.7071 + 0.7071i

However, you can redefine them as something else. Once you do, the original definitions are

gone.

j=5; j^

ans =

25

A complex number is ordinarily expressed in rectangular coordinates z = x + iy

where x is the ‘real’ part and y is the ‘imaginary’ part, or, alternatively, in polar or Euler

notation

i

z e

θ

= ρ , where ρ is the ‘magnitude’ and θ is the ‘angle.’

Table 2. Operations on complex numbers

Function Returns

imag imaginary part of a complex number

real real part of a complex number

abs magnitude of a complex number

angle angle of a complex number

conj conjugate of a complex number

Example:

z=1+i

z =

1.0000 + 1.0000i

abs(z)

ans =

angle(z)*180/pi

ans =

45

Transfer Functions

Creating Transfer Functions

1. numerical using the functions tf or zpk

2. symbolic

EDU» numf=10*[1 2]; EDU» denf=[1 6 9]; EDU» F=tf(numf,denf)

Transfer function: 10 s + 20


s^2 + 6 s + 9

EDU» G=zpk([-2],[-3 -3],10)

Zero/pole/gain: 10 (s+2)


(s+3)^

EDU» s=tf('s'); EDU» H=10*(s+2)/(s+3)^

Transfer function: 10 s + 20


s^2 + 6 s + 9

Converting between Forms

EDU» FF=tf(G)

Transfer function: 10 s + 20


s^2 + 6 s + 9

EDU» GG=zpk(H)

Zero/pole/gain: 10 (s+2)


(s+3)^

Put the pointer on any point on the root loci and click to obtain data.

Bode

EDU» bode(H)

Frequency (rad/sec)

Phase (deg); Magnitude (dB)

Bode Diagrams

0

10

From: U(1)

10 -1^100 101

0

To: Y(1)

Nyquist

nyquist(H)

Real Axis

Imaginary Axis

Nyquist Diagrams

-1 -0.5 0 0.5 1 1.5 2 2.

-1.

-0.

0

1

From: U(1)

To: Y(1)

SISO Tool & LTI Viewer

Here are a couple of tools that combine various functions into a simple to use package.

G=(s+1)/(s^2+2(1/2)2*s+4)

Transfer function: s + 1


s^2 + 2 s + 4

C=(s+2)/s

Transfer function: s + 2


s

sisotool ltiview GCL=2CG/(1+2CG)

Transfer function: 2 s^5 + 10 s^4 + 24 s^3 + 32 s^2 + 16 s


s^6 + 6 s^5 + 22 s^4 + 40 s^3 + 48 s^2 + 16 s

State Space Models

State space models can be defined using the function ss.

EDU» A=[0 1 0;0 0 1;-3 -2 -1];

EDU» B=[1;1;1];

EDU» C=[1 2 1];

EDU» D=0;

EDU» Fss=ss(A,B,C,D)

a = x1 x2 x x1 0 1 0 x2 0 0 1 x3 -3 -2 -

b = u x1 1 x2 1

x3 1

c = x1 x2 x y1 1 2 1

d = u y1 0

Continuous-time model.

Converting between State Space and Transfer Function Objects

A state space model can be converted to a transfer function model using tf

EDU» Ftf=tf(Fss)

Transfer function: 4 s^2 + s - 5


s^3 + s^2 + 2 s + 3

Transfer function models can be converted to state space using ss

G=(s+1)/(s^2+2(1/2)2*s+4)

Transfer function: s + 1


s^2 + 2 s + 4

Gss=ss(G)

a = x1 x x1 -2 - x2 4 0

b = u x1 1 x2 0

c = x1 x y1 1 0.

d = u y1 0

Continuous-time model.

Gss is an LTI object. You can extract the data using ssdata. The function ssdata can be applied to

either G or Gss.

[A,B,C,D]=ssdata(Gss)

A =

-2 - 4 0

For example:

EDU» [y2,t2]=step(Fss); EDU» plot(t2,y2)

0 2 4 6 8 10 12 14 16 18

0

10

20

30

Notice that the system is unstable. To see this, check the transfer function in pole zero form:

EDU» Fzpk=zpk(Fss)

Zero/pole/gain: 4 (s+1.25) (s-1)


(s+1.276) (s^2 - 0.2757s + 2.352)

Interconnections

The basic MATLAB tools for interconnecting LTI objects are the functions: parallel(+),

series (*) and feedback. The following example illustrates the use of series and

feedback.

2

s

s s

s

s +

s + 4

EDU» s=tf('s'); EDU» sys1=(s+1)/(s^2+s+2)

Transfer function: s + 1


s^2 + s + 2

EDU» sys2=2*s/(s+5)

Transfer function: 2 s


s + 5

EDU» sys3=4/(s+4)

Transfer function: 4


s + 4

EDU» sys4=feedback(sys1,sys2)

Transfer function: s^2 + 6 s + 5


s^3 + 8 s^2 + 9 s + 10

EDU» sys5=series(sys4,sys3)

Transfer function: 4 s^2 + 24 s + 20


s^4 + 12 s^3 + 41 s^2 + 46 s + 40

Notice that the last step could have been accomplished using multiplication, i.e.,

EDU» sys4*sys

Transfer function: 4 s^2 + 24 s + 20


s^4 + 12 s^3 + 41 s^2 + 46 s + 40

Also, these commands work with state space as well as transfer function objects.

Eigenvalues & Eigenvectors

The simplest way to compute eigenvalues and eigenvectors is to use the function eig in

MATLAB. For large systems, the function eigs might be a better choice because it computes

only the first k (user specified) eigenvalue – eigenvector pairs.

Chain of Three Inertias

friction, c

m

k

m m

k

1 2 1 1

2 2 1 3 2

3 3 2 3

mx k x x cx

mx k x x k x x cx

mx k x x cx

2

or

m

k

m m

k

m

k

m m

k

Low frequency

high frequency

Antenna Positioning System

The antenna positioning system shown in the figure has transfer function

3 2

G s

s s s s s s

1

2

3

and a state space model (one of many – but this one has the states indicated in the block diagram):

[ ]

1 1

2 2

3 3

x x x

x x u y x

x x x

Drive Electronics

Load Dynamics (drive motor, gear train, dish)

Kinematics

s + 5

s + 1

s

Position Command Signal

Torque Velocity

Angular Position

u y

x 3 x 2 x 1

EDU» A=[0 1 0;0 -1 1;0 0 -5];

EDU» [v,e]=eig(A)

v =

1.0000 -0.7071 0. 0 0.7071 -0. 0 0 0.

e =

Solving Ordinary Differential Equations

MATLAB provides a large number of tools for solving ordinary differential equations. In general

solving ODEs is complicated because any situation can have unique characteristics that require

special treatment. Nevertheless, a MATLAB function that has very broad applicability is the

differential equation solver ode45.

The problem to be solved is this. Let x be the n -dimensional state vector, and suppose the state

equation has the general form

x  = f ( x t , )

with a specified initial condition

x ( t 0 )= x 0

We wish to numerically compute the solution x ( t )on the time interval t 0 ≤ t ≤ tf using the

function ode45. The differential equation is integrated from t 0 to tf by

[T,X] = ODE45(ODEFUN,TSPAN,Y0)

[T,X] = ODE45(ODEFUN,TSPAN,Y0,OPTIONS)

where

TSPAN = [T0 TFINAL]

Y0 is the initial condition Function ODEFUN(t,y) returns a column vector corresponding to f(t,y) Each row in the solution array Y corresponds to a time returned in the column vector T. OPTIONS, is an argument created with the ODESET function. See ODESET for details. Replaces default integration properties by values defined in ODESET.

For example, consider the Euler equations that describe the rotation of a rigid body about its fixed

center of mass

1 2 3

2 1 3

x x x

x x x

x x x 2

with initial conditions x 1 ( 0 ) = 0, x 2 ( 0 ) = 1, x 3 ( 0 )= 1. The vector x is the body angular velocity

vector.

function dy = rigid(t,y) dy = zeros(3,1); % a column vector dy(1) = y(2) * y(3); dy(2) = -y(1) * y(3); dy(3) = -0.51 * y(1) * y(2);

options = odeset('RelTol',1e-4,'AbsTol',[1e-4 1e-4 1e-5]); [T,Y] = ode45(@rigid,[0 12],[0 1 1],options); plot(T,Y(:,1),'-',T,Y(:,2),'-.',T,Y(:,3),'.')