Lecture4 Matlab Loop
Lecture4 Matlab Loop
Instructor:
Danial Faghihi
The Institute for Computational Engineering and Science ACES 4.122, [email protected]
September 9, 2013
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
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
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
t = 0:0.02:50;! y = cos(t);!
Loops
The
while Structure.
repeatedly
executes
statements
in
a
loop
as
long
as
! !
x =! 5!
!
x =! 2!
!
x =! -1!
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!
! !
>> 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
passed function
!
fplot(func,[xmin,xmax])
!
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:
!