Error message: index must be a positive integer or logical

조회 수: 2 (최근 30일)
Catherine
Catherine 2013년 8월 1일
Hi,
Here are my codes and I am not sure why this error message came up as i is surely an integer and positive.
coshym = input('What is the value of coshym? ')
Qsurf = input('What is Qsurf? ')
Qstep = Qsurf/100;
Q = 0:Qstep:100;
X = zeros(1,length(Q)); X(1) = 0;
F_QX = ((Q.*Q/2+coshym).^2-1).^(-1/2);
for i = 1:(length(Q)-1)
k_1 = F_QX(Q(i),X(i));
k_2 = F_QX(Q(i)+0.5*Qstep,X(i)+0.5*Qstep*k_1);
k_3 = F_QX((Q(i)+0.5*Qstep),(X(i)+0.5*Qstep*k_2));
k_4 = F_QX((Q(i)+Qstep),(X(i)+k_3*Qstep));
X(i+1) = X(i) + (1/6)*(k_1+2*k_2+2*k_3+k_4)*Qstep;
end

답변 (2개)

Roger Stafford
Roger Stafford 2013년 8월 1일
편집: Roger Stafford 2013년 8월 1일
The variables 'Q' and 'F_QX' are defined as row vectors in your code. Yet you have accessed them as two dimensional arrays. That won't work. Also the quantity 'X(1)' is zero and invalid as an index in the expression for 'k_1'. A third problem is that values of 'F_QX' were calculated using square root and are almost certainly not integers. Hence the values of k_1 are not integers and cannot be used in calculating indices in k_2. The same problem occurs for k_3 and k_4.
  댓글 수: 1
Catherine
Catherine 2013년 8월 1일
Thanks for the reply. I can fix the two vectors. But F_QX will always be non integer. Is there another form of Runge-Kutta method that I can use which allows me to get around this problem? I am trying to solve the F_QX equation with numerical method. Thanks.

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


Jan
Jan 2013년 8월 1일
It is not the value of F_QX which causes the problems, but its indices. Runge-Kutta methods do not obtain the input as a vector, because this would be a job for trapz. If you ask your favorite search engine to look for other implementations of Runge-Kutta methods in Matlab, you will find dozens of nice and working examples.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by