Sie sind auf Seite 1von 116

Signals and Systems

Fall 2009 Prof. Damir Sersic

Most of the electronic materials (presentations, assignments, instructions, ) that we use in this course come from the public domain sources, made under the Creative Commons license. The authors are:
Scepanovic, Danilo, and Patrick Ho, 6.094 Introduction to MATLAB, January (IAP) 2009. (Massachusetts Institute of Technology: MIT OpenCourseWare), http://ocw.mit.edu (Accessed 04 Oct, 2009). License: Creative Commons BY-NC-SA

The materials are partially modified and adapted to fulfill the needs of this course: we do not suggest in any way that the attributed authors endorse the modified course materials, as well as the actual lecturers and lecture deliveries.
A Vocational Degree programme developed by MCAST and Fraunhofer IAO.

Introduction to Programming in MATLAB

6.094

Lecture 1: Variables, Operations, and Plotting

Sourav Dey Danilo epanovi Ankit Patel Patrick Ho


IAP 2009

Outline
(1) (2) (3) (4) Getting Started Making Variables Manipulating Variables Basic Plotting

Current directory

Workspace

Command Window

Command History

Courtesy of The MathWorks, Inc. Used with permission.

Customization
File Preferences
Allows you personalize your MATLAB experience

Courtesy of The MathWorks, Inc. Used with permission.

MATLAB Basics
MATLAB can be thought of as a super-powerful graphing calculator
Remember the TI-83 from calculus? With many more buttons (built-in functions)

In addition it is a programming language


MATLAB is an interpreted language, like Scheme Commands executed line by line

Conversing with MATLAB


who MATLAB replies with the variables in your workspace what MATLAB replies with the current directory and MATLAB files in the directory why help The most important function for learning MATLAB on your own More on help later

Outline
(1) (2) (3) (4) Getting Started Making Variables Manipulating Variables Basic Plotting

Variable Types
MATLAB is a weakly typed language No need to initialize variables! MATLAB supports various types, the most often used are 3.84 64-bit double (default) a 16-bit char Most variables youll deal with will be arrays or matrices of doubles or chars Other types are also supported: complex, symbolic, 16-bit and 8 bit integers, etc.

Naming variables
To create a variable, simply assign a value to a name: var1=3.14 myString=hello world Variable names
first character must be a LETTER after that, any combination of letters, numbers and _ CASE SENSITIVE! (var1 is different from Var1)

Built-in variables i and j can be used to indicate complex numbers pi has the value 3.1415926 ans stores the last unassigned value (like on a calculator) Inf and -Inf are positive and negative infinity NaN represents Not a Number

Hello World
Here are several flavors of Hello World to introduce MATLAB MATLAB will display strings automatically Hello 6.094 To remove ans =, use disp() disp('Hello 6.094') sprintf() allows you to mix strings with variables class=6.094; disp(sprintf('Hello %g', class)) The format is C-syntax

Scalars

A variable can be given a value explicitly a = 10


shows up in workspace!

Or as a function of explicit values and existing variables c = 1.3*45-2*a To suppress output, end the line with a semicolon cooldude = 13/3;

Arrays
Like other programming languages, arrays are an important part of MATLAB Two types of arrays
(1) matrix of numbers (either double or complex) (2) cell array of objects (more advanced data structure) MATLAB makes vectors easy! Thats its power!

Row Vectors
Row vector: comma or space separated values between brackets row = [1 2 5.4 -6.6]; row = [1, 2, 5.4, -6.6]; Command window:

Workspace:

Courtesy of The MathWorks, Inc. Used with permission.

Column Vectors
Column vector: semicolon separated values between brackets column = [4;2;7;4]; Command window:

Workspace:

Courtesy of The MathWorks, Inc. Used with permission.

Matrices
Make matrices like vectors Element by element a= [1 2;3 4];

1 2 a= 3 4

By concatenating vectors or matrices (dimension matters) a = [1 2]; b = [3 4]; c = [5;6]; d = [a;b]; e = [d c]; f = [[e e];[a b a]];

save/clear/load
Use save to save variables to a file save myfile a b saves variables a and b to the file myfile.mat myfile.mat file in the current directory Default working directory is \MATLAB\work Create own folder and change working directory to it MyDocuments\6.094\day1 Use clear to remove variables from environment clear a b look at workspace, the variables a and b are gone Use load to load variable bindings into the environment load myfile look at workspace, the variables a and b are back Can do the same for entire environment save myenv; clear all; load myenv;

