differential equations are used to model
countless physical systems from pricing
Financial derivatives to determining
chemical equilibria or predicting the
spread of disease ordinary differential
equations or Odes for short are a subset
of differential equations that only
involve derivatives with respect to one
independent variable for example time
logistic population growth provides us
with an example of a single OD modeling
a system in its differential equation
the rate that population changes at any
given time is determined by the current
population and some constants here's an
example of a solution to a logistic
differential equation while we could
solve the OD by hands mat lab saves us
the work let's dive into how to solve
Odes in mat lab in this video we'll
explore the use of both built-in
numerical and symbolic OD solvers
built-in numerical OD solvers include OD
45 most popularly but also OD 23 OD 15i
and many more for the sake of time we'll
only cover OD 45 but you can find more
information on the difference between
the functions at the link in the
description the symbolic OD solver we'll
use is desolve we'll start by using OD
45 to model population data for Tokyo
from 1950 to 2019 then use that model to
extrapolate the future population up to
2050 first let's load our data set we
can quickly see how the population
increases by plotting the population
over Thea of dates now on to the
differential equation I've already used
the curve fitting toolbox in mat lab to
determine what constants will make our
logistic OD model the empirical data
well we need to assign a starting value
for the OD solver to use as a reference
since our differential equation only
tells us the rate of change we'll also
create a time span to input into OD 45
we're only modeling up to 2050 or 100
years past 1950 in order to prevent
deterioration of the model for the
differential equation itself we need to
use a function handle that accepts both
population and time even though the OD
itself does not depend on time putting
it all together OD 45 first needs the
differential equation we just declared
next we simply pass in the time span and
initial value and output the result into
a Time array and a population array
plotting the results we can see that the
differential equation models the
population extremely well symbolic
solving of the same equation is slightly
different instead of using an anonymous
function to define the OD we'll be using
the symbolic toolbox to assign P to be a
symbolic function of T to assign the
differential equation to a variable we
use the diff function set equal to our
OD note that we assign with one equal
sign but need to use the double equal
sign to indicate equality within the
derivative we similarly establish our
initial value by setting the value of
our function at time zero equal to the
first entry in the population array
next the dove equation takes our
symbolic OD and starting value and
outputs a symbolic function while OD 45
returns an array of solutions at each
time step indicated in tspan dve Returns
the solution function itself the
symbolic function similarly matches the
real world data as it did before if we
plot the two models on top of each other
we see that the two methods give us
incredibly similar results the
difference in predicted 2050 population
between them is only 14 people out of
over 38 million when comparing the time
it took to run each script ODU 45 was
slightly faster it's not a big gap but
the time difference can become
significant with a more complex system
another example of differential
equations at work in the Real Worlds is
with the transfer of heat Newton's law
of cooling shows that the rate of
temperature change is proportional to
the temperature difference with the
surroundings while the differential
equation for temperature change holds
individually it can also be used to
calculate the temperature of the system
as as we're about to do in our example
we'll determine how the temperature in
three floors of a house changes over 12
hours initializing the script is much
the same as it was before with OD 45
however since we are now dealing with a
system of differential equations we will
need to pass in a column array of
initial values likewise the OD
declaration function will have to return
an N by1 array containing each od's rate
of change for any given combination of
floor temperatures instead of its input
variable representing a single number
like population it will now be an array
of each floor's current temperature to
make this more visually apparent I'll
initialize the function at the bottom of
the script the OD 45 function Remains
the Same though we still need to use
function handles to pass in a function
of only T and H we still use a time span
and we still have an initial set of
conditions the output however will now
be an array of the three floors
temperatures at every corresponding time
in t we can easily plot it to visualize
the results T solve is modified in
almost exactly the same way instead of
only passing one equation into the
function we now input an N by1 array
containing the entire OD system the
initial values will also need to be
inputed as an array the output however
is different instead of being the single
function as it was before it is now a
symbolic structure we can obtain the
individual solutions by using dot
notation as shown while we now have
functions that can evaluate the
temperature of each floor at any desired
point in time the symbol R of solving
this OD system took significantly longer
than the built-in numerical OD solver
did let's recap some similarities and
differences between the two methods we
explored the most obvious difference
between them is that OD 45 and other
built-in numerical solvers return
solution arrays whereas the dool
function returns a symbolic solution OD
45 is also faster to run than dool
especially when handeling a system of
differential equations depending on the
situation one solver may be more apps
than the other or it might not make a
big difference either way the choice is
yours thank you