difference in ode45 and rk4 code

89 次查看(过去 30 天)
Hi... I have tried to solve a set of non-linear coupled differential equations in matlab by rk4 method. For this i have written a code (but not used any solver like ode45)but during validation results are not matching. Then i have solved a simple equation y'=2*t,y(0)=0 t=[0:0.5:2] the results obtained by the code rk4 exactly matches with ode45 solver. Thats correct means code is working fine. Then i have solved several simple problems from text books of matlab, most cases results are matching. Then i have tried to solve a problem y'=2*t^5; with y(0)=0 t=[0:0.5:2]. But the results obtained by ode45 exactly matches with analytical solution, but results obtained by my own code deviates more, then i have copied a code from text book "APPLIED NUMERICAL METHODS USING MATLAB" by Yang et al. page number 268. The results obtained by my own code matches with results of book code but results of both code deviates from ode45 and analytical results. anybody please tell me the reason behind it...

采纳的回答

Titus Edelhofer
Titus Edelhofer 2018-8-31
Hi Debashis,
I think there is a misunderstanding: ode45 uses the result from the 5th order RK method for the solution, whereas rk4 is a 4th order method. The "4" in the ode45 means, that the 4th order is used for the variable step selection. Now: for "nice" functions like 2*t, both rk4 and rk5 reproduce the result. Therefore you see no difference. For t^5 you see, that the 5th order is still exact (error of ode45 small), whereas 4th order rk4 cannot give exact result (as you see).
Titus
  2 个评论
Debashis Panda
Debashis Panda 2018-8-31
Dear Titus, thank you so much for your replay, your answer is absolutely correct, when i have implemented 5th order Rungi-kutta method in my code i am getting correct result compared with ode45. But another problem that i want to ask you some times in solving a system of coupled equation in my code i was getting NaN error after some iteration, by using 4th order rk method, where as ode45 produce results , i guess it may be due to the order of accuracy, is it correct or not...
Titus Edelhofer
Titus Edelhofer 2018-8-31
Hi Debashis,
NaN typically comes if the step size is too large compared to the dynamics (or the stiffness) of the system. ode45 should seldom suffer from this problem, because it adapts the stepsize automatically (again, as long as the problem is not too stiff). But fixed step solvers like rk4 can run into this problem. Try to reduce the step size.
Titus

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by