Pressure calculation loop error

I have to calculate the pressure at different altitudes. I have the code that calculate all of the variables to go into my pressure calculation and all of those run fine and seem to produce the values that I am looking for but when I try to run the pressure loop it says the sides have different values. Can anyone tell me where I am going wrong?
This is what I have so far:
clc;
clear all;
p(1)=1.013e5;
r=287; %Jules/kg*K
dz=200;
% =================== Gravity Calculations ============================
alt=0:200:50000;
Re=6.37e6;
g0=9.80665;
i=1:length(alt);
g=g0*((Re^2)./((alt(i)+Re).^2));
% =================== Temperature Calculations ============================
for i=1:length(alt)
if alt(i)<=11000
t(i)=((-71.5/11000).*alt(i)+15)+273;
elseif 11000<alt(i) && alt(i)<=20100
t(i)=(-56.5)+273;
elseif 20100<alt(i) && alt(i)<=32200
t(i)=((12/12100).*alt(i)-76.434)+273;
elseif 32200<alt(i) && alt(i)<=47300
t(i)=((42/15100).*alt(i)-134.063)+273;
else
t(i)=(-2.5)+273;
end
x=g./(r*t(i));
end
for i=1:length(alt)
p(i+1)=p(i).*((1/2)*x.*dz)+(1-(1/2)*x.*dz);
end

5 comentarios

Geoff Hayes
Geoff Hayes el 26 de Jun. de 2020
John - x is a 1x251 array, so the line of code
p(i+1)=p(i).*((1/2)*x.*dz)+(1-(1/2)*x.*dz);
is trying to assign a 1x251 array on the right-hand side to the 1x1 scalar on the left-hand side. Is this what you want, or do you mean to use the ith element of x in the calculation. SImilarly, in your for loop, you have
x=g./(r*t(i));
where g is the 1x251 array. Do you mean for x to be a 1x251 array where each element of this array changes on every iteration? Or should this be
x(i) = g(i)./(r*t(i));
?
KSSV
KSSV el 26 de Jun. de 2020
You should replace the last loop to this:
for i=1:length(alt)-1
p(i+1)=p(i).*((1/2)*x(i)*dz)+(1-(1/2)*x(i)*dz);
end
John Woods
John Woods el 26 de Jun. de 2020
I tried your way KSSV but it still gave me an "Unable to perform assignment because the left and right sides have a
different number of elements." Error. Geoff, I am trying to get the code to calculate the pressure at each itteration of the altitude. All the other sections of the code are giving me the values that I am looking for so unless they are messing up my pressure calculations, I'd like to leave them how they are.
KSSV
KSSV el 26 de Jun. de 2020
If you replace the loop with the given..I am not getting any error.....your error is becuse you are using x, replace ot with x(i).
John Woods
John Woods el 26 de Jun. de 2020
Oh okay I just changed the for line. I got it now. Thank you so much!

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 26 de Jun. de 2020

Comentada:

el 26 de Jun. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by