Getting index outta bounds error for line 38 cant find out why
1 view (last 30 days)
Show older comments
Line 38 is in bold
clear; clc;
fo=fopen('table.txt','w');
fprintf(fo,' z(km) p(Pa) T(K) rho(kg/m^3) \n');
fprintf(fo,'--------------------------------------------------------\n');
R=287.0;
z=0:1000:50000;
z_i=[0 11.0 20.0 32.0 47.0 51.0];
T_i=[288.15 216.65 216.65 228.65 270.65 270.65];
p_i=[101325 22632.05 5474.88 868.02 110.91 66.94];
lambda_i=[0.0065 .0000 -0.001 -0.0028 0.0000];
g_i=[9.810 9.776 9.749 9.712 9.666 9.654];
type=[1 2];
for n = 1:length(z)
if z(n)>=z_i(1)&& z(n)<z_i(2)
z1(n)=z_i(1); p1(n)=p_i(1); T1(n)=T_i(1); lambda1(n)=lambda_i(1);
g1(n)=g_i(1); type1=type(1);
elseif z(n)>=z_i(2)&& z(n)<z_i(3)
z1(n)=z_i(2); p1(n)=p_i(2); T1(n)=T_i(2);lambda1(n)=lambda_i(2);
g1(n)=g_i(2); type1=type(2);
elseif z(n)>=z_i(3)&& z(n)<z_i(4)
z1(n)=z_i(3);p1(n)=p_i(3);T1(n)=T_i(3);lambda1(n)=lambda_i(3);
g1(n)=g_i(3);type1=type(1);
elseif z(n)>=z_i(4)&& z(n)<z_i(5)
z1(n)=z_i(4); p1(n)=p_i(4); T1(n)=T_i(4); lambda1(n)=lambda_i(4);
g1(n)=g_i(4); type1=type(2);
elseif z(n)>=z_i(5)&& z(n)<z_i(6)
z1(n)=z_i(5); p1(n)=p_i(5); T1(n)=T_i(5); lambda1(n)=lambda_i(5);
g1(n)=g_i(5); type1=type(2);
end
if type1== type(1)
*p(n)=p1(n)*(1-(lambda1(n)*(z(n)-z1(n))/T1(n))).^(g1(n)/(lambda1(n)*R));*
T(n)=T1(n)-lambda1(n)*(z(n)-z1(n));
rho(n)=p1(n)/(R*T1(n));
end
if type1== type(2)
p(n)=p1(n)*exp(-1*g1(n)*(z(n)-z1(n))*(1/R)*(1/T1(n)));
T(n)=T1(n);
rho(n)=p1(n)/(R*T1(n));
end
end
fprintf(fo,' %10.0i %10.5e %7.2f %10.5e \n',z,p,T,rho);
fclose(fo);
plot(T,z,p,z,rho,z);
Error Message is Attempted to access p1(2); index out of bounds because numel(p1)=1.
Error in Project_2 (line 38) p(n)=p1(n)*(1-(lambda1(n)*(z(n)-z1(n))/T1(n))).^(g1(n)/(lambda1(n)*R));
1 Comment
Muthu Annamalai
on 22 Sep 2014
Can you please use the '{} Code' button to format your code?
My best effort leads me to,
clear; clc;
fo=fopen('table.txt','w');
fprintf(fo,' z(km) p(Pa) T(K) rho(kg/m^3) \n');
fprintf(fo,'--------------------------------------------------------\n');
R=287.0;
z=0:1000:50000;
z_i=[0 11.0 20.0 32.0 47.0 51.0];
T_i=[288.15 216.65 216.65 228.65 270.65 270.65];
p_i=[101325 22632.05 5474.88 868.02 110.91 66.94];
lambda_i=[0.0065 .0000 -0.001 -0.0028 0.0000];
g_i=[9.810 9.776 9.749 9.712 9.666 9.654];
type=[1 2];
for n = 1:length(z)
if z(n)>=z_i(1)&& z(n)<z_i(2) z1(n)=z_i(1); p1(n)=p_i(1); T1(n)=T_i(1); lambda1(n)=lambda_i(1); g1(n)=g_i(1); type1=type(1);
elseif z(n)>=z_i(2)&& z(n)<z_i(3) z1(n)=z_i(2); p1(n)=p_i(2); T1(n)=T_i(2);lambda1(n)=lambda_i(2); g1(n)=g_i(2); type1=type(2);
elseif z(n)>=z_i(3)&& z(n)<z_i(4) z1(n)=z_i(3);p1(n)=p_i(3);T1(n)=T_i(3);lambda1(n)=lambda_i(3); g1(n)=g_i(3);type1=type(1);
elseif z(n)>=z_i(4)&& z(n)<z_i(5) z1(n)=z_i(4); p1(n)=p_i(4); T1(n)=T_i(4); lambda1(n)=lambda_i(4); g1(n)=g_i(4); type1=type(2);
elseif z(n)>=z_i(5)&& z(n)<z_i(6) z1(n)=z_i(5); p1(n)=p_i(5); T1(n)=T_i(5); lambda1(n)=lambda_i(5); g1(n)=g_i(5); type1=type(2);
end
if type1== type(1)
p(n)=p1(n)*(1-(lambda1(n)*(z(n)-z1(n))/T1(n))).^(g1(n)/(lambda1(n)*R));
T(n)=T1(n)-lambda1(n)*(z(n)-z1(n));
rho(n)=p1(n)/(R*T1(n));
end
if type1== type(2)
p(n)=p1(n)*exp(-1*g1(n)*(z(n)-z1(n))*(1/R)*(1/T1(n)));
T(n)=T1(n);
rho(n)=p1(n)/(R*T1(n));
end
end
fprintf(fo,' %10.0i %10.5e %7.2f %10.5e \n',z,p,T,rho);
fclose(fo);
plot(T,z,p,z,rho,z);
Accepted Answer
Geoff Hayes
on 22 Sep 2014
Edited: Geoff Hayes
on 22 Sep 2014
Brad - if you step through the code, using the debugger, you will notice that on the first iteration of the for loop, n==1, the if condition evaluates to true and all arrays are updated with the first element. On the second iteration of the for loop, n==2, no condition for the if or any elseif evaluates to true and so none of the arrays are updated with the second element. And so at line 32 (or 33) the code tries to access p1(2) in an array that only has 1 element.
Either you need to review your conditions, or add an else statement that sets some default values for your five arrays.
EDIT
Note how your z array is initialized as
z = 0:1000:50000
and so is composed of 0,1000,2000,.. i.e. multiples of 1000. But your z_i array is
z_i=[0 11.0 20.0 32.0 47.0 51.0];
So it is impossible for any value in z except for 0 (the first one) to satisfy any of the conditions which is using z_i as interval bounds for the z data.
4 Comments
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!