Overview, MATLAB SyntaxComputation ProgrammingX=0X+3<0.1X=X+0.1Output xyesNo1+2=3sin(30)=0.5⎥⎦⎤⎢⎣⎡=⎥⎦⎤⎢⎣⎡⎥⎦⎤⎢⎣⎡221510743214321Cite as: Peter So, course materials for 2.003J / 1.053J Dynamics and Control I, Fall 2007. MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].MATLAB DesktopCommandWindowCommandHistoryWorkingDirectoryHelp ButtonWorkspace(variable list)Courtesy of The MathWorks, Inc. Used with permission.Cite as: Peter So, course materials for 2.003J / 1.053J Dynamics and Control I, Fall 2007. MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].MATLAB Help WindowInfoWindowBasicMATLAB functionsMATLABTool BoxesCite as: Peter So, course materials for 2.003J / 1.053J Dynamics and Control I, Fall 2007. MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY]. Courtesy of The MathWorks, Inc. Used with permission.MATLAB Data TypesCite as: Peter So, course materials for 2.003J / 1.053J Dynamics and Control I, Fall 2007. MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].MATLAB Data StructureEverything in MATLAB are matrix!A=5; A is a 1x1 matrixA=[ 1 3 4 5]; A is a 1x4 matrixA = [12 62 93 -8 22; 16 2 87 43 91; -4 17 -72 95 6];A is a 5x3 matrixNote: (1) Putting “;” behind a statement suppresses output(2) Rows in a matrix is separated by “;” inside(3) MATLAB command “whos” gives all the defined variablesWhat is a variable?Matrix indexing: A(2,3) = 87Cite as: Peter So, course materials for 2.003J / 1.053J Dynamics and Control I, Fall 2007. MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].Basic Linear Algebra⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡=⎥⎦⎤⎢⎣⎡−−−=⎥⎦⎤⎢⎣⎡==⋅⎥⎦⎤⎢⎣⎡=⎥⎦⎤⎢⎣⎡++++=⎥⎦⎤⎢⎣⎡⋅⎥⎦⎤⎢⎣⎡⎥⎦⎤⎢⎣⎡±±±±=⎥⎦⎤⎢⎣⎡±⎥⎦⎤⎢⎣⎡⎥⎦⎤⎢⎣⎡=⎥⎦⎤⎢⎣⎡⋅−−cbacbaabcdbcadAIAAdbcaAdhbgdfbechagcfaehfgedbcahdfbgcbahfgedbcamdmbmcmadbcam]'[1100111InverseScalar multiAdd/subMatrix multiTransposeCite as: Peter So, course materials for 2.003J / 1.053J Dynamics and Control I, Fall 2007. MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].MATLAB Operators – numeric [] [][][][][]35\.333.0200.0/.125*.11*!*8665,43,21 A =====+=⎥⎦⎤⎢⎣⎡==CACACABAbadCACACBCite as: Peter So, course materials for 2.003J / 1.053J Dynamics and Control I, Fall 2007. MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].MATLAB Operators – Relational, Logical RelationalLogicalCite as: Peter So, course materials for 2.003J / 1.053J Dynamics and Control I, Fall 2007. MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].One more MATLAB Operator – Sequence“:” is the sequence operator that denote a range[][][]⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡==⎥⎥⎦⎤⎢⎢⎣⎡==⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡====⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡=====963741[])2(:,987654:),3:2(852)2(:,654:),2(9876543211185211:3:254325:2AADADCACBABAAAAAIt is very usefulto create, decimate, andgenerate submatrixCite as: Peter So, course materials for 2.003J / 1.053J Dynamics and Control I, Fall 2007. MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].Basic Graphic Output in MATLABX =[1 2 3 4 5 6 7 8 9 10]Y =[1 4 9 16 25 36 49 64 81 100]plot(X,Y)1 2 3 4 5 6 7 8 9 100102030405060708090100Cite as: Peter So, course materials for 2.003J / 1.053J Dynamics and Control I, Fall 2007. MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].More Graphic Outputt=1:1:100;plot(t,cos(t/10));Cite as: Peter So, course materials for 2.003J / 1.053J Dynamics and Control I, Fall 2007. MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY]. Courtesy of The MathWorks, Inc. Used with permission.A couple more very useful graphic commands(1) hold on/hold off – determines whether the next plot command overwrites or not (2) figure – Creates new figure window(3) From the figure window, under “edit menu”, the “copy figure” option allows you to copy the figure to the clipbroad and then you can cut and paste it into other programs such as MSWord.Cite as: Peter So, course materials for 2.003J / 1.053J Dynamics and Control I, Fall 2007. MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].ProgrammingWhat is programming?Programming is the preparation of a step-by-step instruction for a computer to followWhen is programming “profitable”*repetitive computation*automation/real time control*reusable “code” – objectsProgramming languagesC, C++, C#, java, m-lab scriptCite as: Peter So, course materials for 2.003J / 1.053J Dynamics and Control I, Fall 2007. MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].Anatomy of a programFlow chart – a graphic representation of the logicalsequence of instructionsAlgorithm – a sequence of instructions designed tosolve a specific problemX+3<0.1DecisionX=0ActionStartTerminalCite as: Peter So, course materials for 2.003J / 1.053J Dynamics and Control I, Fall 2007. MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].ConditionalsConditional is a branching point in the program. Depending on specific condition, the program can takedifferent actions.Example: a simple program that add 1 to odd integer input and do nothing to even integer inputinput Xrem(X,2)==0X=X+1output xyesNoX=XstartendCite as: Peter So, course materials for 2.003J / 1.053J Dynamics and Control I, Fall 2007. MIT OpenCourseWare (http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].Programming in MATLABStep 1: Create a m-file (xxx.m)[MATLAB Menu: file->new]Step 2: Input sequence of MATLAB instructionsStep 3: Save (in working directory) and run [Editor Menu:debug->save & run]Cite as: Peter So, course materials for 2.003J / 1.053J Dynamics and Control I, Fall 2007. MIT
View Full Document