0% found this document useful (0 votes)
88 views

Lecture4 Matlab Loop

This document provides an overview of lecture 4 on structure programming in MATLAB given by instructor Danial Faghihi at The University of Texas at Austin. The key topics covered include: 1. Decision structures like if, elseif to execute code based on logical conditions being true. 2. Loop structures like for and while to repeatedly execute code. The document emphasizes vectorizing loops for improved efficiency. 3. Passing functions as arguments to other functions using anonymous inline functions and function handles. 4. Developing a function function to calculate average velocity and displacement of a bungee jumper by passing the velocity and displacement functions and integrating over a time range.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Lecture4 Matlab Loop

This document provides an overview of lecture 4 on structure programming in MATLAB given by instructor Danial Faghihi at The University of Texas at Austin. The key topics covered include: 1. Decision structures like if, elseif to execute code based on logical conditions being true. 2. Loop structures like for and while to repeatedly execute code. The document emphasizes vectorizing loops for improved efficiency. 3. Passing functions as arguments to other functions using anonymous inline functions and function handles. 4. Developing a function function to calculate average velocity and displacement of a bungee jumper by passing the velocity and displacement functions and integrating over a time range.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

THE UNIVERSITY OF TEXAS AT AUSTIN DEPARTMENT OF AEROSPACE ENGINEERING AND ENGINEERING MECHANICS

ASE 311 ENGINEERING COMPUTATION


FALL 2013

Instructor:

Danial Faghihi
The Institute for Computational Engineering and Science ACES 4.122, [email protected]

September 9, 2013

Lecture 4: Structure Programming in MATLAB

Decisions (Selec,on): related func,ons


The error Func,on. This func,on is used to trap errors. When this func,on is

encountered, it displays the text message msg, indicates where the error occurred, and causes the M-le to terminate and return to the command window. error (msg)!

!
The nargin Func,on. provides the number of input arguments supplied to a

func,on by a user. It is used to make the code more user friendly.

qExample: freefall_nargin.m

Decisions (Selec,on)
The if Structure. This structure allows you to execute a set of statements if a logical condi,on is true
if expression! statements! end!
!

MATLAB allows two forms for expression: 1. expression1 rop expression2 where rop is ==, <, >, <=, >=, or ~=! 2. expression1 rop expression2 where rop is & or |!
3.

~expr!

!~ (Not). negation on an expression. ~expression! If the expression is true, the result is false and vise versa. & (And). logical conjunction on two expressions. expression1 & expression2! If both expressions evaluate to true, the result is true. If either or both expressions evaluates to false, the result is false. | (Or). logical disjunction on two expressions. expression1 | expression2 If either or both expressions evaluate to true, the result is true.!

Decisions (Selec,on)
The if...elseif Structure. This structure allows you to execute a set of

statements if a logical condi,on is true and to execute a second set if the condi,on is false

if expression1! statements1! elseif expression2! statements2! elseif expression3! statements3! !.! !.! !.! end!
!

qExample: mysign.m

Loops
loops perform opera,ons repe,,vely. depending on how the repe,,ons are terminated, there are two types of loops: 1. for loop ends aGer a specied number 2. while loop ends on the basis of a logical condi,on
The for...end Structure. is a repe,,on statement providing a loop for

automa,c itera,on over a range of numbers or objects.

for index = start : step-width : finish! statements! end! for index from start to finish step step! width do statements! !
!

Example:
for i = 1 : 0.5 : 10! disp(i)! end!

!
Example: Develop an M-le, factor.m to compute the factorial!

Loops
Vectoriza,on instead of for loop in MATLAB

i = 0;! for t = 0:0.02:50! i = i + 1;! y(i) = cos(t);! end!

t = 0:0.02:50;! y = cos(t);!

Loops
The while Structure. repeatedly executes statements in a loop as long as

condi8on remains true.

while condition! statements! end!

! !

x = 8! ! while x > 0! x = x 3! end!

x =! 5!
!

x =! 2!
!

x =! -1!

The while...break Structure. where break terminates execu,on of the loop.

Thus, a single line if is used to exit the loop if the condi,on tests true. Note that as shown, the break can be placed in the middle of the loop (i.e., with statements before and aGer it). x =! x = 8! while (1)! 5! ! statements! while (1)! x =! if condition, break, end! 2! if x < 0, break, end! statements! x = x 3! x =! end! end! -1!
! !

PASSING FUNCTIONS TO M-FILES


Anonymous (inline) Func,ons
Anonymous func8ons allow you to create a simple func,on without crea,ng an M- le.

function_name = @(argument_list) expression! function_name = inline(expression, arg1, arg2, )!


!

>> f1=@(x,y) x^2 + y^2;! >> f1(3,4)! ans =! 25! -----------------! >> a = 4;! >> b = 2;! >> f2=@(x) a*x^b;! >> f2(3)! ans = 36!

>> g = inline('sin(alpha*x)','x','alpha)! g =! Inline function:! g(x,alpha) = sin(alpha*x)! ! >> g(1,pi/3)! ans =! 0.866

PASSING FUNCTIONS TO M-FILES


Func,on func,on
Func8on func8ons are func,ons that operate on other func,ons which are passed to it as input arguments.

passed function
!

fplot(func,[xmin,xmax])
!

fplot is a built-in function, which plots the graphs of functions

Example: plot the velocity of the free-falling bungee jumper from t = 0 to 12


>> vel=@(t) sqrt(9.81*68.1/0.25)*tanh(sqrt(9.81*0.25/68.1)*t);! >> fplot(vel,[0 12])!

PASSING FUNCTIONS TO M-FILES


Exercise:
Develop an M-le Func,on func,on to determine the average value of velocity and displacement of bungee jumper over a range.

1. Average func,on: write a func,on favg = funcave(f,a,b,n) that average the y = f(x) over x = linspace (a,b,n)!

2. bungee jumper: write a func,on [v_avg, y_avg] = freefall_avg(tmin,tmax,nt, m, cd) to calculate v and y as follow

3. Call funcave in the freefall_avg to determine the average velocity, v_avg, and displacement, y_avg.

4. Use the freefall_func to calculate the average velocity and average displacement for:
!

t = linspace (tmin, tmax, nt) = linspace (0,12,13)!

You might also like