필터 지우기
필터 지우기

Index exceeds matrix dimensions. Error

조회 수: 1 (최근 30일)
Matthew
Matthew 2012년 2월 21일
편집: Image Analyst 2013년 10월 19일
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개)

Rick Rosson
Rick Rosson 2012년 2월 22일
Please format your code.

Matthew
Matthew 2012년 2월 22일
Is there a certain format in which questions need to be posted in?
  댓글 수: 2
Walter Roberson
Walter Roberson 2012년 2월 22일
http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Rick Rosson
Rick Rosson 2012년 2월 22일
Thanks!

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2012년 2월 22일
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
Matthew
Matthew 2012년 2월 22일
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
Matthew
Matthew 2012년 2월 23일
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.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2012년 2월 23일
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.
  댓글 수: 1
Matthew
Matthew 2012년 2월 24일
Thanks Walter, I realized this and added && operators

댓글을 달려면 로그인하십시오.


Andrei Bobrov
Andrei Bobrov 2012년 2월 24일
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);

카테고리

Help CenterFile Exchange에서 Simulink에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by