Output argument "T" ( ) not assigned during call to " ".

I am using one function that calls using two others. Here are the functions. I would like to use V as a set of numbers.
function mach_number=mach(V);
mach_number=(V.*10.^3)./(sqrt(1.40.*287.*(temp_alt(alt_velo(V)))));
end
function alt=alt_velo(V);
alt=((.0389*V.^5)-(.8115*V.^4)+(6.537*V.^3)-(25.45*V.^2)+(53.425*V)+3.8234)*(10.^3);
end
function T=temp_alt(h);
if (h >= 25000)
T=(-131.21+.00299.*h)+273.15;
elseif (11000 < h) & (h < 25000);
T=(-56.46+273.15);
elseif (h <= 11000);
T=(15.04-(.00649.*h))+273.15;
end
end
The function mach works when I enter mach(1:1:10) in the command window but when I do mach(.001:.001:1) I get the error below, I do not understand why. Please help.
>>mach(1:1:10)
ans =
Columns 1 through 5
3.12868748903117 5.86169984416974 8.53906953938449 11.0917563079045 13.5302732555593
Columns 6 through 10
15.907404087981 18.006585285012 18.997273867173 17.9653322339638 15.3656938914863
>> mach(.001:.001:1)
Error in temp_alt (line 12)
if (h >= 25000);
Output argument "T" (and maybe others) not assigned during call to "temp_alt".
Error in mach (line 12)
mach_number=(V.*10.^3)./(sqrt(1.40.*287.*(temp_alt(alt_velo(V)))));

3 Comments

Your answer did not work, I want to be able to use V as a set of values that can fall into any of the conditions. Your answer only allows me to use V as a single number.
I just tested my answer there (the version that I corrected before you asked how to call it), and it certainly produces a vector the same length as I give as input.
mm = linspace(0,20,50);
nn = mach(mm);
plot(mm, nn);

Sign in to comment.

 Accepted Answer

See if this construction works for you:
temp_alt = @(h) [((15.04-(.00649.*h))+273.15).*(h<=11000) + (-56.46+273.15).*((11000 < h) & (h < 25000)) + ((-131.21+.00299.*h)+273.15).*(h>=25000)];

6 Comments

Is for a function or script?
It is an anonymous function, so you can put it in you main script if you want to. If you want to set it up as a separate function, this will work:
function T=temp_alt(h)
T = [((15.04-(.00649.*h))+273.15).*(h<=11000) + (-56.46+273.15).*((11000 < h) & (h < 25000)) + ((-131.21+.00299.*h)+273.15).*(h>=25000)];
end
Only the syntax changes. The code doesn’t change.
Worked perfectly! Thanks, how did you know to do this?
My pleasure!
I’m not certain I’m the only one to discover that construction, but I discovered it independently and I’ve been using it for years. As for ‘knowing how to do it’, I just experimented. I tried it and it worked! It may not be the most efficient code, but that approach is easy to code and it’s reliable. It also works and plays well with vectors, and avoids the complexity of if blocks.
Yes it does, thanks again.
As always, my pleasure.

Sign in to comment.

More Answers (2)

The problem is that when you reach this statement
if (h >= 25000)
your variable h is a vector. If you read the documentation for if, you'll notice that this is only going to evaluate as "true" if it is true for every value of h.
With your problematic case, the condition fails every "if" expression evaluation, so T never gets assigned.

1 Comment

So how can I set it so that it works for several values of V? Or is that not possible for Mat Lab. For example the conditions are if h is greater than 25000 than T will be a separate value, if h is between 11000 and 25000 then T will be a different value and so on. Can it be set so that if I enter V as different values then I can get different T values?

Sign in to comment.

2 Comments

Your answer did not work, I want to be able to use V as a set of values that can fall into any of the conditions. Your answer only allows me to use V as a single number.
I tested the answer I posted there and it does certainly work.
mm = linspace(0,20,50);
nn = mach(mm);
plot(mm, nn);

Sign in to comment.

Products

Asked:

G
G
on 14 Sep 2015

Commented:

on 16 Sep 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!