File Exchange

image thumbnail

RK45(dydt, trange, yinit, tol)

version (2.51 KB) by Lateef Adewale Kareem
RK45 is like matlab's ode45. its can be used on octave as it comes without ode solver.


Updated 06 Nov 2017

View License

% function solves ordinary differential equation just like ode45.
% dydt = system of first order differential equations
% trange = range of the independent variable
% yinit = initial condition
% tol = error absolute tolerance, default value = 1e-6;
% Example:
% [t,y] = RK45(@(t,y)[y(2); (1-y(1)^2)*y(2)-y(1)],[0 20],[2; 0]);
% subplot(1,2,2);
% plot(t,y,'-o')
% title('Solution of van der Pol Equation (\mu = 1) with RK45');
% xlabel('Time t');
% ylabel('Solution y');
% legend('y_1','y_2');
% [t2,y2] = ode45(@(t,y)[y(2); (1-y(1)^2)*y(2)-y(1)],[0 20],[2; 0]);
% subplot(1,2,1);
% plot(t2,y2,'-o')
% title('Solution of van der Pol Equation (\mu = 1) with ODE45');
% xlabel('Time t');
% ylabel('Solution y');
% legend('y_1','y_2');

Cite As

Lateef Adewale Kareem (2021). RK45(dydt, trange, yinit, tol) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (2)

Ábout the 10^6, I believe no one wants to solve a problem where you would need 1million steps to cover the interval. in such cases, ode15s may be able to cover the interval in fewer steps. and would therefore be more computationally efficient


Global variables are a bad method to provide the outputs. It is not clear why you limit the number of steps to [10, 1e6]. The message "stiff equation" is not necessarily correct, if you reach the minimal step size. It is useful to mention, where you took the parameters of the Runge-Kutta method from.

MATLAB Release Compatibility
Created with R2012b
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!