Exercise: Variables
Do the following 5 things: Create the variable r as a row vector with values 1 4 7 10 13 Create the variable c as a column vector with values 13 10 7 4 1 Save these two variables to file varEx clear the workspace load the two variables you just created r=[1 4 7 10 13]; c=[13; 10; 7; 4; 1]; save varEx r c clear r c load varEx

Outline
(1) (2) (3) (4) Getting Started Making Variables Manipulating Variables Basic Plotting

Basic Scalar Operations


Arithmetic operations (+,-,*,/) 7/45 (1+i)*(2+i) 1 / 0 0 / 0 Exponentiation (^) 4^2 (3+4*j)^2 Complicated expressions, use parentheses ((2+3)*3)^0.1 Multiplication is NOT implicit given parentheses 3(1+0.7) gives an error To clear cluttered command window Clc

Built-in Functions
MATLAB has an enormous library of built-in functions Call using parentheses passing parameter to function sqrt(2) log(2), log10(0.23) cos(1.2), atan(-.8) exp(2+4*i) round(1.4), floor(3.3), ceil(4.23) angle(i); abs(1+i);

Help/Docs
To get info on how to use a function: help sin Help contains related functions To get a nicer version of help with examples and easy-toread descriptions: doc sin To search for a function by specifying keywords: doc + Search tab lookfor hyperbolic
One-word description of what you're looking for

Exercise: Scalars
Verify that e^(i*x) = cos(x) + i*sin(x) for a few values of x.

x = pi/3; a = exp(i*x) b = cos(x)+ i*sin(x) a-b

size & length


You can tell the difference between a row and a column vector by:
Looking in the workspace Displaying the variable in the command window Using the size function

To get a vector's length, use the length function

transpose
The transpose operators turns a column vector into a row vector and vice versa a = [1 2 3 4] transpose(a) Can use dot-apostrophe as short-cut a.' The apostrophe gives the Hermitian-transpose, i.e. transposes and conjugates all complex numbers a = [1+j 2+3*j] a' a.' For vectors of real numbers .' and ' give same result

Addition and Subtraction


Addition and subtraction are element-wise; sizes must match (unless one is a scalar):

[12 3 + [ 2 11 = [14 14

32 11] 30 32] 2 21]

12 3 9 1 1 2 = 10 13 23 0 33 33

The following would give an error c = row + column Use the transpose to make sizes compatible c = row + column c = row + column Can sum up or multiply elements of vector s=sum(row); p=prod(row);

Element-Wise Functions
All the functions that work on scalars also work on vectors t = [1 2 3]; f = exp(t);
is the same as

f = [exp(1) exp(2) exp(3)]; If in doubt, check a functions help file to see if it handles vectors elementwise Operators (* / ^) have two modes of operation
element-wise standard

Operators: element-wise
To do element-wise operations, use the dot. BOTH dimensions must match (unless one is scalar)! a=[1 2 3];b=[4;2;1]; a.*b, a./b, a.^b all errors a.*b, a./b, a.^(b) all valid
4 [1 2 3] .* 2 = ERROR 1 1 4 4 2 .* 2 = 4 3 1 3 3 1.* 3 1 = 3 1
1 1 1 1 2 3 1 2 3 2 2 2 .* 1 2 3 = 2 4 6 3 3 3 1 2 3 3 6 9 3 3.* 3 3 = 3 3

12 22 1 2 3 4 .^ 2 = 2 3 42 Can be any dimension

Operators: standard
Multiplication can be done in a standard way or element-wise Standard multiplication (*) is either a dot-product or an outerproduct
Remember from linear algebra: inner dimensions must MATCH!!

Standard exponentiation (^) implicitly uses *


Can only be done on square matrices or scalars

Left and right division (/ \) is same as multiplying by inverse Our recommendation: just multiply by inverse (more on this later)
4 [1 2 3]* 2 = 11 1 1 3* 3 1 = 1 1
1 2 1 2 1 2 ^2= 3 4 * 3 4 3 4 Must be square to do powers

