matlab introduction v2
DESCRIPTION
Full Introduction to MatlabTRANSCRIPT
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 1/37
Introduction to MATLAB
Michael Schwedler
Institut f ur Strukturmechanik
Bauhaus-Universitat WeimarWeimar, Germany
Finite Element Methods and Structural Dynamics17. Oktober 2011
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 2/37
basics control structures
Outline
1 basics
2 control structures
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 2 / 27
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 3/37
basics control structures
it’s a calculator...
MATLAB
1 7+32 21∗43 6/74 7 . 2 −11. 375 3ˆ26 s q r t ( 1 6 )7 x = 3 . 248 y = 7 . 459 z=x+y ;
10 z11 x=x+2;12 z13 z=x+y
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 3 / 27
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 4/37
basics control structures
input - fundamental data types
In general
Name Size Range Remark
boolean 1 byte true or false
integer 4 bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
float 4 bytes −3.4 ∗ 1038 to 3.4 ∗ 1038 (∼7 digits) single precisiondouble 8 bytes −1.7 ∗ 10308 to 1.7 ∗ 10308 (∼15 digits) double precision
char 2 bytes 65536 characters 16 bit Unicode
MATLAB - All numbers stored as double
1 35500/1132 f o r m a t l on g e3 a n s4 f o r m a t s h o r t ; f o r m a t l o n g ; f o r m a t s h o r t e ; f o r m a t l o n g e ;
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 4 / 27
b i l
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 5/37
basics control structures
input - fundamental data types
In general
Name Size Range Remarkboolean 1 byte true or false
integer 4 bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
float 4 bytes −3.4 ∗ 1038 to 3.4 ∗ 1038 (∼7 digits) single precisiondouble 8 bytes −1.7 ∗ 10308 to 1.7 ∗ 10308 (∼15 digits) double precision
char 2 bytes 65536 characters 16 bit Unicode
MATLAB - All numbers stored as double
1 35500/1132 f o r m a t l on g e3 a n s4 f o r m a t s h o r t ; f o r m a t l o n g ; f o r m a t s h o r t e ; f o r m a t l o n g e ;
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 4 / 27
b si s nt l st t s
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 6/37
basics control structures
input - matrices
MATLAB - matrix with dimension 1 x 11 n=22 s i z e ( n )
MATLAB - vectors
1 n =[2 3 .7 4 2 . 1 ]2 s i z e ( n )3
4
n = [ 2 , 3 . 7 , 4 , 2 . 1 ]5 s i z e ( n )6
7 n = [ 2 ; 3 . 7 ; 4 ; 2 . 1 ]8 s i z e ( n )
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 5 / 27
basics control structures
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 7/37
basics control structures
input - matrices
MATLAB - matrix with dimension 1 x 11 n=22 s i z e ( n )
MATLAB - vectors
1 n =[2 3 .7 4 2 . 1 ]2 s i z e ( n )3
4
n = [ 2 , 3 . 7 , 4 , 2 . 1 ]5 s i z e ( n )6
7 n = [ 2 ; 3 . 7 ; 4 ; 2 . 1 ]8 s i z e ( n )
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 5 / 27
basics control structures
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 8/37
basics control structures
input - matrices
MATLAB - matrices
1 c l e a r2 M=[2 7 5 . 4 6 . 6 ; 2 2 1 . 0 1 2 . 1 ; 3 . 3 2 . 9 4 6 . 9 ]3 M(2 , 3)4 M( 2 , : )5
6 P( 2 , 7) =45.8
MATLAB - turning a vector into a matrix
1 n =[2 3 .7 4 2 . 1 ]2 n ( 2 , : ) =[4 5 . 5 6 2 . 1 ]3 s i z e ( n )
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 6 / 27
basics control structures
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 9/37
basics control structures
input - matrices
MATLAB - matrices
1 c l e a r2 M=[2 7 5 . 4 6 . 6 ; 2 2 1 . 0 1 2 . 1 ; 3 . 3 2 . 9 4 6 . 9 ]3 M(2 , 3)4 M( 2 , : )5
6 P( 2 , 7) =45.8
MATLAB - turning a vector into a matrix
1 n =[2 3 .7 4 2 . 1 ]2 n ( 2 , : ) =[4 5 . 5 6 2 . 1 ]3 s i z e ( n )
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 6 / 27
basics control structures
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 10/37
input - matrices - special initialization
MATLAB - built in functions
1 a=z e r o s ( 5 , 6 )2 b=e y e ( 5 )3 c=r a n d ( 4 , 6 )
4 d=z e r o s ( s i z e ( c ) )
MATLAB - first:increment:last
1 x = 1 : 5 : 3 52 x= [ 1 : 5 : 3 5 ] ’3 y =[x ’ ; x ’ ]
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 7 / 27
basics control structures
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 11/37
input - matrices - special initialization
MATLAB - built in functions
1 a=z e r o s ( 5 , 6 )2 b=e y e ( 5 )3 c=r a n d ( 4 , 6 )
4 d=z e r o s ( s i z e ( c ) )
MATLAB - first:increment:last
1 x = 1 : 5 : 3 52 x= [ 1 : 5 : 3 5 ] ’3 y =[x ’ ; x ’ ]
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 7 / 27
basics control structures
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 12/37
input
MATLAB - strings
1 s= ’ T h i s i s a s t r i n g ’2 t= ’Don ’ ’ t wor ry ’3
s (1 )=t ( 1 ) ;4 s
MATLAB - input from keyboard
1 n=i n p u t ( ’ E n t e r a v al u e f o r n : ’ )
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 8 / 27
basics control structures
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 13/37
input
MATLAB - strings
1 s= ’ T h i s i s a s t r i n g ’2 t= ’Don ’ ’ t wor ry ’3
s (1 )=t ( 1 ) ;4 s
MATLAB - input from keyboard
1 n=i n p u t ( ’ E n t e r a v al u e f o r n : ’ )
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 8 / 27
basics control structures
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 14/37
calculating
MATLAB1 A = [ 1 , 2 , 3 ; 4 , 5 , 6 ; 7 , 8 , 9 ]2 B = [ 3 , 2 , 1 ; 6 , 4 , 5 ; 8 , 7 , 9 ]3 C = [ 1 ; 2 ; 3 ]
MATLAB - matrix arithmetic
1
2
3 A∗B4 A/B % A ∗ i n v (B)5 A\B % i n v ( A) ∗ B 6 Aˆ27 A∗C
MATLAB - elementwise
1 A+B2 A−B3 A .∗B4 A . / B %A( i , j ) : B( i , J )5 A .\B %B( i , j ) : A( i , J )6 A . ˆ 27 A .∗C
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 9 / 27
basics control structures
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 15/37
calculation hierachy
distance = 0.5 × accel × time ˆ 2distance = 0.5 × accel × ( time ˆ 2 )distance = ( 0.5 × accel × time ) ˆ 2
Hierachy of arithmetic
Precedence Operation
1 The contents of all parentheses are evaluated, starting fromthe innermost parentheses and working outward.
2 All exponentials are evaluated, working from left to right.3 All multiplications and divisions are evaluated, working from
left to right.4 All additions and subtractions are evaluated, working from left
to right.
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 10 / 27
basics control structures
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 16/37
calculation hierachy
distance = 0.5 × accel × time ˆ 2distance = 0.5 × accel × ( time ˆ 2 )distance = ( 0.5 × accel × time ) ˆ 2
Hierachy of arithmetic
Precedence Operation
1 The contents of all parentheses are evaluated, starting fromthe innermost parentheses and working outward.
2 All exponentials are evaluated, working from left to right.3 All multiplications and divisions are evaluated, working from
left to right.4 All additions and subtractions are evaluated, working from left
to right.
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 10 / 27
basics control structures
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 17/37
functions
Functions are subprograms stored in text files with .m extension.
Input parameters are passed to the function by an input argument list.Results are returned to the caller by an output argument list.Anything that happes within the function is invisible to the caller.In order to call a function, the .m-file must be stored within the path of MATLAB.The function name must be equal to the file name.
MATLAB
1 f u n c t i o n [ outArg1 , outArg2 , . . . ] = fname ( inA rg1 , inA rg2 , . . . )2 % H1 comment l i n e 3
% o t h er comment l i n e s 4
5 MATLAB c od e6
7 r e t u r n
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 11 / 27
basics control structures
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 18/37
exercise
Function: dotProduct3
Write a function named dotProduct3 that calculates the dot product of twovectors a and b , , each with length = 3.
a · b = | a| | b | cos( a, b )
Function: crossProduct3
Write a function named crossProduct3 that calculates the cross product of twovectors a and b , each with length = 3.
a × b = (| a| | b | sin θ) n
a · b = a1 b 1 + a2 b 2 + a3 b 3 a × b =
a1
a2
a3
×
b 1b 2b 3
=
a2b 3 − a3b 2a3b 1 − a1b 3a1b 2 − a2b 1
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 12 / 27
basics control structures
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 19/37
exercise
Function: dotProduct3
Write a function named dotProduct3 that calculates the dot product of twovectors a and b , , each with length = 3.
a · b = | a| | b | cos( a, b )
Function: crossProduct3
Write a function named crossProduct3 that calculates the cross product of twovectors a and b , each with length = 3.
a × b = (| a| | b | sin θ) n
a · b = a1 b 1 + a2 b 2 + a3 b 3 a × b =
a1
a2
a3
×
b 1b 2b 3
=
a2b 3 − a3b 2a3b 1 − a1b 3a1b 2 − a2b 1
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 12 / 27
basics control structures
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 20/37
general control structures
The sequence of programs must be controlled. For this purpose three basic typesof program control structures are defined.
sequences
branchesrepetitions
These basic forms are implemented in any programming language by different
constructs.
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 13 / 27
basics control structures
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 21/37
sequences
A sequence is a series of instructions.
Algorithm
1 s t at e me n t 1 ;2 s t at e me n t 2 ;3 s t at e me n t 3 ;4 . . .
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 14 / 27
basics control structures
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 22/37
branches
Branching between alternative code sequences (blocks) depends on whether acondition is satisfied. In MATLAB there are variations of the if construct and theswitch construct for this purpose.
To express conditions, relational and logical operators are required.
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 15 / 27
basics control structures
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 23/37
branches - relational operators
Relational operators determine the relation between two operands (numbers,strings) and return 1, i.e. true or 0, i.e. false , depending on the relation.
Operator Operation
== equal to∼= not equal to> greater than
>= greater than or equal to< less than<= less than or equal to
Examples
Operation Result
3 < 4 13 >= 4 03 == 4 04 <= 4 1’A’<’B ’ 1
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 16 / 27
basics control structures
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 24/37
branches - logical operators
Logical operators evaluate the logic result of one (unary operator) or two operands(binary operator). They return 1, i.e. true or 0, i.e. false , depending on therelation.
Operator Operation
& logical AND| logical OR
xor logical exclusive OR∼ logical NOT
Examples
Inputs AND OR XOR NOTl 1 l 2 l 1&l 2 l 1|l 2 XOR (l 1, l 2) ∼ l 1
0 0 0 0 0 10 1 0 1 1 11 0 0 1 1 01 1 1 1 0 0
In case of scalars & becomes && and | becomes .Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 17 / 27
basics control structures
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 25/37
branches - operator hierachy
Hierachy of arithmetic
Precedence Operation
1The contents of all parentheses are evaluated, starting fromthe innermost parentheses and working outward.
2All exponentials are evaluated, working from left to right.
3All multiplications and divisions are evaluated, working fromleft to right.
4All additions and subtractions are evaluated, working from leftto right.
5 All relational operators (==, ∼=, >, >=, <, <=) are eva-luates, working from left to right.
6 All ∼ operators are evaluated.7 All & and | operators are evaluated, working from left to right.
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 18 / 27
basics control structures
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 26/37
branches - if construct
control expressions will be
some combination of relationaland logical operatorsthe code block below a controlexpression is execetued, in casethe expression evalutes to ’1’,
i.e. true after the execution of one codeblock (i.e. one branch) theother expressions are notcheckedmultiple elseif clauses areallowed, including zerothere can be at most one else
clauseif constructs can be nested
Algorithm
1 i f c o n t r o l e x p r e s s i o n 12 S t a t e m e n t A13 S t a t e m e n t A24 . . .5 e l s e i f c o n t r o l e x p r e s s i o n 26 S t a t e m e n t B17 S t a t e m e n t B28 . . .9 e l s e
10 S t a t e m e n t C111 S t a t e m e n t C212 . . .13 end
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 19 / 27
basics control structures
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 27/37
branches - if construct - example
MATLAB
1 % Prompt t h e u s e r f o r t h e c o e f f i c i e n t s o f t h e e q u a t i o n2 d i s p ( ’ T h i s program s o l v e s f o r t h e r o o t s o f a q u a d r a t i c ’ ) ;3 d i s p ( ’ e q ua t io n o f t he form A∗X ˆ 2 + B∗X + C = 0 . ’ ) ;
4 a = i n p u t ( ’ E n t e r t h e c o e f f i c i e n t A : ’ ) ;5 b = i n p u t ( ’ E n t e r t h e c o e f f i c i e n t B : ’ ) ;6 c = i n p u t ( ’ E n t e r t h e c o e f f i c i e n t C : ’ ) ;7
8 % C a l c u l a t e d i s c r i m i n a n t 9 d i s c r i m i n a n t = b ˆ2 − 4 ∗ a ∗ c ;
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 20 / 27
basics control structures
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 28/37
branches - if construct - example
MATLAB
1
% So l v e f o r t h e r oo ts , d ep e nd in g on t h e d i s c r i m i n a n t 2 i f d i s c r i m i n a n t > 0 % t h e r e a r e two r e a l r o o ts , s o . . .3 x1 = ( −b + s q r t ( d i s c r i m i n a n t ) ) / ( 2 ∗ a ) ;4 x2 = ( −b − s q r t ( d i s c r i m i n a n t ) ) / ( 2 ∗ a ) ;5 d i s p ( ’ T h is e q ua t io n h a s two r e a l r o o t s : ’ ) ;6 f p r i n t f ( ’ x 1 = %f \n ’ , x1 ) ;
7 f p r i n t f ( ’ x 2 = %f \n ’ , x2 ) ;8 e l s e i f d i s c r i m i n a n t == 0 % t h e r e i s one r e p e a t e d r o o t 9 x1 = ( −b ) / ( 2 ∗ a ) ;
10 d i s p ( ’ T h i s e qu at i o n h a s two i d e n t i c a l r e a l r o o t s : ’ ) ;11 f p r i n t f ( ’ x1 = x2 = %f \n ’ , x1 ) ;12 e l s e % t h e r e a r e c om pl ex r o o t s
13 r e a l p a r t = ( −b ) / ( 2 ∗ a ) ;14 i m ag p a rt = s q r t ( a b s ( d i s c r i m i n a n t ) ) / ( 2 ∗ a ) ;15 d i s p ( ’ T hi s e q u at i o n h as c om pl ex r o o t s : ’ ) ;16 f p r i n t f ( ’ x1 = %f + i %f \n ’ , r e a l p a r t , i m ag p a r t ) ;17 f p r i n t f ( ’ x 1 = %f − i %f \n ’ , r e a l p a r t , i m ag p a r t ) ;18 end
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 21 / 27
basics control structures
b h i h
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 29/37
branches - switch construct
the switch expression mustevaluate to a number,character, or booleanonly the code block below thecase, whichs value list containsthe value equal to theswitch expression is executedthe otherwise code block isoptionalat most one code block isexecuted
switch constructs can benested
Algorithm
1 s w i t c h ( s w i t c h e x p r e s s i o n )2 c a s e { v al u e 1 , v a l u e 2 } ,3 S t a t e m e n t A14 S t a t e m e n t A2
5 . . .6 c a s e v a l u e 3 ,7 S t a t e m e n t B18 S t a t e m e n t B29 . . .
10 o t h e r w i s e ,11 S t a t e m e n t C112 S t a t e m e n t C213 . . .14 end
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 22 / 27
basics control structures
i i
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 30/37
repetitions
The repetition of a sequence of statemens is generaly achieved through loops.
In MATLAB, there are
while - loopsfor - loops
The difference between them is in the way the repetitions are controlled.
while-loop:The sequence of statements in the loop is repeated an indefinite number of times,until a specified condition is not satisfied anymore.
for-loop:The number of repetitions is specified before the loop starts.
Loops can be nested.
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 23 / 27
basics control structures
i i hil l
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 31/37
repetitions - while-loop
as long as the expression is
non-zero, i.e. true , the codesequence within the loop willbe executed
Algorithm
1 w h i l e e x p r e s s i o n2 . . .3 end
MATLAB
1 n =1; s um x = 0;2 x = i n p u t ( ’ E nt er t he n ex t v a l u e : ’ ) ;3 w h i l e x ∼= 04 n = n + 1 ;
5 sum x = s um x + x ;6 x = i n p u t ( ’ E n te r t he n ex t v a l u e : ’ ) ;7 end8 f p r i n t f ( ’ The sum o f t he e n te r ed v a l u e s i s : %f \n ’ , sum x ) ;9 f p r i n t f ( ’ You e n t e r e d %i v a l u e s .\ n ’ , n ) ;
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 24 / 27
basics control structures
titi hil l
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 32/37
repetitions - while-loop
as long as the expression is
non-zero, i.e. true , the codesequence within the loop willbe executed
Algorithm
1 w h i l e e x p r e s s i o n2 . . .3 end
MATLAB
1 n =1; s um x = 0;2 x = i n p u t ( ’ E nt er t he n ex t v a l u e : ’ ) ;3 w h i l e x ∼= 04 n = n + 1 ;
5 sum x = s um x + x ;6 x = i n p u t ( ’ E n te r t he n ex t v a l u e : ’ ) ;7 end8 f p r i n t f ( ’ The sum o f t he e n te r ed v a l u e s i s : %f \n ’ , sum x ) ;9 f p r i n t f ( ’ You e n t e r e d %i v a l u e s .\ n ’ , n ) ;
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 24 / 27
basics control structures
titi f l
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 33/37
repetitions - for-loop
expression is ususally a vector or amatrix with n columnsthe code sequence within the loopbody is repeated n timesfor each loop run, the current
column is copied into index index should not be modifiedwithin the loop body
Algorithm
1 f o r i n d e x = e x p r e s s i o n2 . . .3 . . .4 . . .5 end
MATLAB
1 f o r i i = 1 : 1 02 . . .3 end
MATLAB
1 f o r i i = 1 : 2 : 1 02 . . .3 end
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 25 / 27
basics control structures
repetitions for loop
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 34/37
repetitions - for-loop
expression is ususally a vector or amatrix with n columnsthe code sequence within the loopbody is repeated n timesfor each loop run, the current
column is copied into index index should not be modifiedwithin the loop body
Algorithm
1 f o r i n d e x = e x p r e s s i o n2 . . .3 . . .4 . . .5 end
MATLAB
1 f o r i i = 1 : 1 02 . . .3 end
MATLAB
1 f o r i i = 1 : 2 : 1 02 . . .3 end
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 25 / 27
basics control structures
repetitions for loop
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 35/37
repetitions - for-loop
MATLAB
1 f o r i i = [ 5 9 7 ]2 . . .3 end
MATLAB
1 f o r i i = [ 1 2 3 ; 4 5 6 ]2 . . .3 end
MATLAB1 n=i n p u t ( ’ E nt er t he i n t e g e r f o r w h ic h t he f a c t o r i a l i s t o
be c a l c u l a t e d : ’ ) ;2 n f a c t o r i a l =1;3 f o r i i = n :−1: 1
4 n f a c t o r i a l = n f a c t o r i a l ∗ i i ;5 end6
7 f p r i n t f ( ’ The f a c t o r i a l o f %i (% i ! ) i s %i .\ n ’ , n , n ,n f a c t o r i a l ) ;
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 26 / 27
basics control structures
repetitions for loop
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 36/37
repetitions - for-loop
MATLAB
1 f o r i i = [ 5 9 7 ]2 . . .3 end
MATLAB
1 f o r i i = [ 1 2 3 ; 4 5 6 ]2 . . .3 end
MATLAB1 n=i n p u t ( ’ E nt er t he i n t e g e r f o r w h ic h t he f a c t o r i a l i s t o
be c a l c u l a t e d : ’ ) ;2 n f a c t o r i a l =1;3 f o r i i = n :−1: 1
4 n f a c t o r i a l = n f a c t o r i a l ∗ i i ;5 end6
7 f p r i n t f ( ’ The f a c t o r i a l o f %i (% i ! ) i s %i .\ n ’ , n , n ,n f a c t o r i a l ) ;
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 26 / 27
basics control structures
exercise
7/21/2019 Matlab Introduction v2
http://slidepdf.com/reader/full/matlab-introduction-v2 37/37
exercise
Control structures: dotProduct
Modify the function dotProduct3 such, that the dot product of two vectors regardless of their size, can be evaluated.
Make sure, that both vectors received through the inputargument list are of the same size.
Schwedler (Bauhaus-Universitat Weimar) Introduction to MATLAB 18.10.2011 27 / 27