Error message: index must be a positive integer or logical
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
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.
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.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!