Beruflich Dokumente
Kultur Dokumente
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.
6.094
Outline
(1) (2) (3) (4) Getting Started Making Variables Manipulating Variables Basic Plotting
Current directory
Workspace
Command Window
Command History
Customization
File Preferences
Allows you personalize your MATLAB experience
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)
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
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:
Column Vectors
Column vector: semicolon separated values between brackets column = [4;2;7;4]; Command window:
Workspace:
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
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.
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
[12 3 + [ 2 11 = [14 14
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
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!!
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
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
c=1:5
if increment isnt specified, default is 1
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)
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
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.
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!
10 x values:
1000 x values:
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
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]);
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)
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
Help file
Comments
Possible breakpoints
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
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
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
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
for
for loops: use for a definite number of iterations MATLAB syntax:
Loop variable
Command block
Is defined as a vector Is a scalar within the command block Does not have to have consecutive values
while
The while is like a more general for loop:
Don't need to know number of iterations
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
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
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.
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
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
Outline
(1) (2) (3) (4) (5) Linear Algebra Polynomials Optimization Differentiation/Integration Differential Equations
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
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
-0.2 -0.4
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
Outline
(1) (2) (3) (4) (5) Linear Algebra Polynomials Optimization Differentiation/Integration Differential Equations
Evaluate the derivative at a point and approximate by straight line Errors accumulate! Variable timestep can decrease the number of iterations
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
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
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');
& =
v x= v & dx = dt &
Courtesy of The MathWorks, Inc. Used with permission.
4 2 0 -2 -4 -6 -8
Velocity (m/s)
10
The phase plane is just a plot of one variable versus the other:
8 6 4 2 Velocity 0 -2
-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
Simulink Math
Everything is visual in Simulink! Click on the library Continuous
Drag the integrator block between the constant and the scope
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.
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
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!