Title: Matlab: User Defined Functions
1Matlab 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.
2- 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
- 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
3clear clc format compact close all
- 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 105
f 0.020 rho 998 L 1000 D 0.5 mdot
600
A piD2/4 V mdot/(rhoA) dp
f(L/D)rhoV2/2 disp(dp)
4clear 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 piD2/4 V mdot/(rhoA) dp
f(L/D)rhoV2/2 disp(dp)
A piD2/4 n 1000 mdotMin 0 mdotMax
800 dMdot (mdotMax - mdotMin)/(n-1)
for i 11n mdot(i) (i-1)dMdot V
mdot(i)/(rhoA) dp(i) f(L/D)rhoV2/2 en
d
5clear clc format compact close all f
0.020 rho 998 L 1000 D 0.5 A
piD2/4 dPmax 500000 dMdot 10 i
1 mdot(i) 0 dp(1) 0 while (dp(i) lt
dPmax) i i1 mdot(i) (i-1)dMdot
V mdot(i)/(rhoA) dp(i)
f(L/D)rhoV2/2 end plot(mdot,dp) title('Pipe
Flow Pressure Drop') xlabel('mass flow rate
(kg/s)') ylabel('pressure drop (Pa)')
6clear clc format compact close all f
0.020 rho 998 L 1000 D 0.5 A
piD2/4 dPmax 500000 dMdot 10 i
1 mdot(i) 0 dp(1) 0 while (dp(i) lt
dPmax) i i1 mdot(i) (i-1)dMdot
V mdot(i)/(rhoA) dp(i)
f(L/D)rhoV2/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
7function 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 piD2/4 V
mdot/(rhoA) dp f(L/D)rhoV2/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)
8clear clc format compact close all n
1000 mdotMin 0 mdotMax 800 dMdot
(mdotMax - mdotMin)/(n-1) for i 11n
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
9clear clc format compact close all dMdot
10 dPmax 50000 i 1 mdot(i) 0 dp(1)
0 while (dp(i) lt dPmax) i i1
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
10- The aerodynamic drag on an aircraft may be
calculated from
- 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 lt V lt
100 m/s - drag2.m use a while loop to calculate and
plot drag for V gt 20 m/s and D lt 2.5 kN with
velocity increments of 2 m/s - Sample results are on the next slide
clear clc format compact close all CD0
0.025 AR 8 S 16 W 13000 rho 1.225 V
65 CL W/(0.5rhoV2S) CD CD0
CL2/(piAR) D 0.5rhoV2CDS
11(No Transcript)