A differential equation is an equation that involves derivatives, or differentials. We previously look the differential equation of population growth.

\[\frac{dP}{dt} = kP\]

or

\[P^\prime(t) = k \cdot P(t)\]

where \(P\), or \(P(t)\) is the population at time \(t\). This equation is a model of uninhibited population growth. Its solution is the function

\[P(t) = P_0e^{kt}\]

where the constant \(P_0\) is the size of the population at \(t=0\). In general, differential equations have far reaching applications and have solutions that are functions.

Solving (Certain) Differential Equations

We use the notation \(y^\prime\) to denote a derivative. Thus, if \(y = f(x)\), then

\[y^\prime = \frac{dy}{dx} = f^\prime(x)\]

We can find the solutions to some differential equations by finding their antiderivatives (or indefinite integrals). The differential equation

\[\frac{dy}{dx} = g(x)\] or

\[y^\prime = g(x)\] has the solution

\[y = \int g(x) dx\]

As an example then, let’s solve

\[y^\prime = 2x\]

from sympy import *
from sympy.abc import x

yprime = 2 * x
integrate(yprime, x)
## x**2

Note! I don’t know how to return the constant of integration but the complete answer (accorind to any textbook) should be

\[y = \int 2x dx = x^2 + C\]

Technically, \(2x\) is the general solution to the differential equations. Taking specific values of \(C\) gives particular solutions.

import matplotlib.pyplot as plt
import numpy as np

xvals = np.arange(-5, 5, 0.001)
y0 = [x**2 for x in xvals]
y1 = [x**2 + 3 for x in xvals]
y2 = [x**2 - 3 for x in xvals]

plt.plot(xvals, y0, label="C = 0")
plt.plot(xvals, y1, label="C = 3")
plt.plot(xvals, y2, label="C = -3")
plt.legend(frameon=False)

Let’s solve

\[f^\prime(x) = e^x + 5x - x^{1/2}\] given that \(f(0) = 8\).

from sympy.abc import C
from mpmath import e

yprime = e**x + 5*x - x**(1/2)
f = integrate(yprime, x)
f
## 1.0*2.71828182845905**x + 5*x**2/2 - 0.666666666666667*x**1.5
constant = solve(Eq(8, f.subs(x, 0) + C), C)
constant
## [7.00000000000000]

This means that the particular solution is

f + constant[0]
## 1.0*2.71828182845905**x + 5*x**2/2 - 0.666666666666667*x**1.5 + 7.0

Separation of Variables

There are some differential equations where you might have to use a technique called separation of variables. Let’s take an example from the literature.

In psychology, one model of stimulus-response asserts that the rate of change \(dR/dS\) of the reaction \(R\) with respect to a stimulus \(S\) is inversely proportional to the intensity of the stimulus. That is,

\[\frac{dR}{dS} = \frac{k}{S}\] where \(k\) is some positive constant.

To solve this equation, we first separate the variables and then we integrate both sides to find the solution.

\[dR = k \cdot \frac{dS}{S}\] \[\int dR = \int k \cdot \frac{dS}{S}\] \[R = k \ln S + C\] Unfortunately, I couldn’t figure out how to do this in sympy so I’m leaving it like this.