1 1 1 1 2 3 3 6 9 2 2 2 * 1 2 3 = 6 12 18 3 3 3 1 2 3 9 18 27 3 3* 3 3 = 3 3

Exercise: Vector Operations


Find the inner product between [1 2 3] and [3 5 4] a=[1 2 3]*[3 5 4] Multiply the same two vectors element-wise b=[1 2 3].*[3 5 4] Calculate the natural log of each element of the resulting vector c=log(b)

Automatic Initialization
Initialize a vector of ones, zeros, or random numbers o=ones(1,10)
row vector with 10 elements, all 1

z=zeros(23,1)
column vector with 23 elements, all 0

r=rand(1,45)
row vector with 45 elements (uniform [0,1])

n=nan(1,69)
row vector of NaNs (useful for representing uninitialized variables) The general function call is: var=zeros(M,N); Number of rows Number of columns

Automatic Initialization
To initialize a linear vector of values use linspace a=linspace(0,10,5)
starts at 0, ends at 10 (inclusive), 5 values

Can also use colon operator (:) b=0:2:10


starts at 0, increments by 2, and ends at or before 10 increment can be decimal or negative

c=1:5
if increment isnt specified, default is 1

To initialize logarithmically spaced values use logspace


similar to linspace

Exercise: Vector Functions


Make a vector that has 10,000 samples of f(x) = e^{-x}*cos(x), for x between 0 and 10. x = linspace(0,10,10000); f = exp(-x).*cos(x);

Vector Indexing
MATLAB indexing starts with 1, not 0 We will not respond to any emails where this is the problem. a(n) returns the nth element

[13
a(1)

5 9 10]
a(3) a(4)

a(2)

The index argument can be a vector. In this case, each element is looked up individually, and returned as a vector of the same size as the index vector. x=[12 13 5 8]; a=x(2:3); a=[13 5]; b=x(1:end-1); b=[12 13 5];

Matrix Indexing
Matrices can be indexed in two ways
using subscripts (row and column) using linear indices (as if matrix is a vector)

Matrix indexing: subscripts or linear indices

b(1,1) b(2,1)

14 33 9 8

b(1,2) b(2,2)

b(1) b(2)

14 33 9 8

b(3) b(4)

Picking submatrices A = rand(5) % shorthand for 5x5 matrix A(1:3,1:2) % specify contiguous submatrix A([1 5 3], [1 4]) % specify rows and columns

Advanced Indexing 1
The index argument can be a matrix. In this case, each element is looked up individually, and returned as a matrix of the same size as the index matrix. a=[-1 10 3 -2]; b=a([1 2 4;3 4 2]);

1 10 2 b= 3 2 10

To select rows or columns of a matrix, use the :

12 5 c= 2 13
d=c(1,:); e=c(:,2); c(2,:)=[3 6]; d=[12 5]; e=[5;13]; %replaces second row of c

Advanced Indexing 2
MATLAB contains functions to help you find desired values within a vector or matrix vec = [1 5 3 9 7] To get the minimum value and its index: [minVal,minInd] = min(vec); To get the maximum value and its index: [maxVal,maxInd] = max(vec); To find any the indices of specific values or ranges ind = find(vec == 9); ind = find(vec > 2 & vec < 6);
find expressions can be very complex, more on this later

To convert between subscripts and indices, use ind2sub, and sub2ind. Look up help to see how to use them.

Exercise: Vector Indexing


Evaluate a sine wave at 1,000 points between 0 and 2*pi. Whats the value at
Index 55 Indices 100 through 110

Find the index of


the minimum value, the maximum value, and values between -0.001 and 0.001

x = linspace(0,2*pi,1000); y=sin(x); y(55) y(100:110) [minVal,minInd]=min(y) [maxVal,maxInd]=max(y) inds=find(y>-0.001 & y<0.001)

BONUS Exercise: Matrices


Make a 3x100 matrix of zeros, and a vector x that has 100 values between 0 and 10 mat=zeros(3,100); x=linspace(0,10,100); Replace the first row of the matrix with cos(x) mat(1,:)=cos(x); Replace the second row of the matrix with log((x+2)^2) mat(2,:)=log((x+2).^2); Replace the third row of the matrix with a random vector of the correct size mat(3,:)=rand(1,100); Use the sum function to compute row and column sums of mat (see help) rs = sum(mat,2); cs = sum(mat); % default dimension is 1

