Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

Computer Applications in

Energy Systems

ES 810

adeelwaqas@ces.nust.edu.pk

Modeling of Energy Systems Using


MATLAB 1
MATLAB What is m-Files
An m-file, or script file, is a simple text file where you can place MATLAB commands.

When the file is run, MATLAB reads the commands and executes them exactly as it
would if you had typed each command sequentially at the MATLAB prompt.

All m-file names must end with the extension '.m' (e.g. test.m).

If you create a new m-file with the same name as an existing m-file, MATLAB will
choose the one which appears first in the path order (type help path in the command
window for more information).

To make things easier, choose a name for your m-file which doesn't already exist.

Why Use m Files


For simple problems, entering your requests at the MATLAB prompt is fast and
efficient.
However, as the number of commands increases or trial and error is done by changing
certain variables or values, typing the commands over and over at the MATLAB prompt
becomes tedious. M-files will be helpful and almost necessary in these cases.
MATLAB What is m-Files
How to create, save or open an m-file?
To create an m-file, choose New from the File menu and select Script. This
procedure brings up a text editor window in which you can enter MATLAB
commands.

To save the m-file, simply go to the File menu and choose Save (remember to
save it with the '.m' extension). To open an existing m-file, go to the File menu
and choose Open.

How to run the m-file?


After the m-file is saved with the name filename.m in the current MATLAB
folder or directory, you can execute the commands in the m-file by simply
typing filename at the MATLAB command window prompt. F5 key can also
be used from key board to run the file
If you don't want to run the whole m-file, you can just copy the part of the m-
file that you want to run and paste it at the MATLAB prompt.
http://ctms.engin.umich.edu/CTMS/index.php?aux=Extras_Mfile#1
Equation of time from Duffie book
Calculation of Declination Angle
Declination Angle
Calculation of Solar Radiation  ET
Calculation of Solar Radiation  ET
Calculation of Solar Radiation  ET
Conditions And Loops in MATLAB  if Loop
‘if’ statements
Purpose: Tests an expression and only executes the code if the
expression is true.
Format:
if expression
statement(s) to be executed (know as the body of the loop)
end

Rules:
• The variables in the expression to be tested must have values assigned prior to
entering the IF statement.

• The block of code will only execute if the expression is true. If the
expression is false, then the code is ignored.

• Values assigned to the variables used in the expression may be changed in the
block of code inside the IF statement
Conditions And Loops in MATLAB  if Loop
a = 2;
b = 3;
if (a<b)
j = -1;
end
Additional statements can be added for more refined decision making

a = 4; b = 3;

if (a<b)
j = -1;
elseif (a>b)
j = 2;
end
Conditions And Loops in MATLAB  if Loop
Conditions And Loops in MATLAB  if Loop

x = 100;
y = -10;
if x < 50 % to see if x is less than 50
z = x – y^2;
A = x + 12.5*y + z^3;
end
% won’t run because x is not less than 50
Conditions And Loops in MATLAB  for Loop
'for' loops
Purpose:
To repeat a statement or a group of statements for a fixed number of
times.

Format:
for variable = expression
statement(s) to be executed (know as the body of the loop)
end
Conditions And Loops in MATLAB  for Loop
Examples:
Sequential numbers
for i=1:3 %executes three times
x=i^2
end
x=
1
x=
4
x=
9
Example

y = -10;
x=1:1:100

z=x-y
A=x+12.5+z3 for x<50

z= = x-y+2
A = x + 12.5*y + z2 for x=>50
Practice Exercise to format the loops (if and for)
Reynold Number, Nusselt Number and Heat transfer Coefficient
v=1.568*10^-5;%Viscosity of air @40C
Vm=2 m/s
Dia of the pipe is 2cm or 0.002 m
Length of the pipe =L= 5 m

n=0.4
For circular tubes and constant heat flux (especially for solar radiations)

0.68 0.33 0.68


Nu 0.116 *(Re 125) *(Pr ) *(1*(D / L )
Reynold Number and Nusselt Number
Nusselt number is the ratio of convective to conductive heat transfer across (normal to)
the boundary.

A Nusselt number close to unity, namely convection and conduction of similar


magnitude, is characteristic of "slug flow" or laminar flow. A larger Nusselt
number corresponds to more active convection, with turbulent flow typically
in the 100-1000 range

Reynolds number (Re) is a dimensionless number that gives a measure of the ratio of
inertial forces to viscous forces

Reynolds numbers, where viscous forces are dominant, and is characterized by


smooth, constant fluid motion, while turbulent flow occurs at high Reynolds
numbers and is dominated by inertial forces, which tend to produce random
eddies, vortices and other flow fluctuations.
Conditions And Loops in MATLAB  for Loop
Nested (FOR loop inside a FOR loop)
for i=1:3 %executes three times
i
for j = 10:10:30
j
end
end
i= i= i=
1 2 3
j= j= j=
10 10 10
j= j= j=
20 20 20
j= j= j=
30 30 30
Conditions And Loops in MATLAB  for Loop

Write a program using a FOR loop:


•Variables: X, Y and loop variable i
•Initialize X = 5 outside the loop
•Loop variable, i, start = 0, end = 150, increment = 10
•In the loop:
 Calculate Y = X*i
 Display i, X and Y to the screen using the command:
Conditions And Loops in MATLAB  for Loop
Rules:
•FOR loops must end with the END statement (lower case).
•The values for the loop variable are controlled by the expression
•The first time through the loop, the variable is assigned the first value in the expression. For the
second time through, MATLAB automatically assigns to the variable the second value in the
expression. This continues for each value in the expression. The loop terminates after the body
of the loop has been executed with the loop variable assigned to each value in the expression.
•The body of the loop is executed many times, but the loop is only executed once for each value
in the expression.
•The expression in a FOR loop is an array of values. The number of times a loop executes equals
the number of values in the expression array.
•After the loop is finished executing, the loop variable still exists in memory and its value is the
last value used inside the loop.
•Any name can be used for a loop variable.
If the name of the loop variable was already used in the program prior to execution of the
loop, old values of the variable are erased and the values of the variable are controlled by the
loop.
i, j, and k are common loop variables; they should not be used if working with complex
numbers.
•Loops can be nested.

You might also like