Why do I get the error saying I did not assign values to a call? I did assign my values after getting them from a function though? Anyone able to help me out and explain?

2 次查看(过去 30 天)
I got the script as attached below. I did assign T P rho and speedsound after getting it from the loop but I am getting the error:
Output argument "P" (and maybe others) not assigned during call to "atmosplots>atmos".
Error in atmosplots (line 14)
[T, P, rho, speedsound] = atmos(h)
%% Atmos function to plot the graphs of T P rho and speedsound for every altitude in ISA Atmosphere
% Calculating T, p ,rho and speedsound for every altitude in the ISA atmosphere
% ------ Range of Altitude ------ %
hlist = linspace(0,20000,2000)
% ------ Array for storing ------ %
% 1: Altitude 2. Temperature 3. Pressure 4. Density 5. Speedsound
plotting_table = zeros(length(hlist),5);
for i=1:length(hlist)
h = hlist(i);
[T, P, rho, speedsound] = atmos(h)
plotting_table(i,1) = hlist(i)
plotting_table(i,2) = T
plotting_table(i,3) = P
plotting_table(i,4) = rho
plotting_table(i,5) = speedsound
end
% ------ Plotting graph (X vs Y)------ %
figure % Plot for whole range of altitude
subplot(2,2,1) % T vs Altitude
plot(plotting_table(:,2), hlist)
title('Temperature variation with altitude')
xlabel('Temperature [K]')
ylabel('Altitude [m]')
grid on
hold on
subplot(2,2,2) % P vs Altitude
plot(plotting_table(:,3), hlist)
title('Pressure variation with altitude')
xlabel('Pressure [Pa]')
ylabel('Altitude [m]')
grid on
hold on
subplot(2,2,3) % Rho vs Altitude
plot(plotting_table(:,4), hlist)
title('Density variation with altitude')
xlabel('Density [kg/m^3]')
ylabel('Altitude [m]')
grid on
hold on
subplot(2,2,4) % Speedsound vs Altitude
plot(plotting_table(:,5), hlist)
title('Speed of sound variation with altitude')
xlabel('Velocity [m/s]')
ylabel('Altitude [m]')
grid on
%% ------ Atmos function ------ %%
% Altitude in m
% T in kelvins
% P in pascals
% rho in kg/m^3
% Speedsound in m/s
function [T, P, rho, speedsound] = atmos(h)
h1 = 11000; % Height of tropopause
h2 = 20000; % End height of table
g = 9.81;
R = 287;
c = 6.51e-3; % temperature lapse dt/dh = - c = -6.51 degcelcius/km
T0 = 15+273.15; % Temperature sea level
p0 = 101325; % pressure sealevel
rho0 = 101325/R/T0; % density sealevel = pressure / R*T, R=287, T = 15 degcelcius
T1 = T0 - c*h1; % Temperature at 11km
p1 = p0 * (T1/T0)^5.2506; % Pressure at 11km
rho1 = rho0 * (T1/T0)^4.2506; % Density at 11km
T2 = T1; % Temperature at 20km
p2 = p1 * exp(-g/(R*T2)*(h2-h1)); % Pressure at 20km
rho2 = rho1 * exp(-g/(R*T2)*(h2-h1)); % Density at 20km
if h <= h1
% disp('Troposphere');
T = T0 - c*h;
p = p0 * (T/T0)^5.2506;
rho = rho0 * (T/T0)^4.2506;
speedsound = (1.4*R*T)^0.5;
elseif h <= h2
% disp('Tropopause');
T = T1;
p = p1 * exp(-g/(R*T)*(h-h1));
rho = rho1 * exp(-g/(R*T)*(h-h1));
speedsound = (1.4*R*T)^0.5;
end
return
end

采纳的回答

Steven Lord
Steven Lord 2022-3-5
You've defined your atmos function to return the value of its local variable P as the second output.
What value should be returned as that second output if not all elements of h are less than or equal to h1 and not all elements of h are less than or equal to h2? What value does your code assign to the local variable P with in that scenario? Does the local variable P even exist in that scenario?

更多回答(2 个)

Matt J
Matt J 2022-3-5
编辑:Matt J 2022-3-5
The error message is complaining that you did not assign a value to P within atmos(). P is named as an output of atmos,
[T, P, rho, speedsound] = atmos(h)
but the function never computes it.

AndresVar
AndresVar 2022-3-5
Your p assignment is lowercase but output is uppercase

Community Treasure Hunt

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

Start Hunting!

Translated by