Outline
(1) (2) (3) (4) Getting Started Making Variables Manipulating Variables Basic Plotting

Plotting Vectors
Example x=linspace(0,4*pi,10); y=sin(x); Plot values against their index plot(y); Usually we want to plot y versus x plot(x,y);
MATLAB makes visualizing data fun and easy!

What does plot do?


plot generates dots at each (x,y) pair and then connects the dots with a line To make plot of a function look smoother, evaluate at more points x=linspace(0,4*pi,1000); plot(x,sin(x)); x and y vectors must be same size or else youll get an error plot([1 2], [1 2 3])
error!!
1 1

10 x values:

0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1 0 2 4 6 8 10 12 14

1000 x values:

0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1 0 2 4 6 8 10 12 14

Plot Options
Can change the line color, marker style, and line style by adding a string argument plot(x,y,k.-);
color marker line-style

Can plot without connecting the dots by omitting line style argument plot(x,y,.) Look at help plot for a full list of colors, markers, and linestyles

Other Useful plot Commands


Much more on this in Lecture 2, for now some simple commands To To plot two lines on the same graph hold on; plot on a new figure figure; plot(x,y);

Play with the figure GUI to learn more


add axis labels add a title add a grid zoom in/zoom out

Exercise: Plotting
Plot f(x) = e^x*cos(x) on the interval x = [0 10]. Use a red solid line with a suitable number of points to get a good resolution. x=0:.01:10; plot(x,exp(x).*cos(x),r);

End of Part 1
(1) (2) (3) (4) Getting Started Making Variables Manipulating Variables Basic Plotting
Hope that wasnt too much!!

Outline
(1) (2) (3) (4) Plotting Continued Scripts Functions Flow Control

Axis
A grid makes it easier to read values grid on
xlim sets only the x axis limits

xlim([-pi pi]); ylim sets only the y axis limits ylim([-1 1]);

To specify both at once, use axis: axis([-pi pi -1 1]);


sets the x axis limits between -pi and pi and the y axis limits between -1 and 1

Can specify tickmarks set(gca,'XTick', linspace(-pi,pi,3))


see doc axes for a list of properties you can set this way more on advanced figure customization in lecture 4

surf
Make the x and y vectors x=-pi:0.1:pi; y=-pi:0.1:pi; Use meshgrid to make matrices (this is the same as loop) [X,Y]=meshgrid(x,y); To get function values, evaluate the matrices Z =sin(X).*cos(Y); Plot the surface surf(X,Y,Z) surf(x,y,Z);

surf Options
See help surf for more options There are three types of surface shading shading faceted shading flat shading interp You can change colormaps colormap(gray)

Exercise: 3-D Plots


Plot exp(-.1(x^2+y^2))*sin(xy) for x,y in [2*pi,2*pi] with interpolated shading and a hot colormap: x=-2*pi:0.1:2*pi; y=-2*pi:0.1:2*pi; [X,Y]=meshgrid(x,y); Z =exp(-.1*(X.^2+Y.^2)).*sin(X.*Y); surf(X,Y,Z); shading interp colormap hot

Outline
(1) (2) (3) (4) Plotting Continued Scripts Functions Flow Control

Scripts: Overview
Scripts are
written in the MATLAB editor saved as MATLAB files (.m extension) evaluated line by line

To create an MATLAB file from command-line edit myScript.m or click

Courtesy of The MathWorks, Inc. Used with permission.

Scripts: the Editor


* Means that it's not saved Line numbers MATLAB file path Debugging tools

Help file

Comments

Possible breakpoints

Courtesy of The MathWorks, Inc. Used with permission.

Scripts: Good Practice


Take advantage of "smart indent" option Keep code clean
Use built-in functions Vectorize, vectorize, vectorize When making large matrices, allocate space first
Use nan or zeros to make a matrix of the desired size

Keep constants at the top of the MATLAB file COMMENT!


Anything following a % is seen as a comment The first contiguous comment becomes the script's help file Comment thoroughly to avoid wasting time later

Hello World
Here are several flavors of Hello World to introduce MATLAB MATLAB will display strings automatically Hello Malta To remove ans =, use disp() disp('Hello Malta') sprintf() allows you to mix strings with variables class=1; disp(sprintf('Hello %g', class)) The format is C-syntax

