difference in ode45 and rk4 code
65 views (last 30 days)
Show older comments
Debashis Panda
on 31 Aug 2018
Commented: Titus Edelhofer
on 31 Aug 2018
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 Comments
Accepted Answer
Titus Edelhofer
on 31 Aug 2018
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 Comments
Titus Edelhofer
on 31 Aug 2018
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
More Answers (0)
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!