04 Matlab and Roots III
04 Matlab and Roots III
The
static port measures the static pressure (P) of the airflow past the aircraft and the
Pitot tube measures the stagnation pressure (Po) of this flow. If an aircraft's speed
(expressed as a Mach number) is known, the ratio of the two pressures (Po/P) can
be computed:
for M <= 1:
PR( M ) = (1 + 0.2M 2 )
for M >= 1:
PR( M ) = (1.2 M 2 )
3.5
3.5
((7 / 6)M
1 / 6)
2.5
The Matlab if is logically equivalent to a C++ if. Only the details differ.
if M <= 1
% round brackets around expression allowed but not required
ratio = (1 + 0.2 * M^2)^3.5;
else
ratio = (1.2 * M^2)^3.5 * ((7/6)* M^2 - 1/6)^-2.5;
end
Relational and logical operators:
==
~=
<
>
<=
>=
is equal to
is not equal to ** different from C++ **
is less than
is greater than
is less than or equal to
is greater than or equal to
~
&&
||
The script file below plots the pressure ratio for Mach numbers from 0 to 7
(world records: the X-15 rocket plane achieved Mach 6.72; SR-71 Blackbird
jet plane achieved Mach 3.3), reads in a pressure ratio, and outputs the
corresponding Mach number.
figure (1);
fplot (@PR, [0, 7]); % Note the @
xlabel ('Mach Number');
ylabel ('Pressure Ratio');
grid on;
ratio = input ('Enter pressure ratio: ');
% define function for root finding
f = @(M) PR(M) - ratio;
M = fzero (f, [0 7]);
fprintf ('The mach number is %f\n', M);
% f is a function handle
% which is exactly what fplot expects
m-file functions:
The name relates to the function itself
When a function handle is required, one must be created by using @
>>fplot (@PR, [0, 7]); % @PR creates a function handle
Built-in functions:
These are also named
>>fplot (@sin, [0, 7]); % a function handle must be created
Function PR is not vector friendly. Unlike most Matlab functions, it will not work
properly if it is given a vector of input values.
Part of the problem is the mathematical expressions, but no amount of adding
dots to them is going to improve the overall situation.
The real problem is that if M is a vector if M < 1 doesnt make much sense
Comparing a vector to a scalar produces a vector of 1s (true) and 0s (false)
>> M = [ 0.2 0.5 1 2 3 ];
>> M <= 1
ans =
1 1 1 0 0
A vector is considered to be true only if all of its element are non zero. This means
that the formula for M <= 1 will get used only if ALL of the Mach values in the
input vector are <= 1.
Having vectors grow one element at a time is not very efficient. It is much
better create a vector of the required size and then fill in the values. This is
called preallocation.
M = linspace (0, 7, 100); % 100 points between 0 and 7
ratio = ones(size(M)); % preallocate vector for efficiency
for k = 1 : length(M) % from 1 to length(M) in steps of 1
ratio(k) = PR(M(k));
end
Function ones creates an array full of ones. It accepts a two element row
vector containing the dimensions (# of rows, # of columns) of the array.
>> ones([2 3])
ans =
1
1
1
1
1
1
Function zeros (note that Americans dont spell properly) is analogous but
creates an array of zeroes.
Function PRv does the same job as PR did but works properly when
the input value is a vector.
function [ ratio ] = PRv( M )
%PRv Given a Mach number, returns the corresponding pressure ratio
%
Vector friendly version of PR
% Usage: [ ratio ] = PRv( M )
% Inputs: M = Mach number (may be a vector of Mach numbers)
% Outputs: ratio = pressure ratio (stagnation pressure / static pressure)
ratio = ones(size(M)); % preallocate for efficiency
for k = 1:length(M)
if M(k) <= 1
ratio(k) = (1 + 0.2 * M(k)^2)^3.5;
else
ratio(k) = (1.2 * M(k)^2)^3.5 * ((7/6)* M(k)^2 - 1/6)^-2.5;
end
end
end
It might be nice to have a function that converts pressure ratios to Mach numbers
function [ M ] = PR2Mach( ratio )
%PR2Mach Given a pressure ratio, returns the corresponding Mach number
% Usage: [ M ] = PR2Mach( ratio )
% Inputs: ratio = pressure ratio (stagnation pressure / static pressure)
% Outputs: M = Mach number
if ratio < PR(0) || ratio > PR(7)
error 'Pressure ratio is not reasonable.'; % abort function execution
end
% define function for root finding
f = @(M) PR(M) - ratio;
% find root
M = fzero (f, [0 7]);
end
Testing: What should PR2Mach(PR(2)) produce?
Matlab also has while loops and breaks (just like C++).
The script (InteractivePR.m) below uses an infinite loop to read in pressure
ratios and output Mach numbers until a ratio that is less than 1 is entered.
Just like the built-in fplot, our version can be used to plot file functions, built-in
functions, and anonymous functions.
In the first two cases an @ is required (see previous slides).
>> DIYfplot( @PR, [0 7])
>> grid on
>> xlabel ('Mach number');
>> ylabel ('Pressure Ratio');
Dependent\n');
Sample usage:
>> ftable (@PR, 1:0.5:6)
Independent
1.000000
1.500000
2.000000
2.500000
3.000000
3.500000
4.000000
4.500000
5.000000
5.500000
6.000000
Dependent
1.892929
3.413275
5.640441
8.526136
12.060965
16.242001
21.068081
26.538665
32.653474
39.412352
46.815206
Improved Loop:
The loop in the function can be reduced to:
for x = vector
fprintf ('%12f%16f\n', x, f(x));
end