Exercise: Scripts
A student has taken three exams. The performance on the exams is random (uniform between 0 and 100) The first exam is worth 20%, the second is worth 30%, and the final is worth 50% of the grade Calculate the student's overall score Save script as practiceScript.m and run a few times scores=rand(1,3)*100; weights=[0.2 0.3 0.5]; overall=scores*weights

Outline
(1) (2) (3) (4) Plotting Continued Scripts Functions Flow Control

User-defined Functions
Functions look exactly like scripts, but for ONE difference
Functions must have a function declaration

Help file

Function declaration Outputs Inputs

Courtesy of The MathWorks, Inc. Used with permission.

User-defined Functions
Some comments about the function declaration
Inputs must be specified function [x, y, z] = funName(in1, in2) Must have the reserved word: function If more than one output, must be in brackets Function name should match MATLAB file name

No need for return: MATLAB returns the variables whose names match those in the function declaration Variable scope: Any variables created within the function but not returned disappear after the function stops running Can have variable input arguments (see help varargin)

Functions: Exercise
Take the script we wrote to calculate the student's overall score and make it into a function The inputs should be
the scores row vector the weight row vector, with the same length as scores

The output should be


A scalar: the overall score

Assume the user knows the input constraints (no need to check if the inputs are in the correct format\size) Name the function overallScore.m

Functions: Exercise

Courtesy of The MathWorks, Inc. Used with permission.

Functions
We're familiar with zeros size length sum Look at the help file for size by typing help size The help file describes several ways to invoke the function
D = SIZE(X) [M,N] = SIZE(X) [M1,M2,M3,...,MN] = SIZE(X) M = SIZE(X,DIM)

Functions
MATLAB functions are generally overloaded
Can take a variable number of inputs Can return a variable number of outputs

What would the following commands return: a=zeros(2,4,8); D=size(a) [m,n]=size(a) [x,y,z]=size(a) m2=size(a,2) Take advantage of overloaded methods to make your code cleaner!

Outline
(1) (2) (3) (4) Plotting Continued Scripts Functions Flow Control

Relational Operators
MATLAB uses mostly standard relational operators
equal not equal greater than less than greater or equal less or equal == ~= > < >= <=

Logical operators
And Or Not Xor All true Any true

normal
& | ~ xor all any

bitwise
&& ||

Boolean values: zero is false, nonzero is true See help . for a detailed list of operators

if/else/elseif
Basic flow-control, common to all languages MATLAB syntax is somewhat unique
IF if cond commands end if cond commands1 else commands2 Conditional statement: evaluates to true or false end ELSE ELSEIF if cond1 commands1 elseif cond2 commands2 else commands3 end

No need for parentheses: command blocks are between reserved words

for
for loops: use for a definite number of iterations MATLAB syntax:
Loop variable

for n=1:100 commands end The loop variable

Command block

Is defined as a vector Is a scalar within the command block Does not have to have consecutive values

The command block


Anything between the for line and the end

while
The while is like a more general for loop:
Don't need to know number of iterations

WHILE while cond commands end

The command block will execute while the conditional expression is true Beware of infinite loops!

Exercise: Control-Flow
Write a function to calculate the factorial of an integer N using a loop (you can use a for or while loop). If the input is less than 0, return NaN. Test it using some values. function a = factorial(N) if N<0, a=nan, else a = 1; for k=1:N a = a*k; end end

But note that factorial() is already implemented! You should see if there are built-in functions before implementing something yourself. which factorial

find
find is a very important function
Returns indices of nonzero values Can simplify code and help avoid loops

Basic syntax: index=find(cond) x=rand(1,100); inds = find(x>0.4 & x<0.6); inds will contain the indices at which x has values between 0.4 and 0.6. This is what happens:
x>0.4 returns a vector with 1 where true and 0 where false x<0.6 returns a similar vector The & combines the two vectors using an and The find returns the indices of the 1's

Exercise: Flow Control


Given x= sin(linspace(0,10*pi,100)), how many of the entries are positive?
Using a loop and if/else count=0; for n=1:length(x) if x(n)>0 count=count+1; end end Being more clever count=length(find(x>0));
length(x) 100 10,000 100,000 1,000,000 Loop time 0.01 0.1 0.22 1.5 Find time 0 0 0 0.04

