Runge-Kutta solution of first to third order ODEs

rk4gui.m utilizes the Runge-Kutta method to directly solve 1-st to 3-rd order non-stiff ODEs which are IVPs (Initial Value Problems).
260 Downloads
Updated 26 Feb 2020

View License

rk4gui.m directly solve 1-st to 3-rd order non-stiff ODEs with the Runge-Kutta method to yield the same results as Matlab’s ode45.

The advantages of this code are that
(a) It eliminates the necessity of recasting ODEs of the 2-nd and 3-rd order as a system of 1-st order ODEs before using ode45.
(b) It provides a convenient way to write the ODE and specify its solution domain, initial conditions, and the solution step size in a convenient GUI menu.

A plot of the solution and its listing are provided. Three verification examples are provided below.
1st order ODE:

Comparison between the numerical and exact solutions
ODE: y' + y = sin(t) + (1+2t)cos(t)
Exact solution: y(t) = t.*(sin(t) + cos(t))
Input: a0=1, a1=1, f(t)=sin(t) + (1+2*t)*cos(t)
t0=0, tn=5, h=0.25, y0=0, y1=1

t y-Numerical y-Exact RelativeError
------ ------------ ------------ -------------
0.00 0.00000 0.00000 0.0000e+00
0.50 0.67850 0.67850 1.5479e-06
1.00 1.38174 1.38177 2.4093e-05
1.50 1.60228 1.60235 4.2614e-05
2.00 0.98621 0.98630 9.4475e-05
2.50 -0.50676 -0.50668 1.6839e-04
3.00 -2.54666 -2.54662 1.6702e-05
3.50 -4.50531 -4.50534 6.5927e-06
4.00 -5.64167 -5.64178 2.0289e-05
4.50 -5.34729 -5.34747 3.3030e-05
5.00 -3.37612 -3.37631 5.6407e-05

2nd order ODE:

ODE: (t^2)*y'' - (2*t)*y' + 2*y = (t^3)*log(t)
Exact solution: y(t) = (7/4)*t + (1/2)*(t.^3).*log(t) - (3/4)*t.^3
Input: n=2, a0=t^2, a1=-2*t, a2=2, f(t)=(t^3)*log(t)
t0=1, tn=2, h=0.1, y(t0)=1, y'(t0)=0

t y-Numerical y-Exact RelativeError
------ ------------ ------------ -------------
0.00 0.00000 0.00000 0.0000e+00
0.50 0.67850 0.67850 1.5479e-06
1.00 1.38174 1.38177 2.4093e-05
1.50 1.60228 1.60235 4.2614e-05
2.00 0.98621 0.98630 9.4475e-05
2.50 -0.50676 -0.50668 1.6839e-04
3.00 -2.54666 -2.54662 1.6702e-05
3.50 -4.50531 -4.50534 6.5927e-06
4.00 -5.64167 -5.64178 2.0289e-05
4.50 -5.34729 -5.34747 3.3030e-05
5.00 -3.37612 -3.37631 5.6407e-05

3rd order ODE:

ODE: (t^3)*y''' - (t^2)*y'' + (3*t)*y' - 4*y = (t^3)*(5*log(t) + 9)
Exact solution: y(t) = -t.^2 + t.*(cos(log(t)) + sin(log(t))) + sin(t)+(1+2*t)*cos(t)t.^3).*log(t)
Input: n=3, a0=-4, a1=3*t, a2=-t^2, a3=t^3, f(t)=(t^3)*(5*log(t) + 9)
t0=1, tn=2, h=0.1, y(t0)=0, y'(t0)=1, y''(t0)=3

t y-Numerical y-Exact RelativeError
------ ------------ ------------ -------------
1.00 0.00000 0.00000 0.0000e+00
1.10 0.11655 0.11655 8.1801e-06
1.20 0.27271 0.27274 9.1345e-05
1.30 0.47893 0.47910 3.6031e-04
1.40 0.74631 0.74700 9.2112e-04
1.50 1.08650 1.08849 1.8306e-03
1.60 1.51153 1.51626 3.1226e-03
1.70 2.03377 2.04354 4.7792e-03
1.80 2.66582 2.68401 6.7790e-03
1.90 3.42046 3.45185 9.0926e-03
2.00 4.31063 4.36158 1.1681e-02

Cite As

Lawrence Agbezuge (2024). Runge-Kutta solution of first to third order ODEs (https://www.mathworks.com/matlabcentral/fileexchange/74358-runge-kutta-solution-of-first-to-third-order-odes), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2019b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.0.1

Added verification examples.

1.0.0