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...
0 个评论
采纳的回答
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 个评论
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 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!