MATLAB Answers

Getting Error: Unrecognized Function or Variable

39 views (last 30 days)
Spenser Frownfelter
Spenser Frownfelter on 29 Sep 2020 at 0:21
Edited: Walter Roberson on 29 Sep 2020 at 0:47
When calling my aircondition.m function in Task 2, I get this error for my "rho" variable (and presumably P and T). After separately testing the function itself, my variables are defined at the end of the function. Unsure why else I'd be getting this or how I can fix it?
function [rho,P,T] = aircondition(h)
R = 287; %gas constant for air
g = 9.8; %gravity
if h>=0 && h<=11000 %calculations for troposphere
P_init = 101325;
T_init = 288.15;
xi = -0.0065;
T = T_init+(xi*(h-0));
P = P_init*((T/T_init)^((-g)/(xi*R)));
rho = P/(R*T);
elseif h>11000 && h<=20000 %calculations for tropopause
P_init = 22659.44596;
T = 216.65;
P = P_init*exp(((-g)/(R*T))*(h-11000));
rho = P/(R*T);
elseif h>20000 && h<=32000 %calculations for stratosphere 1
P_init = 5487.79142;
T_init = 216.65;
xi = 0.0010;
T = T_init+(xi*(h-20000));
P = P_init*((T/T_init)^((-g)/(xi*R)));
rho = P/(R*T);
elseif h>32000 && h<=47000 %calculations for stratosphere 2
P_init = 871.35791;
T_init = 228.65;
xi = 0.0028;
T = T_init+(xi*(h-32000));
P = P_init*((T/T_init)^((-g)/(xi*R)));
rho = P/(R*T);
end
end
%%Then, calling the function:
m = input('Enter hydrogen mass in kg') + 40; %total balloon mass
delta_t = input('Enter time step in seconds'); %time step input
g = 9.8; %setting up needed constants
R_air = 287;
R_h2 = 4124;
T_s = 110.4;
T_ref = 288.15;
mew_ref = 1.789E-5;
weight = m*g; %weight force
i = 1; %increment counter
h = 0; %initial altitude
w = 0; %initial upward velocity
V = 0; %initial velocity placeholder
t = 0; %initial time
while h(i) < 32000 && V(i) < 5000
[rho,P,T] = aircondition(h(i)); %calculate current air conditions
V(i) = (m*R_h2*T)/P; %calculate current balloon volume
B = rho*V(i)*g; %Bouyancy force
mew = mew_ref*((T/T_ref)^1.5)*((T_ref+T_s)/(T+T_s)); %current mew
Re = (rho*w(i)*2*R_air)/mew; %current Reynolds Num.
Cd = (24/(1+Re))+(6/(1+sqrt(Re)))+0.1; %current Cd coeff.
D = (0.5)*rho*(w(i)^2)*pi*(R_air^2)*Cd; %current Drag Force
a = (B-D-weight)/m; %current acceleration
h(i+1) = h(i)+(w(i)*delta_t); %update height
w(i+1) = w(i)+(delta_t*a); %update velocity
V(i+1) = V(i); %placeholder
i = i+1;
end

  1 Comment

Sindar
Sindar on 29 Sep 2020 at 0:38
this doesn't seem like the issue here, but I'd add an else line with an informative error:
elseif h>32000 && h<=47000 %calculations for stratosphere 2
P_init = 871.35791;
T_init = 228.65;
xi = 0.0028;
T = T_init+(xi*(h-32000));
P = P_init*((T/T_init)^((-g)/(xi*R)));
rho = P/(R*T);
else
error('h=%g is greater than the limit of 47000',h)
end

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 29 Sep 2020 at 0:45
Edited: Walter Roberson on 29 Sep 2020 at 0:47
If your balloon starts descending before it reaches 32000 units, then it will reach the ground, h < 0, which is a condition not defined in your aircondition() function.
g = 9.8; %setting up needed constants
That constant is in the unit meters per second^2
elseif h>11000 && h<=20000 %calculations for tropopause
when I look at wikipedia, it looks to me as if you might be using feet for those boundaries rather than meters ??

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!