Using ode45 (Runge-Kutta 4th and
5th order) to solve differential
equations
Examples of ode45
• One dependent variable with anonymous function
• First-order reaction dy/dt = -0.3*y
f1=@(t,y) -0.3*y;
[t,y]=ode45(f1,[0 10],[100]);
set(gca, 'fontsize',14,'fontweight','bold');
%sets the fontsize of the axis numbers
plot(t,y)
xlabel('time (min)','fontsize',16)
ylabel('y','fontsize',16)
• One dependent variable with inline function
• First-order reaction dy/dt = -0.3*y
f2=inline('-0.3*y','t','y');
>> [t,y]=ode45(f2,[0 10],[100]);
plot(t,y)
xlabel('time (min)','fontsize',16)
ylabel('time (min)','fontsize',16)
>> set(gca, 'fontsize',14,'fontweight','bold');
Examples of ode45 with function M-file
One dependent variable with a function m-file (most common)
First-order reaction dy/dt = -0.4*y
function dy = firstorder(t,y)
%this is the function m file
%first-order differential equation
k=0.4;
dy(1)= -k*y(1);
end
%Solve the differential equation
t=0:0.2:10;
>> tspan=0:0.2:10;
>> [t,y]=ode45(@firstorder,tspan,[100]);
set(gca, 'fontsize',14,'fontweight','bold');
>> xlabel('time (min)','fontsize',16)
ylabel('time (min)','fontsize',16)
From Dr. Maneval, Bucknell University
The height of a fluid in a tank (h(t)) whose outlet flow is dependent on the pressure
head (height of fluid) inside the tank and whose inlet flow is a function of time may be
modeled via the equations d h = ( t ) b h ; h ( 0 ) = h In p u t flo w : ( t ) = 1 0 4 s in ( t )
dt
0
Find the solution, h(t), for 0 < t < 30 if the following parameters are given: b = 2 h0 = 1
function dhdt=tankfill(t,h)
%RHS function for tank-fill problem
A= 10 + 4*sin(t); %alpha(t)
H = 2*sqrt(h); %beta*sqrt(h)
dhdt = A - H;
end
tspan = [0 30];
>> h0 = 1;
>> [t,h]=ode45(@tankfill,tspan,h0);
>> plot(t,h)
>> figure
>> plot(t,h)
>> tspan=linspace(0,30,300);
>> [t,h]=ode45(@tankfill,tspan,h0);
>> figure
>> plot(t,h)
Example of ode45 with a system of
equations
• Solve the pair of ODEs from t= 0 to 0.4 using step size of 0.1.
Initial conditions are y(0) = 2 and z(0) = 4.
dy
= 2 y 4e
t
dt
dz
dt
=
yz
2
3
function dy = pair(t,y)
%example of pair of differential equations
dy=zeros(2,1); %make sure dy is a column vector
dy(1)= -2.*y(1)+4*exp(-t);
dy(2)= -(y(1).*y(2).^2)/3;
end
Solve the ode’s and plot
tspan=0:0.1:0.4;
[t,y]=ode45(@pair,tspan,[2 4]);
figure
hold on
set(gca, 'fontsize',14,'fontweight','bold'); %increase font size of plot axes
xlabel('time (min)','fontsize',16,'fontweight','bold')
>> plot(t,y(:,1),'-r',t,y(:,1),'s','Markerfacecolor','r','markeredgecolor','r')
>> plot(t,y(:,2),'-b',t,y(:,2),'o','Markerfacecolor','b','markeredgecolor','b')
Results for pair.m
Results:
y(t, length(y0))
2.00
1.98
1.93
1.87
1.78
4.00
3.16
2.62
2.25
1.98
Passing parameter values from ode45 to generic
function
• What if we want to solve the function many
times, each with different parameter values?
dy
= ay be
• Example:
dt
t
function dy = pair(t,y,a,b,c)
dy(1)= -a.*y(1)+b*exp(-t); dy(2)= -(y(1).*y(2).^2)/c;
end
tspan=0:0.1:0.4;
a=2; b= 4; c=3;
[t,y]=ode45(@pair,tspan,[2 4],[ ], a,b,c);
dz
dt
=
yz
c
2
© Copyright 2026 Paperzz