10.07.2015 Views

Elements of MATLAB and Simulink - Lecture 1 - Percro

Elements of MATLAB and Simulink - Lecture 1 - Percro

Elements of MATLAB and Simulink - Lecture 1 - Percro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

COURSE OVERVIEW


Applications• Where we can find <strong>MATLAB</strong> <strong>and</strong> <strong>Simulink</strong>?• Which are success stories <strong>of</strong> <strong>MATLAB</strong> <strong>and</strong> <strong>Simulink</strong>?NASA X-43ADoheny EyeNissanPERCRO VRPERCRO BEDLR Robotics


History <strong>and</strong> Versions• History (see this)– Born as script system in 70s for accessing the originalFORTRAN LINPACK <strong>and</strong> EISPACK libraries by Cleve Moler– Company founded in 1984 after contact with Stanford engineer– <strong>Simulink</strong> introduced 1990– Using LAPACK since 2000• <strong>MATLAB</strong> Versioning– Year based, Two per year– This course uses R2008a


Alternatives• GNU Octave http://www.gnu.org/s<strong>of</strong>tware/octave/– Script Language compatible with <strong>MATLAB</strong>– Active Development– Large number <strong>of</strong> modules• SciLab http://www.scilab.org/– Open Source developed by Digiteo (French research park)– <strong>Simulink</strong> equivalent: Scicos– Google Summer <strong>of</strong> Code Mentor http://code.google.com/soc/


<strong>MATLAB</strong> Structure<strong>MATLAB</strong> GUI<strong>Simulink</strong> GUIJavaToolboxesBlocksetsM Engine<strong>Simulink</strong>LAPACKMuPADFFTW…


<strong>MATLAB</strong> in <strong>Simulink</strong> PipelinePre<strong>Simulink</strong>PostData <strong>and</strong>ModelpreparationEmbedded<strong>MATLAB</strong>subsetData AnalysisVisualization


<strong>MATLAB</strong> Interface• <strong>MATLAB</strong> was <strong>and</strong> essentially is still a shell• Workspace is the set <strong>of</strong> variables• Basic comm<strong>and</strong>s:– help– doc– clear/clearvars/clear all– !cmd– editWorkspace– cdCurrent DirShellHistory


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


Programming <strong>MATLAB</strong>• 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 <strong>MATLAB</strong> prefer indexed operations respectloop 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 <strong>MATLAB</strong> Code


Types• Matrix (with type double,int,int8 …)• Strings– Matrix <strong>of</strong> char– Variable length strings using Cell– String functions <strong>and</strong> Regular Expressions• Cell Array– Allows to contain any type– Useful for arrays <strong>of</strong> strings• Structure– Allows to contain structured data– a = []– a.field– a.(name)– fieldnames(a)• Dictionary– Not Native but based on Java Class java.util.Hashtable– x.put(key,value) x.get(key) x.contains(x)– key can be string/number <strong>and</strong> value can be anythingTypesValueTypeReferenceTypeMatrixStructCellH<strong>and</strong>le(number)ObjectJavaObjectFunctionH<strong>and</strong>le


Functions• Every file has a name as the main functionfunction r1,r2,r3 = name (p1, p2, p3)% commentreturn• Sub-functions allowed• Variable arguments– nargin nargout


Function H<strong>and</strong>les• H<strong>and</strong>les 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 <strong>and</strong> p2 it receives also e1,e2,e3> ODE – lesson1_exode


Differential Equations• Set <strong>of</strong> differential Equations can be solved inside <strong>MATLAB</strong>• Useful for <strong>of</strong>fline computation or design setup• Subset <strong>of</strong> <strong>Simulink</strong> power• Define Options with odeset• Run passing Derivative function, time <strong>and</strong> inputsFor interaction during ODE computationSet OutputFcn callbackopts = odeset('OutputFcn', @solverOutputFcn);Inside solverOutputFcn call “drawnow”<strong>and</strong> stop simulation returning 1


<strong>MATLAB</strong> 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 <strong>of</strong> code by blocks (ALT+ENTER)


Storing Data• MAT files are the fundamental way <strong>of</strong> storing data– Binary compressed copy <strong>of</strong> variables in the workspace– Accessible from C using libmat• Variety <strong>of</strong> data formats supported– Image from Image Toolbox (imread)– CSV (importdata) <strong>and</strong> XLS (xlsread)– HF5D hierarchical data format• Example <strong>of</strong> custom format– fopen frlead fwrite– loadbin> Storing Data – lesson1_exdata


Basics <strong>of</strong> plotting• <strong>MATLAB</strong> provides 2D <strong>and</strong> 3D plotting• Plots concepts– H<strong>and</strong>le 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 <strong>and</strong> units <strong>of</strong> 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


<strong>MATLAB</strong> 2D Plots


User Interfaces• Create User Interface for Presenting Data• Run guide for Visual Editor• GUI made <strong>of</strong> a figure-file <strong>and</strong> an associated m-file• Alternative• EasyGUI package available for R2008a• Automatic placement <strong>of</strong> entities• Loop or Callback designmygui = 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• <strong>MATLAB</strong> Toolboxes provide aggregate <strong>of</strong> functions• Example <strong>of</strong> useful ones:– Imaging Toolbox– Control Toolbox– Symbolic Toolbox– Signal Processing– Neural Network Toolbox


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


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

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!