Sie sind auf Seite 1von 11

Matlab: User Defined Functions

The objectives for this tutorial are to review what has been
covered to this point and to introduce the use of user defined
functions. Again, please do not treat the code examples as a
typing exercise. Be certain that you understand the syntax and
purpose of each program line. The code examples and assignment
for this tutorial are very similar to what you will be expected to do
in next weeks exam.
Steps for program creation:
Workspace and console initialization
Program constants
Sample calculation
Implement array calculations with a for
or while loop
Add plotting functions
In the program creation process, it is
important to develop the program
incrementally and to fully debug and verify
each addition before going further
( )
Pa 10 87 1
4
s
kg
00 6
m 5 0 m 000 1
m
kg
998 02 0
Pa drop pressure
2
5
2
3
2
= A
|
.
|

\
|
= = =
= =
= =
= = A
.
.
.
P
D
m
A
m
V m
D L
f
V
D
L
f P
t

We will start the tutorial by illustrating the


process for developing a solution to last
weeks assignment
For the assignment, you were given the
formula for calculating the pressure drop in
a pipe flow and a set of sample calculations
clear
clc
format compact
close all
f = 0.020;
rho = 998;
L = 1000;
D = 0.5;
mdot = 600;
A = pi*D^2/4;
V = mdot/(rho*A);
dp = f*(L/D)*rho*V^2/2;
disp(dp)
Steps for program creation:
Workspace and console initialization
create a new m-file with this code
save the file as pipe1.m & execute it
verify that it runs without errors
Program constants
add this program segment & save
changes
verify that the program runs with the
modifications without errors and that
the variables and correct values show up
in the workspace
Sample calculation
add this program segment & save
changes
verify that the program segment
executes without errors and generates
the correct result, dp = 1.87 x 10
5

clear
clc
format compact
close all

f = 0.020;
rho = 998;
L = 1000;
D = 0.5;
mdot = 600;
Steps for program creation:
This code has been entered and validated
Delete the sample calculation and add the
code to determine the number of passes
through the for loop and the increment
in mass flow rate between loops
enter and save the changes
execute the code and verify the results
Now add the code for the for loop
enter and save the changes
execute the code and verify the array
results are created as expected
Add the code for the plot
enter and save the changes
execute the code and verify that the plot
is created as expected
be certain that you understand the syntax
and purpose of each program statement
A = pi*D^2/4;
V = mdot/(rho*A);
dp = f*(L/D)*rho*V^2/2;
disp(dp)
A = pi*D^2/4;
n = 1000;
mdotMin = 0;
mdotMax = 800;
dMdot = (mdotMax - mdotMin)/(n-1);
for i = 1:1:n
mdot(i) = (i-1)*dMdot;
V = mdot(i)/(rho*A);
dp(i) = f*(L/D)*rho*V^2/2;
end
plot(mdot,dp)
title('Pipe Flow Pressure Drop');
xlabel('mass flow rate (kg/s)');
ylabel('pressure drop (Pa)');
clear
clc
format compact
close all

f = 0.020;
rho = 998;
L = 1000;
D = 0.5;

A = pi*D^2/4;
dPmax = 500000;
dMdot = 10;
i = 1;
mdot(i) = 0;
dp(1) = 0;

while (dp(i) < dPmax)
i = i+1;
mdot(i) = (i-1)*dMdot;
V = mdot(i)/(rho*A);
dp(i) = f*(L/D)*rho*V^2/2;
end

plot(mdot,dp)
title('Pipe Flow Pressure Drop');
xlabel('mass flow rate (kg/s)');
ylabel('pressure drop (Pa)');
clear
clc
format compact
close all

f = 0.020;
rho = 998;
L = 1000;
D = 0.5;

A = pi*D^2/4;
dPmax = 500000;
dMdot = 10;
i = 1;
mdot(i) = 0;
dp(1) = 0;

while (dp(i) < dPmax)
i = i+1;
mdot(i) = (i-1)*dMdot;
V = mdot(i)/(rho*A);
dp(i) = f*(L/D)*rho*V^2/2;
end

plot(mdot,dp)
title('Pipe Flow Pressure Drop');
xlabel('mass flow rate (kg/s)');
ylabel('pressure drop (Pa)');
This is the second version of the program
which uses a while loop save it as pipe2.m
Create and test this program incrementally
as you did for the first program
workspace initialization
variable initialization
initialization required for the first pass
through the while loop
while loop calculations
plot the results
Be certain that you understand the syntax
and function of each program statement
before going further in the turial
function dp = PressureDrop (mdot)
% dp = PressureDrop (mdot)
% dp - pressure drop (Pa)
% mdot - mass flow rate (kg/s)

