Вы находитесь на странице: 1из 27

Elements of

Matlab and Simulink


Lecture 1

Emanuele Ruffaldi

3rd April 2009

Copyright 2009,Emanuele Ruffaldi. This work is licensed under the Creative Commons Attribution-ShareAlike
License.
COURSE OVERVIEW
Objectives

• Have a practical grasp on MATLAB and Simulink


• Understand modeling and simulation of problems
• Use Simulink for understanding problems
• Exploit Simulink for solving problems
• Being able to use Simulink in Research and Work
Structure and Graduation

• 8-10 Lessons of 2-2.5 hours


• Theory presenting Modeling and Tool
• Exercises

• Website: http://percro.sssup.it/~pit/courses/simulink/

• Attending the course


• Final Project and Discussion
INTRODUCING MATLAB
Applications

• Where we can find MATLAB and Simulink?


• Which are success stories of MATLAB and Simulink?

Doheny Eye Nissan


NASA X-43A

PERCRO VR PERCRO BE DLR Robotics


History and Versions

• History (see this)


– Born as script system in 70s for accessing the original
FORTRAN LINPACK and EISPACK libraries by Cleve Moler
– Company founded in 1984 after contact with Stanford engineer
– Simulink introduced 1990
– Using LAPACK since 2000
• MATLAB Versioning
– Year based, Two per year
– This course uses R2008a
Alternatives

• GNU Octave http://www.gnu.org/software/octave/


– Script Language compatible with MATLAB
– Active Development
– Large number of modules
• SciLab http://www.scilab.org/
– Open Source developed by Digiteo (French research park)
– Simulink equivalent: Scicos
– Google Summer of Code Mentor http://code.google.com/soc/
MATLAB Structure

MATLAB GUI Simulink GUI

Java

Toolboxes Blocksets

M Engine Simulink

LAPACK MuPAD FFTW …


MATLAB in Simulink Pipeline

Pre Simulink Post

Data and Embedded Data Analysis


Model MATLAB
preparation subset
Visualization
MATLAB Interface

• MATLAB was and essentially is still a shell


• Workspace is the set of variables
• Basic commands:
– help
– doc Current Dir
– clear/clearvars/clear all
– !cmd
– edit
Workspace
– cd
Shell

History
Matrix
• Matrices are the fundamental type of data in MATLAB
– Dense or Sparse
– Different element types (usually double)
• Which is the meaning of Matrix Data?
– Linear Algebra Matrix
– Mapping
– Image
– Representation of Graphs
• Operators: + * / ^
• Transposition: „
• Element operators: .* .+
• Most of the functions accept matrices and vectors
– Operations on matrices are by columns
See also Matrix Cookbook http://matrixcookbook.com/

> Entering and Manipulating Matrix - lesson1_ex1


Programming MATLAB

• M-file language
– Dynamically Typed
– Scripted language
• Syntax:
– no {} as in C
– more similar to Pascal style
– if .. else .. elseif … end
– for .. end
– while .. end
– boolean operators: && ||
– bit operators: & |
Loop vs Indexed Operations

• When programming in MATLAB prefer indexed operations respect


loop operations
• Exploit: find, sum, min
• Exploit Vectorized logic

• Example Vector Normalization:


– vMag = sqrt(sum(v.ˆ2));
– v = v./vMag(ones(1,size(v,1)),:);

See Writing fast MATLAB Code


Types

• Matrix (with type double,int,int8 …)


• Strings
– Matrix of char
Matrix
– Variable length strings using Cell
Value
– String functions and Regular Expressions Struct
Type
• Cell Array
– Allows to contain any type Cell
– Useful for arrays of strings
• Structure Types Handle
– Allows to contain structured data (number)
– a = []
– a.field Object
Reference
– a.(name) Type
Java
– fieldnames(a)
Object
• Dictionary
– Not Native but based on Java Class java.util.Hashtable Function
Handle
– x.put(key,value) x.get(key) x.contains(x)
– key can be string/number and value can be anything
Functions

