Index exceeds matrix dimensions. Error

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!

回答(5 个)

Matthew
Matthew 2012-2-22
Is there a certain format in which questions need to be posted in?

2 个评论

http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup

请先登录,再进行评论。

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 个评论

Walter,
I really appreciate the advice, however after amending the program in the way you suggested the error still occurs. Would you happen to have any other ideas?
Thank you kindly,
Matt
Also,
Does this error state that the initialized P and T vectors are not large enough for the size of the loop? Or that there is a floating point problem that the vectors cannot handle? The prior seemed the most logical, but no matter how large I initialize the receiving vectors the same error occurs. I have never run into this problem before and it is pretty frustrating.
Thanks again,
Matt
Found the problem in the code body. My equation was not formatted correctly where there should have been a f*(x) there was a f(x.

请先登录,再进行评论。

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.
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);

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

提问:

2012-2-21

Community Treasure Hunt

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

Start Hunting!

Translated by