# Error message: index must be a positive integer or logical

1 view (last 30 days)
Catherine on 1 Aug 2013
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

Roger Stafford on 1 Aug 2013
Edited: Roger Stafford on 1 Aug 2013
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.
Catherine on 1 Aug 2013
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 on 1 Aug 2013
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.