Avoid loops like the plague!

Built-in functions will make it faster to write and execute

Efficient Code
Avoid loops whenever possible
This is referred to as vectorization

Vectorized code is more efficient for MATLAB Use indexing and matrix operations to avoid loops For example: a=rand(1,100); a=rand(1,100); b=[0 a(1:end-1)]+a; b=zeros(1,100); Efficient and clean for n=1:100 if n==1 b(n)=a(n); else b(n)=a(n-1)+a(n); end end
Slow and complicated

End of Lecture 2
(1) (2) (3) (4) Plotting Continued Scripts Functions Flow Control
Vectorization makes coding fun!

Outline
(1) (2) (3) (4) (5) Linear Algebra Polynomials Optimization Differentiation/Integration Differential Equations

Systems of Linear Equations


Given a system of linear equations
x+2y-3z=5 -3x-y+z=-8 x-y+z=0

MATLAB makes linear algebra fun!

Construct matrices so the system is described by Ax=b A=[1 2 -3;-3 -1 1;1 -1 1]; b=[5;-8;0]; And solve with a single line of code! x=A\b;
x is a 3x1 vector containing the values of x, y, and z

The \ will work with square or rectangular systems. Gives least squares solution for rectangular systems. Solution depends on whether the system is over or underdetermined.

More Linear Algebra


Given a matrix mat=[1 2 -3;-3 -1 1;1 -1 1]; Calculate the rank of a matrix r=rank(mat);
the number of linearly independent rows or columns

Calculate the determinant d=det(mat);


mat must be square if determinant is nonzero, matrix is invertible

Get the matrix inverse E=inv(mat);


if an equation is of the form A*x=b with A a square matrix, x=A\b is the same as x=inv(A)*b

Matrix Decompositions
MATLAB has built-in matrix decomposition methods The most common ones are [V,D]=eig(X)
Eigenvalue decomposition

[U,S,V]=svd(X)
Singular value decomposition

[Q,R]=qr(X)
QR decomposition

Exercise: Linear Algebra


Solve the following systems of equations:
System 1: x+4y=34 -3x+y=2

System 2: 2x-2y=4 -x+y=3 3x+4y = 2

Exercise: Linear Algebra


Solve the following systems of equations:
System 1: x+4y=34 -3x+y=2

A=[1 4;-3 1]; b=[34;2]; rank(A) x=inv(A)*b;

System 2: 2x-2y=4 -x+y=3 3x+4y = 2

A=[2 -2;-1 1;3 4]; b=[4;3;2]; rank(A)


rectangular matrix

x1=A\b;
gives least squares solution

A*x1

Outline
(1) (2) (3) (4) (5) Linear Algebra Polynomials Optimization Differentiation/Integration Differential Equations

Polynomials
Many functions can be well described by a high-order polynomial MATLAB represents a polynomials by a vector of coefficients
if vector P describes a polynomial

ax3+bx2+cx+d
P(1) P(2) P(3) P(4)

P=[1 0 -2] represents the polynomial x2-2 P=[2 0 0 0] represents the polynomial 2x3

Polynomial Operations
P is a vector of length N+1 describing an N-th order polynomial To get the roots of a polynomial r=roots(P)
r is a vector of length N

Can also get the polynomial from the roots P=poly(r)


r is a vector length N

To evaluate a polynomial at a point y0=polyval(P,x0)


x0 is a single value; y0 is a single value

To evaluate a polynomial at many points y=polyval(P,x)


x is a vector; y is a vector of the same size

Outline
(1) (2) (3) (4) (5) Linear Algebra Polynomials Optimization Differentiation/Integration Differential Equations

Nonlinear Root Finding


Many real-world problems require us to solve f(x)=0 Can use fzero to calculate roots for any arbitrary function fzero needs a function passed to it. We will see this more and more as we delve into solving equations. Make a separate function file x=fzero('myfun',1) x=fzero(@myfun,1)
1 specifies a point close to the root

Courtesy of The MathWorks, Inc. Used with permission.

Minimizing a Function
fminbnd: minimizing a function over a bounded interval x=fminbnd('myfun',-1,2);
myfun takes a scalar input and returns a scalar output myfun(x) will be the minimum of myfun for -1x 2

