Matlab: User Defined Functions
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
\
|
|
.
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
)