Effacer les filtres
Effacer les filtres

Why won't the arrays I make work in the functions I created where I need to find pressure?

3 vues (au cours des 30 derniers jours)
I need to find pressure but the arrays won't plug into my functions as I keep getting D = 0. Is there any ways to fix this? Thanks!
%% Calculating Pressure
% Gives 201 values
for x = 0:30000:6000000
% calculates the dome height at every x value
D = calc_dome_height(x);
% for every x value get all the verticle locationso
for z = 0 : 50 : 10000
% calculates the reference temperature
ref_T = calc_ref_temp(z,D);
% calculates the reference pressure
ref_P = calc_ref_pressure(z,D);
% calculates the pressure
p = calc_pressure(ref_P,ref_T,z,D);
end
end
Unrecognized function or variable 'calc_warm_temp'.

Error in solution>calc_pressure (line 83)
temp_w = calc_warm_temp(z);
function [D] = calc_dome_height(x)
if x < 1000000
D = 0;
elseif (1000000 <= x) && (x <= 5000000)
D = 5000 .* sin((pi/4000000).*(x - 1000000));
elseif x >= 5000000
D = 0;
end
end
function [temp_c] = calc_cold_temp(z, D)
temp_c = 300 - (0.004 - -0.007) .* D - -0.007* z;
end
function [ref_T] = calc_ref_temp(z, D)
if z <= D
ref_T = 300;
elseif z > D
ref_T = 300 - (0.004 .* D);
end
end
function [ref_P] = calc_ref_pressure(z,D)
if z <= D
ref_P = 1000;
elseif z > D
temp_c = calc_cold_temp(D,D);
ref_P = 1000* ((temp_c/300)^(9.81/(287*-0.007)));
end
end
function [pressure] = calc_pressure(ref_P, ref_T, z, D)
if z <= D
temp_c = calc_cold_temp(z,D);
pressure = ref_P * ((temp_c / ref_T)^(9.81/(287 * -0.007)));
elseif z > D
temp_w = calc_warm_temp(z);
pressure = ref_P * ((temp_w / ref_T)^(9.81/(287* 0.004)));
end
end
  2 commentaires
Walter Roberson
Walter Roberson le 19 Oct 2023
We need the calc_warm_temp function to test with.
Torsten
Torsten le 19 Oct 2023
What exactly do you mean ? As you can see, D is not always equal to 0.
iter = 0;
for x = 0:30000:6000000
% calculates the dome height at every x value
iter = iter + 1;
D(iter) = calc_dome_height(x);
end
plot(1:iter,D)
function [D] = calc_dome_height(x)
if x < 1000000
D = 0;
elseif (1000000 <= x) && (x <= 5000000)
D = 5000 .* sin((pi/4000000).*(x - 1000000));
elseif x >= 5000000
D = 0;
end
end

Connectez-vous pour commenter.

Réponses (2)

Walter Roberson
Walter Roberson le 19 Oct 2023
D = calc_dome_height(x);
That overwrites all of D for every iteration of the for loop, so at the end of the for loop, D is going to be whatever it was calculated to be for the last iteration. Which is going to e 0 because of the
elseif x >= 5000000
D = 0;
You are not recording each D as it is produced.

Matthew
Matthew le 19 Oct 2023
Here's the code with everything. I want to go through each element of the x array and z array in order to get pressure at each value. Is there anyway to do that so D doesn't always equal zero? Thanks.
% Gives 201 values
% calculates the dome height at every x value
for x = 0:30000:6e6;
D = calc_dome_height(x);
for z = 0:50:10000;
ref_T2 = calc_ref_temp(z,D);
% calculates the reference pressure
ref_P2 = calc_ref_pressure(z,D);
% calculates the pressure
p1 = calc_pressure(ref_P2,ref_T2,z,D);
end
end
function D = calc_dome_height(x)
if all(x < 1000000)
D = 0;
elseif (1000000 <= x) && (x <= 5000000)
D = 5000 .* sin((pi/4000000).*(x - 1000000));
elseif all(x > 5000000)
D = 0;
end
end
% Reference Temperature Calculation
function ref_T = calc_ref_temp(z, D)
if z <= D
ref_T = 300;
elseif z > D
ref_T = 300 - (0.004 .* D);
end
end
function temp_c = calc_cold_temp(z, D)
temp_c = 300 - (0.004 - -0.007) .* D - -0.007* z;
end
% Warm Teperature Calculation
function [temp_w] = calc_warm_temp(z)
temp_w = 300 - 0.004 * z;
end
% Reference Pressure Calculation
function [ref_P] = calc_ref_pressure(z,D)
if z <= D
ref_P = 1000;
elseif z > D
temp_c = calc_cold_temp(D,D);
ref_P = 1000* ((temp_c/300)^(9.81/(287*-0.007)));
end
end
% Pressure Calculation
function [pressure] = calc_pressure(ref_P, ref_T, z, D)
if z <= D
temp_c = calc_cold_temp(z,D);
pressure = ref_P * ((temp_c / ref_T)^(9.81/(287 * -0.007)));
elseif z > D
temp_w = calc_warm_temp(z);
pressure = ref_P * ((temp_w / ref_T)^(9.81/(287* 0.004)));
end
end

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by