You are on page 1of 4

▌Objectives

Introduction to  Introducing to flow charts


 Algorithm capabilities
Matlab  Employ numerical methods in examples
 Emphasizing the appeal of MATLAB as a
programming tool.

Prepared by
Dr. Mohamed Saber Sokar
Dr. Mohamed Saber Sokar 2

▌Outline: ▌Flow chart -00

 Flow chart
Flow chart is a graphical
 Flow Control
representation, illustrating the logical
steps, calculation and decisions, in
 Using of M-File
sequence, that must be performed to
 Writing practical m.files
accomplish a specific task.

Dr. Mohamed Saber Sokar 3 Dr. Mohamed Saber Sokar 4

▌Flow chart -01 ▌Flow chart -02


1- Start and End 2- Input data and output results

Start
Enter
x1, x2, x3

Output
Stop y1, y2, y3

Dr. Mohamed Saber Sokar 5 Dr. Mohamed Saber Sokar 6

1
▌Flow chart -03 ▌Flow chart -04
3- Processing 4- If - Statement Syntax
 Any constant value or equation must be indicated IF [condition]
in a rectangle [code to execute if condition is true]
ELSE
 Constants constant = 9.81 [code to execute if condition is false]
END Dummy Example
a=2;
if (a<3)
 Equations y =f(x) Some Matlab Commands;
else
Some Matlab Commands;
end
Dr. Mohamed Saber Sokar 7 Dr. Mohamed Saber Sokar 8

▌Flow chart -05 ▌Flow chart -06


5- While Loop Syntax 6- For - loop syntax Some Dummy Examples
while (condition)
for j=1:3:200
Matlab Commands for i=intial:step:final Some Matlab Commands;
end
end Matlab Commands
Dummy Example end
while ((a>3) & (b==5)) for x = 1:2:100
For i = 1 to xx, step xx
Some Matlab Commands; r = x^2;
fprintf('r(%g)=%g \n',x,r)
end end

Dr. Mohamed Saber Sokar 9 Dr. Mohamed Saber Sokar 10

5.

▌Flow chart -07


Fi ▌Case study
Cont. For - loop syntax  If C and F are Celsius and Fahrenheit
temperatures respectively, the formula for
N = intial conversion from Fahrenheit to Celsius is
C=5/9(F-32)
 Write a script which will ask for the Fahrenheit
is No temperature and display the equivalent Celsius
N= N+step
N <=> end one with some sort of comment.
Yes

Dr. Mohamed Saber Sokar 11 Dr. Mohamed Saber Sokar 12

2
▌Case study (flow chart) ▌Case study (*.m file)
Start % program to convert Fahrenheit temp. to Celsius
Enter F = input(' Enter Fahrenheit temperature: ');
Fahrenheit
c = 5/9* (F- 32);
Celsius = (Fahrenheit-32) disp( ' The Celsius temperature is: ' ), disp(c)
% or
Output
fprintf( ' The Celsius temperature is: %g \n ', c)
Celsius

Stop
Dr. Mohamed Saber Sokar 13 Dr. Mohamed Saber Sokar 14

▌Case study ▌Case study (flow chart)


Start
 The general gas low is (P V= m R T)
Enter
 Draw a flow chart and write a script that will ask M, m, V, T

8.314
for the molecular weight of a gas, mass,
temperature in Kelvin and volume to calculate
the pressure in S.I. units /

Output
Pressure (p)

Stop

Dr. Mohamed Saber Sokar 15 Dr. Mohamed Saber Sokar 16

▌Case study (*.m file) ▌Plotting pv diagram at T=c


% program to calculate gas pressure  What is should added to draw plot of p-v diagram?
clc  modify
M = input(' Enter moleuclar weight of gas= '); V = input(' Enter volume of gas in [m3]= ');
m = input(' Enter mass of gas in [kg]= ');
 Replaced by
T = input(' Enter temerature of gas in [K]= ');
V = input(' Enter volume of gas in [m3]= '); Vmax = input(' Enter max volume of gas in [m3]= ');
R=8.314 / M; V= 0 : 0.1 : Vmax
p=m*R*T/V; plot( v , p ) ; grid ;
fprintf(' The pressure of a given gas= %g Pa \n ', p) title( ' p-v diagram' )
xlabel( ' Volume [m3] ' ) ; ylabel( ' pressure [Pa] ' )

Dr. Mohamed Saber Sokar 17 Dr. Mohamed Saber Sokar 18

3
▌Case study (*.m file) ▌pv diagram at T=c
% program to calculate gas pressure p-v diagram

M = input(' Enter moleuclar weight of gas= '); 900

800
m = input(' Enter mass of gas in [kg]= '); 700

Vmax = input(' Enter max volume of gas in [m3]= '); 600

Pressure [Pa]
T = input(' Enter temerature of gas in [K]= '); 500

R=8.314 / M; 400

300
V= 0 : 0.1 : Vmax; 200
p = m * R * T ./ V ; 100

plot( V , p ) ; grid ; title( ' p-v diagram at T=c' ) 0


0 1 2 3 3
4 5 6
Volume [m ]
xlabel( ' Volume [m3] ' ) ; ylabel( ' pressure [Pa] ' )

Dr. Mohamed Saber Sokar 19 Dr. Mohamed Saber Sokar 20

▌M-file (using for –loop)


% Calculate vertical motion under the action of gravity 200
Vertical motion under gravity

u = 60; % or u = input(' Enter initial velocity in m/s= ');


g = 9.8; % acceleration due to gravity 150

for t=0 : 0.1: 13


Vertical displacement

s = u*t - g/2 * t^2 ; % without dot (.^) 100

plot( t , s, 'r--*') , hold on


50
end
title( ' Vertical motion under gravity ' ) 0

xlabel( ' Time ' )


ylabel( ' Vertical displacement ' ), grid -50
0 2 4 6 8 10 12 14
Time

Dr. Mohamed Saber Sokar 21 Dr. Mohamed Saber Sokar 22

Many thank for Your


attention

Dr. Mohamed Saber Sokar 23

You might also like