Vlachopoulos Georgios Lecturer of Computer Science and Informatics Technological Institute of Patras, Department of Optometry, Branch of Egion Lecturer of Biostatistics Technological Institute of Patras, Department of Physiotherapy, Branch of Egion Programming in Matlab Basic Control Structures
SymbolMeaning ==Equal >Greater Than >=Greater or Equal <Less Than <=Less or Equal ~=Not Equal Vlachopoulos Georgios
SymbolMeaning &Logical AND (conjugation) |Logical OR (disjunction) ~Logical NOT (negation) Vlachopoulos Georgios
ABA & BA | B~ A~B False True FalseTrueFalseTrue False TrueFalse TrueFalseTrue False Vlachopoulos Georgios Priority : & | ~
Selection Structures ◦ Simple Selection ◦ Composite Selection ◦ Nested Selection ◦ Multiple Selection Iteration Structures Vlachopoulos Georgios
Simple Selection Vlachopoulos Georgios condition commands if condition commands end
Composite Selection Vlachopoulos Georgios Condition commands1 if condition commands1 else commands2 end commands2
Nested Selection Vlachopoulos Georgios if condition1 commands1 if condition 2 … else … end else if condition 3 … else … end
Multiple Selection Vlachopoulos Georgios if condition1 Commands1 elseif condition2 Commands2 elseif condition3 Commands3 …. elseif conditionN CommandsN else CommandsN+1 end Every multiple Selection can be written as nested
Example 1 Create an m-file that receives from the user a value and calculates the absolute value without the use of the corresponding elementary function Example 2 Create an m-file that receives from the user the coefficients of a first order linear equation and solves it. Vlachopoulos Georgios
For while Vlachopoulos Georgios Syntax for index=initial_value:final_value:step commands end Syntax While condition commands end
for i=1:100:1 disp (‘Your Name’) End i=1 while (i<=100) disp (‘Your Name’) i=i+1 end Vlachopoulos Georgios
Exercise 1a: Create an m-file to calculate the sum: s=1+2+3+… Exercise 1b: Convert the m-file to function that receives as argument the final value of the sum and calculates it. Exercise 1c: Create an m-file to calculate the sum: s=1+2+3+… up to the value that the sum remains lower to Exercise 2: Create an m-file to calculate the product: p=3*5*7*…*21 Exercise 4: Create an m-file to calculate the sum: s= …50 4 up to the value that the sum remains lower to Vlachopoulos Georgios
Exercise 5: Create an m-file to calculate the sum: S= … Exercise 6: Create an m-file to calculate the sum: s=3!-4!+5!-6!+7!-…+21! Exercise 7: Create an m-file to calculate the sum: Vlachopoulos Georgios