fminsearch: unconstrained interval x=fminsearch('myfun',.5)


finds the local minimum of myfun starting at x=0.5

Optimization Toolbox
If you are familiar with optimization methods, use the optimization toolbox Useful for larger, more structured optimization problems Sample functions (see help for more info) linprog
linear programming using interior point methods

quadprog
quadratic programming solver

fmincon
constrained nonlinear optimization

Outline
(1) (2) (3) (4) (5) Linear Algebra Polynomials Optimization Differentiation/Integration Differential Equations

Numerical Differentiation
1 0.8 MATLAB can 'differentiate' numerically 0.6 x=0:0.01:2*pi; 0.4 y=sin(x); 0.2 dydx=diff(y)./diff(x); 0

diff computes the first difference

-0.2 -0.4

Can also operate on matrices mat=[1 3 5;4 8 6]; dm=diff(mat,1,2)

-0.6 -0.8 -1 0 100 200 300 400 500 600 700

first difference of mat along the 2nd dimension, dm=[2 2;4 -2] see help for more details

2D gradient [dx,dy]=gradient(mat);

Numerical Integration
MATLAB contains common integration methods Adaptive Simpson's quadrature (input is a function) q=quad('derivFun',0,10);
q is the integral of the function derivFun from 0 to 10

q2=quad(@sin,0,pi)
q2 is the integral of sin from 0 to pi

Trapezoidal rule (input is a vector) x=0:0.01:pi; z=trapz(x,sin(x));


z is the integral of sin(x) from 0 to pi

z2=trapz(x,sqrt(exp(x))./x) x z2 is the integral of e x from 0 to pi

Outline
(1) (2) (3) (4) (5) Linear Algebra Polynomials Optimization Differentiation/Integration Differential Equations

ODE Solvers: Method


Given a differential equation, the solution can be found by integration:

Evaluate the derivative at a point and approximate by straight line Errors accumulate! Variable timestep can decrease the number of iterations

ODE Solvers: MATLAB


MATLAB contains implementations of common ODE solvers Using the correct ODE solver can save you lots of time and give more accurate results ode23
Low-order solver. Use when integrating over small intervals or when accuracy is less important than speed

ode45
High order (Runge-Kutta) solver. High accuracy and reasonable speed. Most commonly used.

