# Chapter-2-First-order-differential-equations.ipynb

from sympy.physics.mechanics import dynamicsymbols, init_vprinting
init_vprinting()
x, y = dynamicsymbols('x y')
Derivative(x, t)
In [1]:
from sympy import *
from sympy.abc import x, y, n, t, g, m, A, B, u

In [2]:
y = Function('y')
yP = Function('y_+')
yM = Function('y_-')
f = Function('f')
g = Function('g')
ySym = symbols('y')
ySymP = symbols('y_+')
ySymM = symbols('y_-')
p = Function('p')
x_0, y_0 = symbols('x_0 y_0')
dx, dy = symbols('dx dy')
y_1 = symbols('y_1')
mu = Function('mu')
mu_0 = symbols('mu_0')

In [3]:
generalODE = Eq(Derivative(y(x), x), f(x, y(x)))
display(generalODE)

$\displaystyle \frac{d}{d x} y{\left(x \right)} = f{\left(x,y{\left(x \right)} \right)}$

## 2.1 The Euler method

In [ ]:


In [4]:
Eq(y_1, y_0 + DiracDelta(x) * f(x_0, y_0))

Out[4]:
$\displaystyle y_{1} = y_{0} + f{\left(x_{0},y_{0} \right)} \delta\left(x\right)$
In [5]:
DiracDelta(x)

Out[5]:
$\displaystyle \delta\left(x\right)$

## 2.2 Separable equations

In [6]:
diffEq = Eq(g(y(x)) * Derivative(y(x), x), f(x), evaluate = False)
display(diffEq)

$\displaystyle g{\left(y{\left(x \right)} \right)} \frac{d}{d x} y{\left(x \right)} = f{\left(x \right)}$
In [7]:
IC = Eq(y(x_0), y_0)
display(IC)

$\displaystyle y{\left(x_{0} \right)} = y_{0}$
In [8]:
solveDiffEq = Eq(Integral(g(y(x)) * Derivative(y(x), x), (x, x_0, x)),
Integral(f(x), (x, x_0, x)), evaluate = False)
display(solveDiffEq)

$\displaystyle \int\limits_{x_{0}}^{x} g{\left(y{\left(x \right)} \right)} \frac{d}{d x} y{\left(x \right)}\, dx = \int\limits_{x_{0}}^{x} f{\left(x \right)}\, dx$
In [9]:
valOfU = Eq(u, y(x))
valOfDiffU = Eq(Derivative(u, x),  Derivative(y(x), x))
valOfYAtX_0 = Eq(y(x_0), y_0)
valOfYAtX = Eq(y(x), y, evaluate = False)

display(valOfU)
display(valOfDiffU)
display(valOfYAtX_0)
display(valOfYAtX)

$\displaystyle u = y{\left(x \right)}$
$\displaystyle \frac{d}{d x} u = \frac{d}{d x} y{\left(x \right)}$
$\displaystyle y{\left(x_{0} \right)} = y_{0}$
$\displaystyle y{\left(x \right)} = y$
In [10]:
solveDiffEq = Eq(Integral(g(u), (u, y_0, y)),
Integral(f(x), (x, x_0, x)), evaluate = False)
display(solveDiffEq)

$\displaystyle \int\limits_{y_{0}}^{y} g{\left(u \right)}\, du = \int\limits_{x_{0}}^{x} f{\left(x \right)}\, dx$
In [11]:
solveDiffEq = Eq(Integral(g(ySym), (ySym, y_0, ySym)),
Integral(f(x), (x, x_0, x)), evaluate = False)
display(solveDiffEq)

$\displaystyle \int\limits_{y_{0}}^{y} g{\left(y \right)}\, dy = \int\limits_{x_{0}}^{x} f{\left(x \right)}\, dx$
In [12]:
solveDiffEq = Eq(g(ySym) * Derivative(ySym, x) , f(x))
display(solveDiffEq)

$\displaystyle g{\left(y \right)} \frac{d}{d x} y = f{\left(x \right)}$
In [13]:
dsolve(diffEq, y(x))

Out[13]:
$\displaystyle \int\limits^{y{\left(x \right)}} g{\left(y \right)}\, dy = C_{1} + \int f{\left(x \right)}\, dx$

### with intial condition $$y{\left(0 \right)} = 2$$

In [14]:
diffEq = Eq(Derivative(y, x) + y(x)/2 , Rational(3, 2), evaluate = False)
display(diffEq)

