Unformatted text preview:

Cite as: Peter So, course materials for 2.003J/1.053J Dynamics and Control I, Spring 2007. MIT OpenCourseWare(http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].MatLab & ProgrammingProgrammingX=0X+3<0.1X=X-0.1Output xyesNoCite as: Peter So, course materials for 2.003J/1.053J Dynamics and Control I, Spring 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, Spring 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, Spring 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, Spring 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, Spring 2007. MIT OpenCourseWare(http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].x=input('input integer: ');if (rem(x,2) == 0)x=x;elsex=x+1;endxinput Xrem(X,2)==0X=X+1output xyesNoX=XstartendMatLab realization of programCite as: Peter So, course materials for 2.003J/1.053J Dynamics and Control I, Spring 2007. MIT OpenCourseWare(http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].Conditional: If, else, endif logic conditionaction1;action1;elseaction2;action2;endCheck out also elseifCite as: Peter So, course materials for 2.003J/1.053J Dynamics and Control I, Spring 2007. MIT OpenCourseWare(http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].RepetitionExample: fill a 1-D matrix A with length 10 with 2s.Create Ai>10A(i)=2;YesNoi=1;startendi=i+1;Cite as: Peter So, course materials for 2.003J/1.053J Dynamics and Control I, Spring 2007. MIT OpenCourseWare(http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].A=zeros(10,1);for i=1:10A(i)=2;endCreate Ai>10A(i)=2;yesnoi=1;startendi=i+1;Repetition: for loopfor start/end conditionaction1;action1;action1;endend conditionalloop variable updatestart:loop variableinitiationCite as: Peter So, course materials for 2.003J/1.053J Dynamics and Control I, Spring 2007. MIT OpenCourseWare(http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].More Conditionals – elseifif logic conditionaction1;action1;elseaction2;action2;endif logic condition 1action1;action1;else if logic condition 2action2;action2;else if logic condition 3action3;action3;elseaction4;action4;endCite as: Peter So, course materials for 2.003J/1.053J Dynamics and Control I, Spring 2007. MIT OpenCourseWare(http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].More Conditionals – switchswitch variablecase var1action1;action1;case var2action2;action2;case var3action3;action3;otherwiseaction4;action4;endCite as: Peter So, course materials for 2.003J/1.053J Dynamics and Control I, Spring 2007. MIT OpenCourseWare(http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].Switch -- examplesa=2;switch acase 1disp('1')case {2; 3; 4}disp('2 or 3 or 4')case 5disp('5')otherwisedisp('something else')enda='M';switch acase 'a'disp('A')case {'b'; 'c'; 'd'}disp('B')case 'M'disp('m')otherwisedisp('something else')endCite as: Peter So, course materials for 2.003J/1.053J Dynamics and Control I, Spring 2007. MIT OpenCourseWare(http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].Conditionals – if or switchWhen should we use “if-elseif-else-end”or “switch-case-otherwise-end”?There are no fix rules … whatever makes the inherent logic clearer to the programmer and the reader“if” is more binary decision process while “case” ismore tree-like“if”“case”Cite as: Peter So, course materials for 2.003J/1.053J Dynamics and Control I, Spring 2007. MIT OpenCourseWare(http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].Nesting – layers and layersa='M';switch acase 'a'disp('A')case {'b'; 'c'}switch acase {'b'}disp('B')case {'c'}disp('C')end;otherwisedisp('something else')end;a=3;if rem(a,2) ~= 0a=a+1;elseif a== 0a=a-1;elsea=a+2;end;end;disp(a)Cite as: Peter So, course materials for 2.003J/1.053J Dynamics and Control I, Spring 2007. MIT OpenCourseWare(http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].Loops: more for loopsfor start/end conditionaction1;action1;action1;endfor a=1:5disp(a);endfor a=1:2:5disp(a);endOutput: 1, 2, 3, 4, 5Output: 1, 3, 5for a=1:-2.5:-5disp(a);endOutput: 1, -1.5,-4for a=-10:-2.5:-5disp(a);endOutput:Ending condition is testedat the “for” statementCite as: Peter So, course materials for 2.003J/1.053J Dynamics and Control I, Spring 2007. MIT OpenCourseWare(http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].Nesting “For” loopsfor start/end condition1action1;action1;for start/end condition2action2;action2;end;end;Cite as: Peter So, course materials for 2.003J/1.053J Dynamics and Control I, Spring 2007. MIT OpenCourseWare(http://ocw.mit.edu), Massachusetts Institute of Technology. Downloaded on


View Full Document

MIT 2 003 - MatLab & Programming

Download MatLab & Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view MatLab & Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view MatLab & Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?