Systems of differential equations are everywhere. From our relationships and sports games, to complex mathematical problems, differential equations help us analyze the world. They provide concrete models for reality and provide correlations between events, people, and actions. In this Motivation page you will see many of the ways systems of equations impact your life. Whether an engineer or not, systems of differential equations show up everywhere and are crucial to understand in applying your education.
It should be noted that it is often helpful to ground the examples that follow in reality. Read through the examples first and don't focus on the math or equations. Once you have a basic understanding of the problem and solution, and hopefully an appreciation for the elegance of a mathematical model, then look into the method of solution.
Example 1: A balance between protein and mRNA synthesis and degredation/inactivation can be modeled to help in laboratory testing of processes involving biological information. A certain study uses an E. coli cell-free expression system, where the informational elements (mRNA and proteins) are studied outside of the body and their respective cells. Testing processes in these conditions is key to developing new biological engineering applications.
In this example, we examine endogenous mRNA inactivation to better understand gene expression dynamics. The protein used is deGFP, which is a highly translatable green fluorescent protein. The following differential equations describe the rate of change over time of the concentration of deGFP mRNA (m), dark deGFP (deGFPd), and fluorescent deGFP (deGFPf).
clear; syms B m(t) deGFPd(t) a k deGFPf(t); eqns=[diff(m)==-B*m;diff(deGFPd)==a*m-k*deGFPd;diff(deGFPf)==k*deGFPd]The various constants are defined as the protein production rate a, the mRNA inactivation rate B, and the maturation time of the protein 1/k (including folding, etc). The following initial conditions are given in the study as the starting point for the respective assays:
cond=[deGFPf(0)==0;deGFPd(0)==0.25;m(0)==0.25]We solve the above differential equations using these initial conditions to find equations for the concentration of each item in terms of time.
soln=dsolve([eqns;cond]); deGFPd_(t)=soln.deGFPd
m_(t)=soln.m
deGFPf_(t)=soln.deGFPf
Example 2: The differential equation governing the motion of a particle with electric charge q in an electromagnetic field is
Example 3: Suppose that a ball of mass m is released with some initial velocity from a particular point over a surface. Then its position (x,y) is determined from the follwoing initial avlue problem
(*Calculates the position and velocity of the ball after one bounce \ on a given curve*)
(*Calculates the position and velocity of the ball after reflecting \ off of the curve*)
(*Calculates the path of the ball over a specific range of x bounce \ by bounce and plots it*)
ramp[x_] := If[x < 1, 1 - x, 0];
BouncingBall[.7, ramp, {0, 1.25}] |
![]() |
(* 0.7 means the coefficient of rebouncing *)
circle[x_] := If[x < 1, Sqrt[1 - x^2], 0]; BouncingBall[.7, circle, {.1, 1.25}] |
![]() |
|
![]() |
|
![]() |
(*Sine wave ramp*)
sineramp[x_] := If[x < 5, 1 - .5 Sin[x], 0]; BouncingBall[.75, sineramp, {0, 1.25}] |
![]() |
arccosramp[x_] := If[-1 < x < 1, ArcCos[x], 0];
BouncingBall[.5, arccosramp, {-0.1, 2.3}] |
![]() |
(*Logarithmic ramp*)
logramp[x_] := If[x < 2, 2 - Log[x + 1], 0]; BouncingBall[.65, logramp, {0, 3}] |
![]() |
(*Parabolic ramp*)
squareramp[x_] := If[x < 4, x^2, 0]; BouncingBall[.7, squareramp, {.5, 1}] |
![]() |
(*Cubic ramp*)
cuberamp[x_] := If[-1 < x < 5, 4 (x^3) - x, 0]; BouncingBall[.9, cuberamp, {-0.25, .5}] |
![]() |
Example 4: Now we consider a bouncing ball on stairs.
function bouncing_ball _no _animation
%% Parameters
clc;
clear;
P.gravity = 9.81; % gravitational acceleration m/s^2
P.mass = 0.3; % mass of the ball kg
P.drag = 0.02; % quadratic drag coefficient N*s^2/m^2
P.coeff_restitution = 0.95;
P.rollingThreshold = 1e3;
P.maxBounce = 6; % Max number of bounces
P.maxTime = 25; % Max time
% Initial positions/velocities
Pos_X = 0; % m
Pos_Y = 15;
Vel_X = 3; % m/s
Vel_Y = -1;
P.initCond = [Pos_X; Pos_Y; Vel_X; Vel_Y];
P.Options = odeset('RelTol',1e-6,'AbsTol', 1e-6,'Events', @BounceEvent,'Vectorized',true,'MaxStep',0.1);
P.plotTimeStep = 0.005;
P.slowMotionFactor = 3;
P.CurveLineWidth = 3;
P.PlotBallSize = 50;
%% Define Ground/Stairs
function [y, dy] = Ground(x)
y = 12 - 3*heaviside(x - 3) - 3*heaviside(x - 6)- 3*heaviside(x - 9)-3*heaviside(x - 12)-3*heaviside(x - 15);
dy = - 3*dirac(x - 3) - 3*dirac(x - 6) - 3*dirac(x - 9) - 3*dirac(x - 12) - 3*dirac(x - 15);
% y = -.5*x+12;
% dy = -.5;
end
%% Run Simulation
Trajectory = cell(P.maxBounce,1); % Output of ode45
IC = P.initCond; % Save initial conditions
tNow = 0; % Start time
tEnd = P.maxTime; % End if over max time
maxBounce = P.maxBounce; % End if over maxbounces
TermCond = 'Max Bounces'; % For display only
% Loop through each section of the trajectory
for bounceNum=1:maxBounce
Tspan = [tNow,tEnd];
sol = ode45(@(t,X)Dynamics(t,X,P),Tspan,IC,P.Options); % events defined in Options
impactState = sol.y(:,end); % Store the state immediately before bounce
[IC]= impactMap(impactState,P); % Find new start state
tNow = sol.x(end); % Last time = new first time
Trajectory{bounceNum}=sol; % Store ode sol
% Exit condition
if tEnd <= tNow
TermCond = 'Max Time';
break
end
end
disp(TermCond); % Display termination condition
Trajectory = Trajectory(1:bounceNum);
%% ODE
function dZ = Dynamics(~,Z,P)
g = P.gravity;
m = P.mass; % Mass of ball
c = P.drag; % Drag coefficient N*s^2/m^2
% pos = Z(1:2,:);
vel = Z(3:4,:); % m/s
speed = sqrt(vel (1,:).^2+vel (2,:).^2);
Drag = -c*vel.*speed/m;
Gravity = [0;-g]*ones(size(speed));
acc = Drag + Gravity;
dZ = [vel;acc];
end
function [value,isterminal,direction] = BounceEvent(~,X)
x = X(1,:);
y = X(2,:);
ground = Ground(x);
value = y-ground;
isterminal = true; % Stop on ground hit
direction = -1; % Should only be coming to the ground from above
end
function [stateOut] = impactMap(stateIn,P)
posOut = stateIn(1:2,:); % pos in = pos out
velIn = stateIn(3:4,:);
horizPos = stateIn(1,:);
% Get slope of ground at collision
[~,groundSlope] = Ground(horizPos);
c = P.coeff_restitution; % loses energy on bounce
theta = atan2(groundSlope,1); % angle between horizontal axis and ground
R = [cos(theta),sin(theta);-sin(theta),cos(theta)]; % Rotation to find vel out
E = [1,0;0,-c]; % assume only vertical velocity is affected
velNT = E*(R*velIn); % Normal and tangential velocity components
velOut = R\velNT; % Rotate velocity back to the inertial coordinates
stateOut = [posOut;velOut];
end
%% Organize Ode for Plot
timeAll = []; % Stores the interpolated solution for plotting
stateAll = [];
timeOde = []; % Stores the original ode45 solution, stitched
stateOde = [];
% Loop through data, interpolating and then stitching:
for i=1:length(Trajectory)
% Figure out where we want to interpolate the data
dt = P.plotTimeStep;
tStart = Trajectory {i}.x(1);
tFinal = Trajectory {i}.x(end);
time = linspace(tStart,tFinal,round((tFinal-tStart)/dt));
if ~isempty(time)
% interpolate points
state = deval(Trajectory{i},time);
else
disp('Only one grid point in this trajectory section')
time = Trajectory {i}.x;
state = Trajectory {i}.y;
end
heightList = BounceEvent([],state);
if min(heightList)<-P.Options.AbsTol % Height
disp('The ball passed through the ground. Reduce the MaxStep size in P.Options.')
end
timeAll = [timeAll, time];
stateAll = [stateAll, state];
timeOde = [timeOde, Trajectory {i}.x];
stateOde = [stateOde, Trajectory {i}.y];
end
%% Plot Solution
xPos = stateAll(1,:);
yPos = stateAll(2,:);
xGround = linspace(min(xPos),max(xPos),1000);
yGround = Ground(xGround);
figure
hold on
plot(xPos,yPos,'LineWidth',P.CurveLineWidth)
plot(xGround,yGround,'k-','LineWidth',P.CurveLineWidth);
axis padded; axis equal
ylabel('Vertical Position (m)')
xlabel('Horizontal Position (m)')
title('Path taken by the ball')
set(gca);
ylabel('Vertical Position (m)')
xlabel('Horizontal Position (m)')
title('Ball Trajectory')
end
function bouncing_ball
%% Parameters
clc;
clear;
P.gravity = 9.81; % gravitational acceleration m/s^2
P.mass = 0.3; % mass of the ball kg
P.drag = 0.02; % quadratic drag coefficient N*s^2/m^2
P.coeff_restitution = 0.95;
P.rollingThreshold = 1e3;
P.maxBounce = 6; % Max number of bounces
P.maxTime = 25; % Max time
% Initial positions/velocities
Pos_X = 0; % m
Pos_Y = 15;
Vel_X = 3; % m/s
Vel_Y = -1;
P.initCond = [Pos_X; Pos_Y; Vel_X; Vel_Y];
P.Options = odeset('RelTol',1e-6,'AbsTol', 1e-6,'Events', @BounceEvent,'Vectorized',true,'MaxStep',0.1);
P.plotTimeStep = 0.005;
P.slowMotionFactor = 1;
P.CurveLineWidth = 3;
P.PlotBallSize = 50;
%% Define Ground/Stairs
function [y, dy] = Ground(x)
y = 12 - 3*heaviside(x - 3) - 3*heaviside(x - 6)- 3*heaviside(x - 9)-3*heaviside(x - 12)-3*heaviside(x - 15);
dy = - 3*dirac(x - 3) - 3*dirac(x - 6) - 3*dirac(x - 9) - 3*dirac(x - 12) - 3*dirac(x - 15);
% y = -.5*x+12;
% dy = -.5;
end
%% Run Simulation
Trajectory = cell(P.maxBounce,1); % Output of ode45
IC = P.initCond; % Save initial conditions
tNow = 0; % Start time
tEnd = P.maxTime; % End if over max time
maxBounce = P.maxBounce; % End if over maxbounces
TermCond = 'Max Bounces'; % For display only
% Loop through each section of the trajectory
for bounceNum=1:maxBounce
Tspan = [tNow,tEnd];
sol = ode45(@(t,X)Dynamics(t,X,P),Tspan,IC,P.Options); % events defined in Options
impactState = sol.y(:,end); % Store the state immediately before bounce
[IC]= impactMap(impactState,P); % Find new start state
tNow = sol.x(end); % Last time = new first time
Trajectory{bounceNum}=sol; % Store ode sol
% Exit condition
if tEnd <= tNow
TermCond = 'Max Time';
break
end
end
disp(TermCond); % Display termination condition
Trajectory = Trajectory(1:bounceNum);
%% ODE
function dZ = Dynamics(~,Z,P)
g = P.gravity;
m = P.mass; % Mass of ball
c = P.drag; % Drag coefficient N*s^2/m^2
% pos = Z(1:2,:);
vel = Z(3:4,:); % m/s
speed = sqrt(vel (1,:).^2+vel (2,:).^2);
Drag = -c*vel.*speed/m;
Gravity = [0;-g]*ones(size(speed));
acc = Drag + Gravity;
dZ = [vel;acc];
end
function [value,isterminal,direction] = BounceEvent(~,X)
x = X(1,:);
y = X(2,:);
ground = Ground(x);
value = y-ground;
isterminal = true; % Stop on ground hit
direction = -1; % Should only be coming to the ground from above
end
function [stateOut] = impactMap(stateIn,P)
posOut = stateIn(1:2,:); % pos in = pos out
velIn = stateIn(3:4,:);
horizPos = stateIn(1,:);
% Get slope of ground at collision
[~,groundSlope] = Ground(horizPos);
c = P.coeff_restitution; % loses energy on bounce
theta = atan2(groundSlope,1); % angle between horizontal axis and ground
R = [cos(theta),sin(theta);-sin(theta),cos(theta)]; % Rotation to find vel out
E = [1,0;0,-c]; % assume only vertical velocity is affected
velNT = E*(R*velIn); % Normal and tangential velocity components
velOut = R\velNT; % Rotate velocity back to the inertial coordinates
stateOut = [posOut;velOut];
end
%% Organize Ode for Plot
timeAll = []; % Stores the interpolated solution for plotting
stateAll = [];
timeOde = []; % Stores the original ode45 solution, stitched
stateOde = [];
% Loop through data, interpolating and then stitching:
for i=1:length(Trajectory)
% Figure out where we want to interpolate the data
dt = P.plotTimeStep;
tStart = Trajectory {i}.x(1);
tFinal = Trajectory {i}.x(end);
time = linspace(tStart,tFinal,round((tFinal-tStart)/dt));
if ~isempty(time)
% interpolate points
state = deval(Trajectory{i},time);
else
disp('Only one grid point in this trajectory section')
time = Trajectory {i}.x;
state = Trajectory {i}.y;
end
heightList = BounceEvent([],state);
if min(heightList)<-P.Options.AbsTol % Height
disp('The ball passed through the ground. Reduce the MaxStep size in P.Options.')
end
timeAll = [timeAll, time];
stateAll = [stateAll, state];
timeOde = [timeOde, Trajectory {i}.x];
stateOde = [stateOde, Trajectory {i}.y];
end
%% Plot/Animate
xPos = stateAll(1,:);
yPos = stateAll(2,:);
xGround = linspace(min(xPos),max(xPos),1000);
yGround = Ground(xGround);
h=figure
hold on
plot(xPos,yPos)
plot(xGround,yGround)
axis padded; axis equal
Animate(timeAll, stateAll, stateOde, P);
function Animate(timeAll,stateAll,stateOde,P)
clf;
xPos = stateAll(1,:);
yPos = stateAll(2,:);
Pos = [xPos;yPos];
xGround = linspace(min(xPos),max(xPos),1000);
yGround = Ground(xGround);
Extents = [min(xPos),max(xPos),min(yGround),max(yPos)];
tic; % Start a timer for to sync the animation
endTime = timeAll(end);
SlowMo = P.slowMotionFactor;
cpuTime=toc/SlowMo;
counter = 1; % Plotting index
n=0; % for making gif
while (cpuTime
(*corner to corner bounces (c)*)
c= .7;
sol = NDSolve[{y''[t] == -9.81, y[0] == 10, y'[0] == 5, a[0] == 3,
WhenEvent[y[t] == a[t], y'[t] -> -c y'[t]],
WhenEvent[a[t] == y[t], a[t] -> 1 - (a[t])^2],
WhenEvent[ y[t] < a[t] , y''[t] = -y'[t]]}, {y, a}, {t, 0, 10},
DiscreteVariables -> {a}];
Plot[Evaluate[{y[t], a[t]} /. sol], {t, 0, 8},
PlotStyle -> {Thick, Thick}]
(*time based steps*)
c = .5;
sol = NDSolve[{y''[t] == -9.81, y[0] == 10, y'[0] == 5, a[0] == -1,
WhenEvent[y[t] == a[t], y'[t] -> -c y'[t]],
WhenEvent[t == -a[t], a[t] -> a[t] - 3],
WhenEvent[ y[t] < a[t] , y''[t] = -y'[t]]}, {y, a}, {t, 0, 5},
DiscreteVariables -> {a}];
Plot[Evaluate[{y[t], a[t]} /. sol], {t, 0, 8},
PlotStyle -> {Thick, Thick}]
c = 0.8;
sol = NDSolve[{y''[t] == -9.81, y[0] == 10, y'[0] == 6, a[0] == -1,
WhenEvent[y[t] == a[t], y'[t] -> -c y'[t]],
WhenEvent[t == -a[t], a[t] -> a[t] - 3],
WhenEvent[ y[t] < a[t] , y''[t] = -y'[t]]}, {y, a}, {t, 0, 5},
DiscreteVariables -> {a}];
Plot[Evaluate[{y[t], a[t]} /. sol], {t, 0, 8},
PlotStyle -> {Thick, Thick}]
corner to corner bounces
time based steps
elastic bounce
■