$\displaystyle \frac{y{\left(x \right)}}{2} + \frac{d}{d x} y = \frac{3}{2}$
In [15]:
IC = Eq(y(0), 2)
display(IC)

$\displaystyle y{\left(0 \right)} = 2$
In [16]:
solveDiffEq = Eq(dy / (3 - ySym) , Rational(1, 2) * dx, evaluate = False)
display(solveDiffEq)

$\displaystyle \frac{dy}{3 - y} = \frac{dx}{2}$
In [17]:
solveDiffEq = Eq(Integral(1/ (3 - ySym), (ySym, 2, y)) , Integral(Rational(1, 2), (x, 0, x)), evaluate = False)
display(solveDiffEq)

$\displaystyle \int\limits_{2}^{y} \frac{1}{3 - y}\, dy = \int\limits_{0}^{x} \frac{1}{2}\, dx$
In [18]:
solveDiffEq = Eq(ln(3 - ySym), -Rational(1, 2) * x, evaluate = False)
display(solveDiffEq)

$\displaystyle \log{\left(3 - y \right)} = - \frac{x}{2}$
In [19]:
solveDiffEq = Eq(3 - ySym, exp(-Rational(1, 2) * x), evaluate = False)
display(solveDiffEq)

$\displaystyle 3 - y = e^{- \frac{x}{2}}$
In [20]:
solveDiffEq = Eq(y, 3 - exp(-Rational(1, 2) * x), evaluate = False)
display(solveDiffEq)

$\displaystyle y = 3 - e^{- \frac{x}{2}}$
In [21]:
checkSoln = Eq(Eq(y(0), 3 - exp(-Rational(1, 2) * 0, evaluate = False)), 2, evaluate = False)
display(checkSoln)

$\displaystyle y{\left(0 \right)} = 3 - e^{0} = 2$

### with intial condition $$y{\left(0 \right)} = 4$$

In [22]:
diffEq = Eq(Derivative(y, x) + y(x)/2 , Rational(3, 2), evaluate = False)
display(diffEq)

$\displaystyle \frac{y{\left(x \right)}}{2} + \frac{d}{d x} y = \frac{3}{2}$
In [23]:
IC = Eq(y(0), 4)
display(IC)

$\displaystyle y{\left(0 \right)} = 4$
In [24]:
solveDiffEq = Eq(Integral(1/ (3 - ySym), (ySym, 2, y)) , Integral(Rational(1, 2), (x, 0, x)), evaluate = False)
display(solveDiffEq)

$\displaystyle \int\limits_{2}^{y} \frac{1}{3 - y}\, dy = \int\limits_{0}^{x} \frac{1}{2}\, dx$
In [25]:
solveDiffEq = Eq(-ln(-3 + ySym), -Rational(1, 2) * x, evaluate = False)
display(solveDiffEq)

$\displaystyle - \log{\left(y - 3 \right)} = - \frac{x}{2}$
In [26]:
solveDiffEq = Eq(-3 + ySym, exp(-Rational(1, 2) * x), evaluate = False)
display(solveDiffEq)

$\displaystyle y - 3 = e^{- \frac{x}{2}}$
In [27]:
solveDiffEq = Eq(y, 3 + exp(-Rational(1, 2) * x), evaluate = False)
display(solveDiffEq)

$\displaystyle y = 3 + e^{- \frac{x}{2}}$
In [28]:
checkSoln = Eq(Eq(y(0), 3 + exp(-Rational(1, 2) * 0, evaluate = False)), 4, evaluate = False)
display(checkSoln)

$\displaystyle y{\left(0 \right)} = e^{0} + 3 = 4$

### (ii) For what value of $x > 0$ is $y(x)$ maximum?

In [29]:
diffEq = Eq(Derivative(y, x), 2 * cos(2*x)/(3+2*ySym), evaluate = False)
display(diffEq)

$\displaystyle \frac{d}{d x} y = \frac{2 \cos{\left(2 x \right)}}{2 y + 3}$
In [30]:
IC = Eq(y(0), -1)
display(IC)

$\displaystyle y{\left(0 \right)} = -1$
In [31]:
solveDiffEq = Eq((3 + 2 *ySym) * dy , 2 * cos(2*x) * dx, evaluate = False)
display(solveDiffEq)

