How do you save variables to vectors and plot them?

This is my code:
valueOfP = [];
valueOfD = [];
valueOfT = [];
valueOfH = [];
Irrelevant code that functions on its own
end
fprintf(' Altitude is %d Temp = %d Pressure = %d Density = %d \n', h, t, p, d)
end
valueOfP(h) = p;
valueOfD(h) = d;
valueOfT(h) = t;
valueOfH(h) = h;
plot(valueOfP,valueOfH)
plot(valueOfT,valueOfH)
plot(valueOfD,valueOfH)
I keep getting error messages when I try to run the code. How would I go about saving the variables and then plotting pvs h, t vs h, and d vs h?

3 comentarios

madhan ravi
madhan ravi el 14 de Mzo. de 2019
dipidi dapudi doo , totally vague
madhan ravi
madhan ravi el 14 de Mzo. de 2019
post your code or upload your script file
Macy Walters
Macy Walters el 14 de Mzo. de 2019
Editada: madhan ravi el 14 de Mzo. de 2019
g = 9.807;
r = 287;
a1 = -.0065;
a3 = -.0045;
a2 = .003;
a4 = .004;
e = exp(1);
valueOfP = [];
valueOfD = [];
valueOfT = [];
valueOfH = [];
h1=0;
T1 = 288.16;
P1 = 101325;
D1 = 1.225;
P0 = P1*(1+(a1/T1)*(h-h1))^(g/(a1*r));
D0 = D1*((1+a1/T1)*(h-h1))^(g/(a1*r)+1);
h2=11;
tx1 = 216.66;
px1 = P0*e^((-g/(r*t0)*(h-h2)));
dx1 = D0*e^((-g/(r*t0)*(h-h2)));
h3=25;
t2=216.66;
p2 = p1*(1+(a2/t2)*(h-h3))^(g/(a2*r));
d2 = d1*((1+a2/t2)*(h-h3))^(g/(a2*r)+1);
h4=47;
t3 = 282.66;
p3 = p2*e^((-g/(r*t3)*(h-h4)));
d3 = d2*e^((-g/(r*t3)*(h-h4)));
h5=53;
t4 = 282.66;
p4 = p3*(1+(a3/t4)*(h-h5))^(g/(a3*r));
d4 = d3*((1+a3/t4)*(h-h5))^(g/(a3*r)+1);
h6=79;
t5 = 165.66;
p5 = p4*e^((-g/(r*t5)*(h-h6)));
d5 = d4*e^((-g/(r*t5)*(h-h6)));
h7=90;
t6 = 265.66;
p6 = p5*(1+(a4/t6)*(h-h7))^(g/(a4*r));
d6 = d5*((1+a4/t6)*(h-h7))^(g/(a4*r)+1);
h=0;
for h=0:.5:105
if (0<=h&&h<11||53<=h&&h<79)
if(0<=h&&h<11)
h1=0;
t1 = 288.16;
p1 = 101325;
d1 = 1.225;
t = t1+a1*(h-h1);
p = P0*(1+(a1/t1)*(h-h1))^(g/(a1*r));
d = D0*((1+a1/t1)*(h-h1))^(g/(a1/r)+1);
else
h1=53;
t1 = 282.66;
t = t1+a3*h;
p = p4*(1+(a3/t1)*(h-h1))^(g/(a3*r));
d = d4*((1+a3/t1)*(h-h1))^(g/(a3/r)+1);
end
elseif(11<=h&&h<25||47<=h&&h<53||79<=h&&h<90)
if(11<=h&&h<25)
h1=11;
t = 216.66;
p = px1*e^((-g/(r*t)*(h-h1)));
d = dx1*e^((-g/(r*t)*(h-h1)));
elseif(47<=h&&h<53)
h1=47;
t = 282.66;
p = p3*e^((-g/(r*t)*(h-h1)));
d = d3*e^((-g/(r*t)*(h-h1)));
else
h1=79;
t = 165.66;
p = p5*e^((-g/(r*t)*(h-h1)));
d = d5*e^((-g/(r*t)*(h-h1)));
end
disp('The temperature is')
disp(t)
disp('The pressure is ')
disp(p)
disp('The density is ')
disp(d)
else
if(25<=h&&h<47)
h1=25;
t1=216.66;
t = t1+(a2*(h-h1));
p = p2*(1+(a2/t1)*(h-h1))^(g/(a2*r));
d = d2*((1+a2/t1)*(h-h1))^(g/(a2*r)+1);
elseif(90<=h&&h<105)
h1=90;
t1 = 165.66;
t = t1+a4*(h-h1);
p = p6*(1+(a4/t1)*(h-h1))^(g/(a4*r));
d = d6*((1+a4/t1)*(h-h1))^(g/(a4*r)+1);
end
end
fprintf(' Altitude is %d Temp = %d Pressure = %d Density = %d \n', h, t, p, d)
end
valueOfP(h) = p;
valueOfD(h) = d;
valueOfT(h) = t;
valueOfH(h) = h;
plot(valueOfP,valueOfH)
plot(valueOfT,valueOfH)
plot(valueOfP,valueOfH)

Iniciar sesión para comentar.

Respuestas (2)

KSSV
KSSV el 14 de Mzo. de 2019
x = zeros([],1) ;
y = zeros([],1) ;
for i = 1:10
x(i) = i ;
y(i) = rand ;
end
plot(x,y) ;

2 comentarios

Macy Walters
Macy Walters el 14 de Mzo. de 2019
Error in HWM3 (line 131)
valueOfP(h) = p;
Say That i tried to run the code and got this, how would i fix this error? Thank you
KSSV
KSSV el 14 de Mzo. de 2019
You should specify the error.....how you expect us to correct it without code and showing us the error.

Iniciar sesión para comentar.

Stephen23
Stephen23 el 14 de Mzo. de 2019
Editada: Stephen23 el 14 de Mzo. de 2019

0 votos

You define h to have values that are NOT positive integer:
for h=0:.5:105
which means that you cannot use h as an index. The simplest solution is to define a vector of h values, and loop over its indices. For your code you will need to do something like this:
hvec = 0:.5:105; % vector of h values
N = numel(hvec);
Pvec = nan(1,N); % preallocate output
Dvec = nan(1,N); % preallocate output
Tvec = nan(1,N); % preallocate output
for k = 1:N % loop over indices
h = hvec(k);
... your code
Pvec(k) = p;
Dvec(k) = d;
Tvec(k) = t;
end
plot(hvec,Pvec)
figure()
plot(hvec,Dvec)
figure()
plot(hvec,Tvec)

Categorías

Más información sobre Sparse Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 14 de Mzo. de 2019

Editada:

el 14 de Mzo. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by