ode15s
Stiff ODE solver (Gear's algorithm), use when the diff eq's have time constants that vary by orders of magnitude

ODE Solvers: Standard Syntax


To use standard options and variable time step [t,y]=ode45('myODE',[0,10],[1;0])
ODE integrator: 23, 45, 15s ODE function Initial conditions Time range

Inputs:
ODE function name (or anonymous function). This function takes inputs (t,y), and returns dy/dt Time interval: 2-element vector specifying initial and final time Initial conditions: column vector with an initial condition for each ODE. This is the first input to the ODE function

Outputs:
t contains the time points y contains the corresponding values of the integrated fcn.

ODE Function
The ODE function must return the value of the derivative at a given time and function value Example: chemical reaction
Two equations 10

dA = 10 A + 50 B dt dB = 10 A 50 B dt
ODE file:

A
50

y has [A;B] dydt has [dA/dt;dB/dt]


Courtesy of The MathWorks, Inc. Used with permission.

ODE Function: viewing results


To solve and plot the ODEs on the previous slide: [t,y]=ode45('chem',[0 0.5],[0 1]);
assumes that only chemical B exists initially

plot(t,y(:,1),'k','LineWidth',1.5); hold on; plot(t,y(:,2),'r','LineWidth',1.5); legend('A','B'); xlabel('Time (s)'); ylabel('Amount of chemical (g)'); title('Chem reaction');

ODE Function: viewing results


The code on the previous slide produces this figure
Chem reaction 1 0.9 0.8 Amount of chemical (g) 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 0 0.05 0.1 0.15 0.2 0.25 0.3 Time (s) 0.35 0.4 0.45 0.5 A B

Higher Order Equations


Must make into a system of first-order equations to use ODE solvers Nonlinear is OK! Pendulum example:
&& + g sin ( ) = 0 L && = & let = g sin ( ) L g sin ( ) L

& =

v x= v & dx = dt &
Courtesy of The MathWorks, Inc. Used with permission.

Plotting the Output


We can solve for the position and velocity of the pendulum: [t,x]=ode45('pendulum',[0 10],[0.9*pi 0]);
assume pendulum is almost vertical (at top)

plot(t,x(:,1)); hold on; plot(t,x(:,2),'r'); legend('Position','Velocity');


8 6 Position Velocity

Position in terms of angle (rad)

4 2 0 -2 -4 -6 -8

Velocity (m/s)

10

Plotting the Output


Or we can plot in the phase plane: plot(x(:,1),x(:,2)); xlabel('Position'); yLabel('Velocity');

The phase plane is just a plot of one variable versus the other:
8 6 4 2 Velocity 0 -2

Velocity is greatest when theta=0

Velocity=0 when theta is the greatest

-4 -6 -8 -3

-2

-1

0 Position

End of Part 3
(1) (2) (3) (4) (5) Linear Algebra Polynomials Optimization Differentiation/Integration Differential Equations
We're almost done!

What is Simulink?
A model-based equation solver Some analysis packages (ANSYS, Multisim) have built in equations modeling complex engineering problems.
Save lots of time Can only be used for tackling specific problems

Simulink lets you build a GUI-based model and simulates the result.
Unlimited complexity (constrained by runtime and memory) Adaptable for any field Downside? You have to do the modeling work

Getting Started
Create a new file Examine the Simulink Library Browser
Click on a library: Sources Drag a block into Simulink: Constant Visualize the block by going into Sinks Drag a Scope into Simulink

Connections
Click on the carat/arrow on the right of the constant box Drag the line to the scope
Youll get a hint saying you can quickly connect blocks by hitting Ctrl Connections between lines represent signals

Click the play button Double click on the scope.


This will open up a chart of the variable over the simulation time

Simulink Math
Everything is visual in Simulink! Click on the library Continuous
Drag the integrator block between the constant and the scope

Play and click on scope. What happens?


Simulink has a built in ODE solver The equation that represents your model is solved by Simulink Weve represented

Behind the curtain


Go to Simulation->Configuration Parameters at the top menu
See ode45? Change the solver type here

Courtesy of The MathWorks, Inc. Used with permission.

So whats going on?


The toolboxes Simulink provides you are full of modeling tools By selecting components that correspond to your model, you can design a simulation

Toolboxes
Math
Takes the signal and performs a math operation Add, subtract, round, multiply, gain, angle

Continuous
Adds differential equations to the system Integrals, Derivatives, Transfer Functions, State Space

Discontinuities
Adds nonlinearities to your system

Discrete
Simulates discrete difference equations Useful for digital systems

Building systems
Sources
Step input, white noise, custom input, sine wave, ramp input, Provides input to your system

Sinks
Scope: Outputs to plot simout: Outputs to a MATLAB vector on workspace MATLAB mat file

Modifying Blocks
Right click on the block, select the Parameters item corresponding to the item type Transfer Function: Numerator on first row Denominator on second row Summing Junction: List of signs determines inputs to junction Not shown: Sampling time row
Courtesy of The MathWorks, Inc. Used with permission.

Modifying Scopes
Within the scope: Autoscale fits the axes to the curve automatically Axes properties lets you customize the axes Changing the number of axes: Left click on icon
Courtesy of The MathWorks, Inc. Used with permission.

Courtesy of The MathWorks, Inc. Used with permission.

Change the number of axes field

Courtesy of The MathWorks, Inc. Used with permission.

First System
Drag a summing junction between the constant and integrator Change the signs to |+ Click on the open carat under the minus sign and connect it to the integrator output

Creating Subsystems
Drag a box around the parts of the subsystem Summing Junction Integrator Right click and select create subsystem Double click the subsystem: The parts are now inside Whats the system do when you run it?

Example Systems
ODE
d3y/dt3 + a*d2y/dt2 + b* dy/dt + c*y = F

Classical Control System

Courtesy of The MathWorks, Inc. Used with permission. Courtesy of The MathWorks, Inc. Used with permission.

Toolboxes
Simulink has many advanced toolboxes Control Systems Neural Networks Signal Processing SimMechanics Virtual Reality Real Time Hopefully youll get to use some of these powerful tools!

Das könnte Ihnen auch gefallen