$\displaystyle dy \left(2 y + 3\right) = 2 dx \cos{\left(2 x \right)}$
In [32]:
solveDiffEq = Eq(Integral(3 + 2 *ySym, (ySym, -1, y)) , Integral( 2 * cos(2*x), (x, 0, x)), evaluate = False)
display(solveDiffEq)

$\displaystyle \int\limits_{-1}^{y} \left(2 y + 3\right)\, dy = \int\limits_{0}^{x} 2 \cos{\left(2 x \right)}\, dx$
In [33]:
solveDiffEq = Eq(3*ySym + ySym**2 + 2, sin(2*x), evaluate = False)
display(solveDiffEq)

$\displaystyle y^{2} + 3 y + 2 = \sin{\left(2 x \right)}$
In [34]:
solveDiffEq = Eq(3*ySym + ySym**2 + 2 - sin(2*x), 0, evaluate = False)
display(solveDiffEq)

$\displaystyle y^{2} + 3 y - \sin{\left(2 x \right)} + 2 = 0$
In [35]:
solveDiffEqPlus = Eq(ySymP, Rational(1, 2) *( - 3 + sqrt(1 + 4 *sin(2*x))), evaluate = False)
display(solveDiffEqPlus)

$\displaystyle y_{+} = \frac{\sqrt{4 \sin{\left(2 x \right)} + 1}}{2} - \frac{3}{2}$
In [36]:
solveDiffEqMinus = Eq(ySymM, Rational(1, 2) *( - 3 - sqrt(1 + 4 *sin(2*x))), evaluate = False)
display(solveDiffEqMinus)

$\displaystyle y_{-} = - \frac{\sqrt{4 \sin{\left(2 x \right)} + 1}}{2} - \frac{3}{2}$
In [37]:
checkSolnP = Eq(yP(0), Rational(1, 2) *( - 3 + sqrt(1 + 4 *sin(2*0))), evaluate = False)
display(checkSolnP)

$\displaystyle \operatorname{y_{+}}{\left(0 \right)} = -1$
In [38]:
checkSolnM = Eq(yM(0), Rational(1, 2) *( - 3 - sqrt(1 + 4 *sin(2*0))), evaluate = False)
display(checkSolnM)

$\displaystyle \operatorname{y_{-}}{\left(0 \right)} = -2$
In [39]:
Soln = Eq(y, Rational(1, 2) *( - 3 + sqrt(1 + 4 *sin(2*x))), evaluate = False)
display(Soln)

$\displaystyle y = \frac{\sqrt{4 \sin{\left(2 x \right)} + 1}}{2} - \frac{3}{2}$

## 2.3 Linear equations

In [40]:
diffEq = Eq(p(x) * ySym + Derivative(ySym, x), g(x), evaluate = False)
display(diffEq)

$\displaystyle y p{\left(x \right)} + \frac{d}{d x} y = g{\left(x \right)}$
In [41]:
IC = Eq(y(x_0), y_0)
display(IC)

$\displaystyle y{\left(x_{0} \right)} = y_{0}$
In [42]:
solveDiffEq = Eq(mu(x)*(p(x) * ySym + Derivative(ySym, x)), mu(x) * g(x), evaluate = False)
display(solveDiffEq)

$\displaystyle \left(y p{\left(x \right)} + \frac{d}{d x} y\right) \mu{\left(x \right)} = g{\left(x \right)} \mu{\left(x \right)}$
In [43]:
solveDiffEqLHS = Eq(mu(x)*(p(x) * ySym + Derivative(ySym, x)), Derivative(mu(x) * ySym, x), evaluate = False)
display(solveDiffEqLHS)

$\displaystyle \left(y p{\left(x \right)} + \frac{d}{d x} y\right) \mu{\left(x \right)} = \frac{\partial}{\partial x} y \mu{\left(x \right)}$
In [44]:
solveDiffEq = Eq(Derivative(mu(x) * ySym, x), mu(x) * g(x), evaluate = False)
display(solveDiffEq)

$\displaystyle \frac{\partial}{\partial x} y \mu{\left(x \right)} = g{\left(x \right)} \mu{\left(x \right)}$
In [45]:
valOfMuAtX_0 = Eq(mu(x_0), mu_0)
valOfYAtX_0 = Eq(y(x_0), y_0)

display(valOfMuAtX_0)
display(valOfYAtX_0)

