Index exceeds matrix dimensions. Error
1 view (last 30 days)
Show older comments
This is the error I receive
_??? Index exceeds matrix dimensions.
Error in ==> CalcPress at 111
P = pb*((tb/(tb + lb(h - hb)))^(g/(Rs*lb)));
Error in ==> P_2_1_1 at 30
[P(j),T(j)] = CalcPress(h);_
Script....................
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
istep = 1000;
imax = 100000;
h = 0;
j = 1;
for i = 0:istep:imax
h = i;
[P(j),T(j)] = CalcPress(h); %%%%line 30 %%%%%%
j = j+1;
end
Function......
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Constants;
EngineSpec;
FlightChar;
lb = 0;
tb = 0;
pb = 0;
hb = 0;
L = [-0.0065,0.0,0.001,0.0028,0.0,-0.0028,-0.002]; %temp lapse rate : K/m
Ts = [288.15,216.65,216.65,228.65,270.65,270.65,214.65]; %standard temp : K
Pa = [101325,22632.1,5474.89,868.019,110.906,66.9389,3.95642]; %static pressure : Pa
H = [0,11000,20000,32000,47000,51000,71000]; %height at b : meters
b = [0,1,2,3,4,5,6];
%%%%%%%%%%Determine proper values of lb, tb, pb, hb %%%%%%%%%%%%%%%%%%%
if h <= H(2)
lb = L(1);
tb = Ts(1);
hb = H(1);
pb = Pa(1);
end
if H(2) <= h <= H(3)
lb = L(2);
tb = Ts(2);
hb = H(2);
pb = Pa(2);
end
if H(3) <= h <= H(4)
lb = L(3);
tb = Ts(3);
hb = H(3);
pb = Pa(3);
end
if H(4) <= h <= H(5)
lb = L(4);
tb = Ts(4);
hb = H(4);
pb = Pa(4);
end
if H(5) <= h <= H(6)
lb = L(5);
tb = Ts(5);
hb = H(5);
pb = Pa(5);
end
if H(6) <= h <= H(7)
lb = L(6);
tb = Ts(6);
hb = H(6);
pb = Pa(6);
end
if H(7) <= h
lb = L(7);
tb = Ts(7);
hb = H(7);
pb = Pa(7);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%% Calc Pressure %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if lb ~= 0
P = pb*((tb/(tb + lb(h - hb)))^(g/(Rs*lb))); %%%line 111 %%%%
end
if lb == 0
P = pb*exp((-g*(h - hb))/(Rs*tb));
end
return;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Any clues on how to fix this error? Appreciate your time and effort!
0 Comments
Answers (5)
Matthew
on 22 Feb 2012
2 Comments
Walter Roberson
on 22 Feb 2012
http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Walter Roberson
on 22 Feb 2012
In your script, before your line
for i = 0:istep:imax
insert the lines
P = zeros(imax+1,1);
T = zeros(imax+1,1);
3 Comments
Walter Roberson
on 23 Feb 2012
Note: your expression
H(2) <= h <= H(3)
will be interpreted as
((H(2) <= h) <= H(3))
The first sub-expression is a logical comparison, false (value 0) or true (value 1). The second comparison would then be comparing that 0 or 1 to H(3).
There is no built-in range comparison operator in MATLAB.
Andrei Bobrov
on 24 Feb 2012
try variant
% Your Function, input: h
L = [-0.0065,0.0,0.001,0.0028,0.0,-0.0028,-0.002].'; %temp lapse rate : K/m
Ts = [288.15,216.65,216.65,228.65,270.65,270.65,214.65].';%standard temp : K
% static pressure : Pa
Pa = [101325,22632.1,5474.89,868.019,110.906,66.9389,3.95642].';
H = [0,11000,20000,32000,47000,51000,71000].'; %height at b : meters
b = [0,1,2,3,4,5,6];
[bin,bin] = histc(h,[H(:),inf]);
P = zeros(numel(h),1);
lb = L(bin);
x = [L(bin) Ts(bin), H(bin), pb(bin)];
t1 = lb ~= 0;
t2 = ~t1;
x1 = x(t1,:);
x2 = x(t2,:);
P(t1) = x1(:,4).*((x1(:,2)./(x1(:,2) + x1(:,1).*(h - x1(:,3)))).^(g/(Rs*x1(:,1))));
P(t2) = x2(:,4).*exp((-g*(h - x2(:,3)))./(Rs*x2(:,2)));
% Your Script
h = 0:1e3:1e5;
[P,..] = CalcPress(h);
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!