Array indices must be positive integers or logical values.
Mostrar comentarios más antiguos
I am trying to solve an equation by using Runge-Kutta Euler Method. Why do I get "Array indices must be positive integers or logical
values." error?
%Euler Method
%parameters
g=9.81;
rho=1.2;
s=0.00011;
m=0.023;
Cd=0.9;
%Initial Condition
V0=0;
V=0;
%I choose dt as
dt=2;
%time interval
t0=0;
ts=10;
t=0;
i=0;
while dt<ts;
d1=g-0.5.*rho.*V0.^2*(s/m).*Cd;
phiAvg=d1;
V(i)=V0+dt.*phiAvg;
V0=V;
t(i)=t;
i=i+1;
t=t+dt;
end
plot(V,t)
Respuesta aceptada
Más respuestas (3)
John D'Errico
el 16 de Oct. de 2021
Editada: John D'Errico
el 16 de Oct. de 2021
It does not matter that you WANT V(0) to be something. MATLAB does not support zero based indexing. PERIOD.
However, nothing stops you from starting the vector at V(1). And then when you index into the vector, just use V(ind + 1). Now when ind == 0, there is no problem.
If you want to do a plot?
t = 0:1:10; % or whatever it should be
plot(t,V(t+1))
1 comentario
Ali Deniz
el 16 de Oct. de 2021
Image Analyst
el 18 de Oct. de 2021
1 voto
This most FAQQY of FAQs is thoroughly explained in the FAQ:
Categorías
Más información sobre Functions en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!