Download - Matlab Ch4
-
7/18/2019 Matlab Ch4
1/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
Matlab Programming for EngineersMatlab Pr
ogramming for Engineers
Dr. Nidal Farhat
Introduction to MatlabIntroduction to Matlab
Matlab BasicsMatlab Basics
BranchingBranchingStatementsStatementsLoopsLoops
User Defined FunctionsUser Defined Functions
Additional Data TpesAdditional Data Tpes
Input!"utput FunctionsInput!"utput Functions
Simulin# Toolbo$Simulin# Toolbo$
Important Toolbo$es %if time is a&ailable'Important Toolbo$es %if time is a&ailable'
-
7/18/2019 Matlab Ch4
2/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
OBJECTIVES
The whileLoop
The forLoop
Logical Arra! a"# $ec%ori&a%io"
The Ma%la' Profiler
-
7/18/2019 Matlab Ch4
3/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE whileLOOP
S%r(c%(re:The Code Block is repeateduntil the expression is false.
If it is false, the program
executes the first statement
after the end.
-
7/18/2019 Matlab Ch4
4/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE whileLOOP
E)a*ple:
+ri%e a progra* %ha% calc(la%e! %he a,erage-
a"# %he !%a"#ar# #e,ia%io"-
for a !e% of ,al(e! ./0- e"%ere# ' %he (!er1
-
7/18/2019 Matlab Ch4
5/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE whileLOOP
Sol(%io":
-
7/18/2019 Matlab Ch4
6/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE whileLOOP
Sol(%io" .*o#ifie#0:
-
7/18/2019 Matlab Ch4
7/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE whileLOOP
Sol(%io" .*o#ifie#0:
-
7/18/2019 Matlab Ch4
8/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE forLOOP
S%r(c%(re:
Repea%! a 'loc2 of !%a%e*e"%! .Bo#0 !pecifie# "(*'er of %i*e!1
Legal E)a*ple!:
Matlab generates an array by
evaluating this expression
column by column.
-
7/18/2019 Matlab Ch4
9/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE forLOOP
Legal E)a*ple!:
-
7/18/2019 Matlab Ch4
10/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE forLOOP
E)a*ple .The Fac%orial F("c%io"0:
N3 N 5 .N670 5 .N680 5 9 5 5 8 5 7
-
7/18/2019 Matlab Ch4
11/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE forLOOP
E)a*ple .The Fac%orial F("c%io"0:
N3 N 5 .N670 5 .N680 5 9 5 5 8 5 7
-
7/18/2019 Matlab Ch4
12/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE forLOOP
E)a*ple- calc(la%i"g %he #a of ear:
-
7/18/2019 Matlab Ch4
13/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE forLOOP
E)a*ple:
;!e THE for LOOP %o calc(la%e %he a,erage a"# !%a"#ar# #e,ia%io" for
a" ."0 ,al(e! .*o#if %he pre,io(! while progra*01
-
7/18/2019 Matlab Ch4
14/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE forLOOP
-
7/18/2019 Matlab Ch4
15/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE forLOOP
-
7/18/2019 Matlab Ch4
16/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE forLOOP
The 'rea2 a"# co"%i"(e S%a%e*e"%!:
The 'rea2!%a%e*e"% %er*i"a%e! %he e)ec(%io" of a loop a"# pa!!e!
co"%rol %o %he "e)% !%a%e*e"% af%er %he e"# of %he loop- e)a*ple:
-
7/18/2019 Matlab Ch4
17/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE forLOOP
The 'rea2 a"# co"%i"(e S%a%e*e"%!:
The co"%i"(e!%a%e*e"% %er*i"a%e! %he c(rre"% !%ep of %he loop a"#
re%(r" %he co"%rol %o %he %op of %he loop- e)a*ple:
co"%i"(e
-
7/18/2019 Matlab Ch4
18/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE forLOOP
Ne!%i"g Loop!:
O(%p(%
ii jj product
-
7/18/2019 Matlab Ch4
19/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE forLOOP
Ne!%i"g Loop!:
O(%p(%
ii jj product
-
7/18/2019 Matlab Ch4
20/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE forLOOP
Ne!%i"g Loop!:
The 'rea2Dco"%i"(e !%a%e*e"%! %he appl %o %he c(rre"% loop- e)a*ple:
-
7/18/2019 Matlab Ch4
21/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE forLOOP
Logical arra a"# ,ec%ori&a%io":
-
7/18/2019 Matlab Ch4
22/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE forLOOP
Logical arra a"# ,ec%ori&a%io":
Logical arra! ca" !er,e a! a maskfor ari%h*e%ic opera%io"!1 The
!pecifie# opera%io" will be applied to the selected elementsa"# "o% %o
%he re*ai"i"g ele*e"%!- e)a*ple:
+ill %a2e %he !(are roo% of all ele*e"%!
for which %he logical arra ' i! %r(e a"#
lea,e all %he o%her ele*e"%! i" %he arra
("cha"ge#
MATLAB PROGRAMMING FOR ENGINEERS
-
7/18/2019 Matlab Ch4
23/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE forLOOP
Logical arra a"# ,ec%ori&a%io":
E)a*ple 7:
Calc(la%e %he !r% of all %he ele*e"%! of arra .a0 1
Fa!%er
:MATLAB PROGRAMMING FOR ENGINEERS
-
7/18/2019 Matlab Ch4
24/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE forLOOP
Logical arra a"# ,ec%ori&a%io":
E)a*ple 8: Calc(la%e %he !r% of all %he ele*e"%! of arra .a0 - a"# %he
!(are of %he re*ai"i"g .("!elec%e# ' %he pre,io(! opera%io"01
:MATLAB PROGRAMMING FOR ENGINEERS
-
7/18/2019 Matlab Ch4
25/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE forLOOP
The Ma%la' Profiler:
;!e# %o i#e"%if %he par%! of %he progra* %ha% co"!(*e *o!% e)ec(%io"
%i*e1
:MATLAB PROGRAMMING FOR ENGINEERS
-
7/18/2019 Matlab Ch4
26/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE forLOOP
The Ma%la' Profiler:
:MATLAB PROGRAMMING FOR ENGINEERS
-
7/18/2019 Matlab Ch4
27/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE forLOOP
E)a*ple:.Proec%ile Mo%io"0
Ge"eral e(a%io":
+he" %he 'all hi%! %he gro("#0
2*
0,y
v
t tg
= =
0
0
210 2
0
( )
( )
y
x
y t y v t gt
x t x v t
= +
= +
0
0
0
0
sin( )
cos( )
y
x
v v
v v
=
=
:MATLAB PROGRAMMING FOR ENGINEERS
-
7/18/2019 Matlab Ch4
28/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE forLOOP
E)a*ple:.Proec%ile Mo%io"0
Ge"eral e(a%io":
+he" %he 'all hi%! %he gro("#0
2*
0,y
v
t t g= =
0
0
210 2
0
( )
( )
y
x
y t y v t gt
x t x v t
= +
= +
0
0
0
0
sin( )
cos( )
y
x
v v
v v
=
=
:MATLAB PROGRAMMING FOR ENGINEERS
-
7/18/2019 Matlab Ch4
29/30
:
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE forLOOP
E)a*ple:.Co**a"#! a"# F("c%io"!0
:MATLAB PROGRAMMING FOR ENGINEERS
-
7/18/2019 Matlab Ch4
30/30
:MATLAB PROGRAMMING FOR ENGINEERS
Home Work
Sol,e %he followi"g pro'le*!:
417- 7J- 87