Runge-kutta calculation giving lower order of accuracy than expected
6 次查看(过去 30 天)
显示 更早的评论
I'm solving an ODE: y'' + (y^2 − 1)*y' + y = 0, y(0) = 1, y'(0) = 0, t ∈ [0, 1] using RK method with uk = u(k−1) +(h/6)*(K1 + K2 + 4K3)
K1 = f(t(k−1), u(k−1))
K2 = f(t(k−1) + h, u(k−1) + hK1)
K3 = f(t(k−1) + h∕2, u(k−1) + hK1∕4 + hK2∕4)
The program give the correct solution to the problem but when experimenting with different step sizes N the convergence appears to be of order 1, doubling the steps only halves the error. Shouldn't the convergence be much faster than this? Any ideas what I could be doing wrong? In the code I split the ODE into a system of 2 first order ODE
t=0; tslut=100; T=t;
N=4000; h=tslut/N;
y0=1; u1=y0;
dy0=0; u2=dy0;
for k=2:N+1
%The K values
K11 = u2(k-1);
K12 = u2(k-1)+h*K11;
K13 = u2(k-1)+h*K11/4+h*K12/4;
K21 = -(u1(k-1)^2-1)*u2(k-1)-u1(k-1);
K22 = -((u1(k-1)+h*K21)^2-1)*(u2(k-1)+h*K21)-(u1(k-1)+h*K21);
K23 = -((u1(k-1)+h*K21/4+h*K22/4)^2-1)*(u2(k-1)+h*K21/4+h*K22/4)-(u1(k-1)+h*K21/4+h*K22/4);
%Iterating over u1 and u2 using RK method
u1(k)=u1(k-1)+(h/6)*(K11+K12+K13);
u2(k)=u2(k-1)+(h/6)*(K21+K22+K23);
t=t+h;
T=[T;t];
end
plot(T,u1)
u1(end)
0 个评论
回答(1 个)
Abraham Boayue
2018-4-2
Try using a higher order to see if your result will improve. RK4 usually gives better convergence. Check this link https://www.mathworks.com/matlabcentral/answers/386695-how-to-solve-this#answer_308949
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!