$\displaystyle \mu{\left(x_{0} \right)} = \mu_{0}$
$\displaystyle y{\left(x_{0} \right)} = y_{0}$
In [46]:
solveDiffEq = Eq(mu(x) * ySym - mu_0 * y_0 ,
Integral(mu(x) * g(x), (x, x_0, x)), evaluate = False)
display(solveDiffEq)

$\displaystyle - \mu_{0} y_{0} + y \mu{\left(x \right)} = \int\limits_{x_{0}}^{x} g{\left(x \right)} \mu{\left(x \right)}\, dx$
In [47]:
solveDiffEq = Eq(y,
(Integral(mu(x) * g(x), (x, x_0, x)) + mu_0 * y_0)/ mu(x),
evaluate = False)
display(solveDiffEq)

$\displaystyle y = \frac{\mu_{0} y_{0} + \int\limits_{x_{0}}^{x} g{\left(x \right)} \mu{\left(x \right)}\, dx}{\mu{\left(x \right)}}$
In [48]:
solveDiffEqLHS = Eq(mu(x)*p(x) * ySym + mu(x)*Derivative(ySym, x),
Derivative(mu(x) * y(x), x).doit().subs(y(x), ySym), evaluate = False)
display(solveDiffEqLHS)

$\displaystyle y \mu{\left(x \right)} p{\left(x \right)} + \mu{\left(x \right)} \frac{d}{d x} y = y \frac{d}{d x} \mu{\left(x \right)} + \mu{\left(x \right)} \frac{d}{d x} y$
In [49]:
solveDiffEqLHS = Eq(Derivative(mu(x), x),
p(x) * mu(x), evaluate = False)
display(solveDiffEqLHS)

$\displaystyle \frac{d}{d x} \mu{\left(x \right)} = \mu{\left(x \right)} p{\left(x \right)}$
In [50]:
valOfMu_X = Eq(mu(x), mu_0 * exp(Integral(p(x), (x, x_0, x))))
display(valOfMu_X)

$\displaystyle \mu{\left(x \right)} = \mu_{0} e^{\int\limits_{x_{0}}^{x} p{\left(x \right)}\, dx}$
In [53]:
soln = Eq(y,
(Integral(mu(x) * g(x), (x, x_0, x)) + mu_0 * y_0)/ mu(x),
evaluate = False)
display(soln)

$\displaystyle y = \frac{\mu_{0} y_{0} + \int\limits_{x_{0}}^{x} g{\left(x \right)} \mu{\left(x \right)}\, dx}{\mu{\left(x \right)}}$

### with intial condition $$y{\left(0 \right)} = \frac{3}{4}$$

In [58]:
diffEq = Eq(Derivative(y(x), x) + 2 * y(x) , exp(-x), evaluate = False)
display(diffEq)

$\displaystyle 2 y{\left(x \right)} + \frac{d}{d x} y{\left(x \right)} = e^{- x}$
In [55]:
IC = Eq(y(0), Rational(3, 4))
display(IC)

$\displaystyle y{\left(0 \right)} = \frac{3}{4}$
In [57]:
valOfMu_X = Eq(mu(x), exp(2*x))
display(valOfMu_X)

$\displaystyle \mu{\left(x \right)} = e^{2 x}$
In [62]:
solveDiffEq = Eq(y, exp(-2*x) * (Rational(3, 4) + Integral(exp(2*x) * exp(-x), (x, 0, x))), evaluate = False)
display(solveDiffEq)

$\displaystyle y = \left(\int\limits_{0}^{x} e^{x}\, dx + \frac{3}{4}\right) e^{- 2 x}$
In [65]:
solveDiffEq = Eq(y, exp(-2*x) * (Rational(3, 4) + Integral(exp(2*x) * exp(-x), (x, 0, x))).doit(), evaluate = False)
display(solveDiffEq)

$\displaystyle y = \left(e^{x} - \frac{1}{4}\right) e^{- 2 x}$
In [71]:
soln = Eq(y, exp(-x) * (1 - Rational(1, 4) * exp(x)), evaluate = False)
display(soln)

$\displaystyle y = \left(1 - \frac{e^{x}}{4}\right) e^{- x}$
In [77]:
checkSoln = Eq(Eq(y(0), exp(-x) * (1 - Rational(1, 4) * exp(x))), Rational(3, 4), evaluate = False)
display(checkSoln)

$\displaystyle y{\left(0 \right)} = \left(1 - \frac{e^{x}}{4}\right) e^{- x} = \frac{3}{4}$
In [ ]: