Error message: index must be a positive integer or logical
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (2)
Roger Stafford
le 1 Août 2013
Modifié(e) : Roger Stafford
le 1 Août 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.
Jan
le 1 Août 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.
0 commentaires
Voir également
Catégories
En savoir plus sur Interpolation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!