difference in ode45 and rk4 code

80 Ansichten (letzte 30 Tage)
Debashis Panda am 31 Aug. 2018
Kommentiert: Titus Edelhofer am 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 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Titus Edelhofer am 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 KommentareKeine anzeigenKeine ausblenden
Debashis Panda am 31 Aug. 2018
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 am 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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Ordinary Differential Equations finden Sie in Help Center und File Exchange

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by