0% found this document useful (0 votes)
16 views14 pages

Lab IAT 02

This document discusses MATLAB programming concepts including scripts, loops, and conditional statements. It provides examples of scripts to evaluate functions at given points and in intervals. Exercises are presented to write scripts that calculate the factorial of a number, check if a number is a multiple of another, and determine if a year is a leap year. Loops and conditional statements like if-else are demonstrated to control program logic and flow.

Uploaded by

igorkapganglink
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views14 pages

Lab IAT 02

This document discusses MATLAB programming concepts including scripts, loops, and conditional statements. It provides examples of scripts to evaluate functions at given points and in intervals. Exercises are presented to write scripts that calculate the factorial of a number, check if a number is a multiple of another, and determine if a year is a leap year. Loops and conditional statements like if-else are demonstrated to control program logic and flow.

Uploaded by

igorkapganglink
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Introduction to Numerical Methods and MATLAB

Programming for Engineers


Lecture 2: Scripts, loops, if statement

Claudia Zoccarato

E-mail: [email protected]

June, 05 2017

Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
Introduction to programming

Aim: write codes and develop algorithms in MATLAB


Use of the m-file, text files saved with .m extension
The m-files consist of sequence of commands executed by the
calculator
You can choose among two type of programs in MATLAB:
1 SCRIPT: it is simply a collection of MATLAB commands in an m-file
(predefined or user-defined commands)
2 FUNCTION: collection of MATLAB commands in a m-file with
input/output variables
The scripts use global variables
The functions use local variables which are deleted from the memory
after the function execution

Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017
SCRIPT - Example

Write a script in MATLAB to display the sentence ’hello world’


Open MATLAB, create a new script (Home -> New Script) and save
it as hw.m
Write the following text in the file hw.m:

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 ’ )

Save the file


Run the script from the command window:
>> filename + ENTER

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

in a point x. The point is x = 3 and the solution will be y = 78.


Use the command fprintf to display the result of the calculation. Display
the variables with an appropriate format. Note: Save the script as eval1.m

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

Greater > AND && performs a logical AND


Greater or equal >= OR || performs a logical OR
Less < NOT ∼ Find logical NOT
Less or equal <=
Equal ==
Not equal ∼= (a>=-2) && (a<=2)
True if a ∈ [-2,2]
False if a < −2 or a > 2
a>b
True if ’a’ is greater than ’b’
False if ’a’ is less or equal to ’b’

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

in n + 1 evenly spaced points within the interval I = [xmin , xmax ]:


x0 = xmin , x1 = x0 + h, . . . , xn = xmax , with h = (xmax − xmin )/n.
Use, for example, xmin = −4, xmax = 6 e n = 11.
Use for loop to solve the problem.

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

1 Implement the factorial function in matlab:

n! = n · (n − 1) · ... · 1

HINT: use a while loop


2 Implement a program that, given two numbers a and b (integers),
checks if the a is a multiple of b. HINT: use the MATLAB function
mod(a,b) which gives the modulus after division.
3 Implement a program that checks if a year is bissextile. A year is
bissextile if it is divisible for 4 but not for 100 or if it is divisible for
400.

Claudia Zoccarato MATLAB Programming for Engineers ENSTP Yaounde Camerun - 05/06/2017

You might also like