Numerical Integration in Matlab

1
Haroon Khan
MAT 275 Honors Project
May 4, 2011
Professor Brewer
Numerical Integration in Matlab
In Modern Differential Equations, numerical integration is the part of numerical analysis that
finds solutions to ordinary differential equations. In Matlab, it is possible to not only numerically
integrate the differential equation with initial conditions for the solutions, but also plot in two or three
dimensions. The first example given is on the competition model of rabbits versus sheep. The equations
given are
𝑑π‘₯
= π‘₯ (3 βˆ’ π‘₯ βˆ’ 2𝑦)
𝑑𝑑
𝑑𝑦
= 𝑦 (2 βˆ’ π‘₯ βˆ’ 𝑦)
𝑑𝑑
Figure 1: Competition: Rabbits vs Sheep Model
2
The code used to plot this model is as follows.
% to plot multiple trajectories (i.e., solution curves) on the same screen:
t0=0;
tf=25;
tspan=[t0 tf];
x0=[.2 1 .2 4 4 4 4; .4 4 .3 1 4 3 2.5];
%a matrix with I.C.'s (.2,.4), (1,4), etc.
N=length(x0(1,:)); % # of I.C.'s = # of columns of x0
figure
hold on
5;
for i=1:N
[t,x]=ode45('compete',tspan,x0(:,i));
plot(x(:,1),x(:,2));
end;
xlabel('x');
ylabel('y');
title('Competition between rabbits and sheep')
[u,v]=meshgrid(0:.3:4,0:.3:4);
pu=u.*(3-u-2*v);
pv=v.*(2-u-v);
quiver(u,v,pu,pv);
hold off
axis([0 4 0 4])
function dx=compete(t,x)
%dx is a vector with entries being the right-hand-sides
%of the system of differential equations
%x in our system is x(1) in Matlab
%y in our system is x(2) in Matlab
dx=[ x(1).*(3-x(1)-2*x(2)); x(2).*(2-x(1)-x(2))];
In the first exercise, we consider an alternative predator-prey model. Changing the code in order reflect
the new differential equation, we get as follows.
𝑑π‘₯
= (3 βˆ’ 𝑦)π‘₯
𝑑𝑑
𝑑𝑦
π‘₯
= (βˆ’5 + ) 𝑦
𝑑𝑑
2
3
Figure 2: Predator-Prey Model
The code used to plot this model is as follows.
% to plot multiple trajectories (i.e., solution curves) on the same screen:
t0=0;
tf=25;
tspan=[t0 tf];
x0=[.2 1 .2 4 4 4 4; .4 4 .3 1 4 3 2.5];
%a matrix with I.C.'s (.2,.4), (1,4), etc.
N=length(x0(1,:)); % # of I.C.'s = # of columns of x0
figure
hold on
5;
for i=1:N
[t,x]=ode45('compete',tspan,x0(:,i));
plot(x(:,1),x(:,2));
end;
xlabel('x');
ylabel('y');
title('Predator-Prey Model')
[u,v]=meshgrid(0:.3:4,0:.3:4);
pu=u.*(3-v);
pv=v.*(-5+u/2);
quiver(u,v,pu,pv);
hold off
axis([0 4 0 4])
function dx=compete(t,x)
%dx is a vector with entries being the right-hand-sides
%of the system of differential equations
%x in our system is x(1) in Matlab
%y in our system is x(2) in Matlab
dx=[ x(1).*(3-x(2)); x(2).*(-5+x(1)/2)];
4
In the second exercise, Lorenz equations are used.
𝑑π‘₯
𝑑𝑑
𝑑𝑦
𝑑𝑑
= 𝜌 (𝑦 βˆ’ π‘₯)
= π‘₯ (π‘Ÿ βˆ’ 𝑧) βˆ’ 𝑦
𝑑𝑧
= π‘₯𝑦 βˆ’ 𝑏𝑧
𝑑𝑑
Figure 3: Lorenz Equation
The code used to plot this model is as follows.
x0=[20 20 20];
t0=0; tf=50;
[tout, xout] = ode45('lorenzf', [t0, tf], x0);
figure(1);
hp=plot3(xout(:,1), xout(:,2), xout(:,3));
box on;
axis([-15 15 -20 20 0 60]);
function u = lorenzf(t,x)
p=10;
r= [24.6, 24.9, 28];
b=8./3;
u = [p*(x(2)-x(1)), x(1)*(r - x(3)) - x(2), x(1)*x(2) - b*x(3)]';