f = 0.020;
rho = 998;
L = 1000;
D = 0.5;
A = pi*D^2/4;
V = mdot/(rho*A);

dp = f*(L/D)*rho*V^2/2;
Notes:
the first word on the first line (function
header line) must be function this is a
keyword for matlab
next on the header line:
return variable (dp)
equals sign (=)
function name (PressureDrop)
parameter list (mdot) contained in
parentheses
the lines beginning with % are comments
used for documentation
when the function executes, it will have its
own private workspace for variable
storage required variables must be
defined in the function file
calculations ending with the evaluation of
the function return value that was
specified in the header line (dp)
Now we are going to modify the two
programs to use a user-defined function to
calculate the pressure drop rather than
including the calculations directly within
the program
Create a new m-file which contains the
code shown above
Save the file as PressureDrop.m (this will be
the default choice)
clear
clc
format compact
close all

n = 1000;
mdotMin = 0;
mdotMax = 800;
dMdot = (mdotMax - mdotMin)/(n-1);

for i = 1:1:n
mdot(i) = (i-1)*dMdot;
dp(i) = PressureDrop(mdot(i));
end

plot(mdot,dp)
title('Pipe Flow Pressure Drop');
xlabel('mass flow rate (kg/s)');
ylabel('pressure drop (Pa)');

Modify pipe1.m as shown to use the new
function file PressureDrop.m
Notes:
most of the variable initialization has
been deleted since the variables are only
needed with the function
the function is called with the syntax that
was defined in the function header line:
the function name is PressureDrop
it requires one parameter, the mass
flow rate mdot
it returns a single result dp
Save and execute the modified program
Verify that the results are the same as before
Be certain that you understand the syntax
and function of each program line
clear
clc
format compact
close all

dMdot = 10;
dPmax = 50000;
i = 1;
mdot(i) = 0;
dp(1) = 0;

while (dp(i) < dPmax)
i = i+1;
mdot(i) = (i-1)*dMdot;
dp(i) = PressureDrop(mdot(i));
end

plot(mdot,dp)
title('Pipe Flow Pressure Drop');
xlabel('mass flow rate (kg/s)');
ylabel('pressure drop (Pa)');
Modify pipe2.m to use the function
PressureDrop.m as shown
Save the changes, execute the program, and
verify the same results are produced
C
D0
0.025 := parasite drag coef f icient S 16m
2
:= wing area
AR 8 := wing aspect ratio W 13000N := aircraf t weight
V 65
m
s
:= f light speed 1.225
kg
m
3
:= air density
C
L
V ( )
W
1
2
V
2
S
:= C
D
V ( ) C
D0
C
L
V ( )
2
t AR
+ :=
D V ( )
1
2
V
2
C
D
V ( ) S := D 65
m
s
|

\
|
|
.
1.198 10
3
N =
clear
clc
format compact
close all

CD0 = 0.025;
AR = 8;
S = 16;
W = 13000;
rho = 1.225;
V = 65;

CL = W/(0.5*rho*V^2*S);
CD = CD0 + CL^2/(pi*AR);
D = 0.5*rho*V^2*CD*S;
The aerodynamic drag on an aircraft may
be calculated from:
S V
W
C
AR
C
C C
S C V D
L
L
D D
D
2
2
1
2
0
2
2
1

= + =
=
Sample calculations at a speed of 65 m/s
from Mathcad and Matlab are shown at left
Create 2 programs to calculate and plot the
variation of drag with flight speed with the
following requirements:
both programs should use a function
drag.m which takes velocity as an input
parameter and returns the drag
drag1.m use for loop to calculate and
plot drag for 100 values of velocity with
20 m/s < V < 100 m/s
drag2.m use a while loop to calculate
and plot drag for V > 20 m/s and D < 2.5
kN with velocity increments of 2 m/s
Sample results are on the next slide
20 30 40 50 60 70 80 90 100
800
1000
1200
1400
1600
1800
2000
2200
2400
2600
Aircraft Drag
Flight Speed (m/s)
D
r
a
g

(
N
)

Das könnte Ihnen auch gefallen