{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

AE202 Lecture 2 Matlab Programming

AE202 Lecture 2 Matlab Programming - AE202 Aerospace Flight...

This preview shows pages 1–10. Sign up to view the full content.

AE202 Aerospace Flight Mechanics Lecture 2: Programming in MATLAB

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Repetition May want to repeat certain operations multiple times Use loops In general want to avoid loops in MATLAB, using vectorization is much faster. Two basic kinds of loops for Loop over a definite number of iterations while Loop until a condition is met
General for construct index must be a variable If for k = first:increment:last The number of times the loop is executed is given by: floor(x) round x towards - e.g. for i = -1:-2:-8 has an iteration count of 4 i takes the values -1, -3, -5 and -7 for index = j : k statements end or for index = j : m : k statements end 1 last first floor increment

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
General for construct On completion of the for loop the index contains the last value used. If the vector j : k or j : m : k is empty, statements are not executed and control passes to the statement following end. The index is a counter. It does not have to appear explicitly in statements. It is good practice to indent the statements in the for loop. Can use smart-indenting in the MATLAB m-file editor Most general form for index = v where v can be any vector useful for processing lists If the index does appear explicitly in statements can often vectorize the for loop, which is much more efficient.
Example: Newton’s method (for loop) Use Newton’s method to find the square root of a number a = 2; x = a/2; disp ([’The approach to sqrt (a) for a = ’, num2str(a)]) for i = 1:6 x = (x + a / x) / 2; disp( x ) end disp ( ’ Matlab ’’s value: ’ ) disp( sqrt(2) )

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Relational Operators MATLAB gives a value of 1 to a relational expression that is true and 0 to one that is false . Can assign the results of relational operators to variables Be very careful using == with non-integers Operator Meaning < Less than <= Less than or equal == Equal ~= Not equal to > Greater than >= Greater than or equal
Example: Newton’s method (while loop) Use Newton’s method to find the square root of a number with a while loop a = 2; x = a/2; numIter=6; currIter=1; disp ([’The approach to sqrt (a) for a = ’, num2str(a)]) while currIter<=numIter x = (x + a / x) / 2; disp( x ) currIter = currIter+1; end disp ( ’ Matlab ’’s value: ’ ) disp( sqrt(2) )

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Example: Newton’s method (while loop) Use Newton’s method to find the square root of a number to a certain tolerance a = 2; x = a/2; tol=1e-10; disp ([’The approach to sqrt (a) for a = ’, num2str(a)]) while abs(x-sqrt(a))>tol x = (x + a / x) / 2; disp( x ) end disp ( ’ Matlab ’’s value: ’ ) disp( sqrt(2) )
if statements Basis for decision making in any programming language Condition is usually a logical expression (contains a relational operator) Can be an arithmetic operation Condition is false if operation evaluates to zero Condition is true if operation evaluates to any other value Not recommended. Code is more readable if condition if a logical expression.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 40

AE202 Lecture 2 Matlab Programming - AE202 Aerospace Flight...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online