AE202 Lecture 2 Matlab Programming

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

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

View Full Document Right Arrow Icon
AE202 Aerospace Flight Mechanics Lecture 2: Programming in MATLAB
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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   
Background image of page 3

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

View Full DocumentRight Arrow Icon
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.
Background image of page 4
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) )
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
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) )
Background image of page 7

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

View Full DocumentRight Arrow Icon
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) )
Background image of page 8
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.
Background image of page 9

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

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online