Runge Kutta 3 ODE

조회 수: 19 (최근 30일)
Hugo B
Hugo B 2022년 2월 4일
답변: Alan Stevens 2022년 2월 4일
Part of bigger project. The task:
4. Implement the Runge-Kutta-3 (RK3) solver in Matlab and use it to compute the time evolution of the positions of the three bodies. For an ODE in general form y 0 = f(t, y) the RK3 method is defined as follows:
K1 = hf(ti , yi)
K2 = hf(ti + 1 2 h, yi + 1 2K1)
K3 = hf(ti + h, yi − K1 + 2K2)
yi+1 = yi + 1 6 (K1 + 4K2 + K3) where ti = t0 + ih.
As the name suggests, it is of third order accuracy.
Our code so far:
for i=1:n
t(i)=t(0)+i*h;
K1=h*f(t(i),y(i));
K2=h*f(t(i)+h/2,y(i)+K1/2);
K3=h*f(t(i)+h,y(i)-K1+2*K2);
y(i+1)=y(i)+(K1+4*K2+K3)/6;
end
We have tried putting n=different numbers but dont know what we are doing.
Error message:
Array indices must be positive integers or logical values.
Error in RK3 (line 7)
t(i)=t(0)+i*h;
What is the problem and how do solve the error?

채택된 답변

Alan Stevens
Alan Stevens 2022년 2월 4일
You can't have
t(i)=t(0)+i*h;
indices must be a positive integer - you have t(0), Matlab doesn't like this!

추가 답변 (0개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by