Lab IAT 02
Lab IAT 02
Claudia Zoccarato
E-mail: [email protected]
June, 05 2017
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
Introduction to programming
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
SCRIPT - Example
1 % S c r i p t to d i s p l a y the s t r i n g ’ h e l l o world ’
2 f p r i n t f ( ’ h e l l o w o r l d \n ’ )
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
Exercise 1
Write a program using a script to evaluate the function
y = 4x3 − 3x2 − 3
Exercise 2
Write a program using a script to evaluate the function
y = 4x3 − 3x2 − 3
in a point x loaded from input file. HINT: use the commands save e
load. Note: Save the script as eval2.m
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
SCRIPT 1
1 close all
2 clear variables
3 % This s c r i p t e v a l u a t e s the f u n c t i o n
4 % y=4xˆ3−3xˆ2−3 i n x=3
5 x = 3.0;
6 y = 4 . 0 ∗ x ˆ3 −3.0∗ x ˆ2 −3.0;
7 f p r i n t f ( ’ The s o l u t i o n y i s %.2 f \n ’ , y )
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
for Loop
The for loop repeats a block of commands for a specified number of
times. The MATLAB syntax is the following:
1 ...
2 for index = values
3 block of statements
4 end
5 ...
Example
1 n = 10;
2 for i = 1:n
3 f p r i n t f ( ’ The v a l u e o f t h e c o u n t e r i i s ’ %d \n ’ , i )
4 end
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
while Loop
The while loop repeats a block of commands untill a specified condition
is veryfied. The MATLAB syntax is the following:
1 ...
2 while condition
3 block of statements
4 end
5 ...
Example
1 n = 0;
2 a = 6;
3 w h i l e n < exp ( a )
4 n = n + 1;
5 end
6 f p r i n t f ( ’ The f i n a l v a l u e o f n i s %d \n ’ , n)
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
Logical Operators in MATLAB
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
The if statement
The if statement allows to verify if a logical expression is true or false. If the
expression is true than the program execute the statements 1, otherwise the
statements 2 are exectuted. The MATLAB syntax is the following:
1 ...
2 i f ( logical expression )
3 { b l o c k o f s t a t e m e n t s 1}
4 else
5 { b l o c k o f s t a t e m e n t s 2}
6 end
7 ...
Example
1 % F i n d t h e maximum b e t w e e n t h e s c a l a r v a r i a b l e s a and b
2 a = 6;
3 b =2;
4 i f (a < b)
5 maxval = b ;
6 else
7 maxval = a ;
8 end
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
Exercise 3
Write a program using a script to evaluate the function
y = 4x3 − 3x2 − 3
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
SCRIPT 3
1 close all
2 clear
3 % T h i s s c r i p t e v a l u a t e t h e f u n c t i o n y=4xˆ3−3xˆ2−3
4 % i n n+1 e v e n l y s p a c e d p o i n t s
5 xmin = −4.0;
6 xmax = 6 . 0 ;
7 n = 11;
8 h = ( xmax−xmin ) /n ;
9 f o r i = 1 : ( n+1)
10 x i = xmin + h ∗( i −1) ;
11 y = 4 . 0 ∗ x i ˆ3−3.0∗ x i ˆ2 −3.0;
12 f p r i n t f ( ’ The s o l u t i o n y i s %.2 f w i t h x = %1.2 f \n ’ , y , x i )
13 end
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
Exercise 4
Write a program using a script to evaluate the real roots x1 , x2 of a
quadratic equation ax2 + bx + c = 0.
The syntax to perform the square root is: y = sqrt(x).
Use the statement if to check the square root is positive, negative or null.
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
SCRIPT 4
Substitutes the dots and write the appropriate statements.
1 clear
2 close all
3 % Compute t h e r e a l r o o t s o f a quadratic equation
4 %
5 d e l t a = b ˆ2 −4.0∗a∗c ; % Compute t h e s q u a r e r o o t
6 % Costrutto logico
7 i f ( d e l t a > 0) % Check i f two s o l u t i o n s exist
8 ...
9 else
10 i f ( d e l t a == 0 ) % Coincident solutions
11 ...
12 else
13 ... % No s o l u t i o n s
14 end
15 end
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
Exercises
n! = n · (n − 1) · ... · 1
Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017