• Every file has a name as the main function


function r1,r2,r3 = name (p1, p2, p3)
% comment
return
• Sub-functions allowed
• Variable arguments
– nargin nargout
Function Handles

• Handles to functions (callbacks)


– functions are full types
– references to named functions using @ operator
– See Plot
• Passing Parameters to Callback
{@fx, e1, e2, e3}
When fx is called with p1 and p2 it receives also e1,e2,e3

> ODE – lesson1_exode


Differential Equations

• Set of differential Equations can be solved inside MATLAB


• Useful for offline computation or design setup
• Subset of Simulink power

• Define Options with odeset


• Run passing Derivative function, time and inputs
For interaction during ODE computation
Set OutputFcn callback
opts = odeset('OutputFcn', @solverOutputFcn);
Inside solverOutputFcn call “drawnow”
and stop simulation returning 1
MATLAB Scope

• Workspaces
– assignin(workspace,name,value)
– evalin(workspace,expression)
• Global to Functions
– global
• Persistent to Functions (C static)
– persistent name
Publishing Results

• Cell-mode development in M-file


– Mark Cells using %%
– Evaluate (CTRL+ENTER)
– Up/Down (CLTR+UP CTRL+DN)
• Publishing results
• Editing inside Word
– Notebook feature
– Define Block (ALT+D)
– Evaluation of code by blocks (ALT+ENTER)
Storing Data

• MAT files are the fundamental way of storing data


– Binary compressed copy of variables in the workspace
– Accessible from C using libmat
• Variety of data formats supported
– Image from Image Toolbox (imread)
– CSV (importdata) and XLS (xlsread)
– HF5D hierarchical data format
• Example of custom format
– fopen frlead fwrite
– loadbin

> Storing Data – lesson1_exdata


Basics of plotting

• MATLAB provides 2D and 3D plotting


• Plots concepts
– Handle to entity (get / set functions)
– Axis (limits vs auto-limit)
– Subplots (subplot)
– Exporting plots for publishing (EMF)
– Default Entities: gca (get axis) gcf (get figure)
• 3D Plot
– surf
– Also mesh based plots
• Important for Papers/Thesis
– Always put labels and units of measures in axis (xlabel,ylabel)
– Sometime adjust default font size
– xlabel,ylabel,title,legend
• Save Plots
– print -f1 -dFORMAT myfile.FORMAT (es. eps emf png jpg)
• Animation

> Plot Data – lesson1_explot


MATLAB 2D Plots
User Interfaces

• Create User Interface for Presenting Data


• Run guide for Visual Editor
• GUI made of a figure-file and an associated m-file
• Alternative
• EasyGUI package available for R2008a
• Automatic placement of entities
• Loop or Callback design

mygui = gui.autogui;
num = gui.slider(„Points:‟,[5 50]);
txt = gui.textmenu(„Color‟,{„r‟,‟b‟});
while mygui.waitForInput()
plot (num.Value,txt.Value)
end
Major Toolboxes

• MATLAB Toolboxes provide aggregate of functions


• Example of useful ones:
– Imaging Toolbox
– Control Toolbox
– Symbolic Toolbox
– Signal Processing
– Neural Network Toolbox
Symbolic Toolbox

• Manipulation of symbolic expressions


• Definition of symbols
– sym(„name‟)
– syms name1 name2 .. „real‟
• Analysis operations
– diff
• Resolution
– solve
– dsolve
• Replacing variables
– subs(expr, x, x+2)
– subs(expr, {x,t},{5,2})
• Plotting
– ezplot(fx,[-1,1])
– ezsurf
• Printing
– pretty
• Note: a symbolic variable is of a special type that overloads all the typical operators allowing to
perform all the operations as if it is effectively one of the fundamental matrix types
MATLAB Resources

• File Exchange
– http://www.mathworks.com/matlabcentral/fileexchange/
• MATLAB/Simulink Books (SSSA)

Вам также может понравиться