SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 6.1.1 Differential equations, and separable equationsConcept · 35 min
      2. 6.1.2 Linear first-order equations and integrating factorsConcept · 35 min
      3. 6.1.3 Practice set: first-order equationsPractice set · 25 min
      4. 6.1.4 Case study: bond prices under a deterministic short rateCase study · 35 min

Level 6, Differential equations 6.1 First-order ODEs Lesson 2 of 4

ConceptLesson 6.1.2 in Level 6, Differential equations. About 35 minutes.

Linear first-order equations and integrating factors

Linear equations

A first-order equation is linear when yy and y′y' appear only on their own, each multiplied by a function of tt alone. Every such equation can be written with y′y' first, with coefficient 1:

Rule: Linear first-order equation

y′+p(t) y=q(t)y' + p(t)\,y = q(t)

This is the standard form. p(t)p(t) multiplies yy, and q(t)q(t), the forcing, holds every term without yy. Either may be a constant.

Bring an equation to standard form before reading off pp and qq:

  • y′=0.8(2−y)y' = 0.8(2 - y) from lesson 6.1.1 becomes y′+0.8y=1.6y' + 0.8y = 1.6, so p=0.8p = 0.8 and q=1.6q = 1.6.
  • y′=t+yy' = t + y becomes y′−y=ty' - y = t. Moving yy to the left flips its sign: p=−1p = -1.
  • 2y′+4y=62y' + 4y = 6 becomes y′+2y=3y' + 2y = 3 after dividing by 2.

Squares and products of yy break linearity. y′=y2y' = y^2, the logistic y′=y(1−y4)y' = y\left(1 - \frac{y}{4}\right), y′=sin⁡yy' = \sin y and y y′=ty\,y' = t are not linear.

When q=0q = 0, the equation y′+p(t) y=0y' + p(t)\,y = 0 is homogeneous. It is separable as well (lesson 6.1.1): dyy=−p dt\frac{dy}{y} = -p\,dt gives y=Ce−∫p dty = Ce^{-\int p\,dt}. When q≠0q \neq 0 it usually does not separate, and needs a new idea.

The integrating factor

The product rule (lesson 2.3.2) gives (μy)′=μy′+μ′y(\mu y)' = \mu y' + \mu' y for any function μ(t)\mu(t), written with the Greek letter mu. Multiply the standard form by μ\mu:

μy′+μp y=μq\mu y' + \mu p\,y = \mu q

The left side is (μy)′(\mu y)' exactly when μ′=p μ\mu' = p\,\mu. That is a separable equation for μ\mu, solved by μ=e∫p dt\mu = e^{\int p\,dt}, since ddteg(t)=g′(t) eg(t)\frac{d}{dt}e^{g(t)} = g'(t)\,e^{g(t)} (lesson 2.3.4). This μ\mu is the integrating factor.

Rule: Integrating factor

μ(t)=e∫p(t) dt(μy)′=μ qy=1μ(∫μ q dt+C)\begin{gathered} \mu(t) = e^{\int p(t)\,dt} \\[4pt] (\mu y)' = \mu\,q \\[4pt] y = \frac{1}{\mu}\Big(\int \mu\,q\,dt + C\Big) \end{gathered}

Multiply by μ\mu, integrate both sides, then divide by μ\mu. Any antiderivative of pp will do: another constant only rescales μ\mu, and the rescaling cancels. The +C+ C is divided by μ\mu too.

A constant pp

Solve y′+2y=6y' + 2y = 6 with y(0)=0y(0) = 0.

  1. 1
    μ=e2t\mu = e^{2t}
  2. 2
    (e2ty)′=6e2t(e^{2t}y)' = 6e^{2t}
  3. 3
    e2ty=3e2t+Ce^{2t}y = 3e^{2t} + C
  4. 4
    y=3−3e−2ty = 3 - 3e^{-2t}
tt00.5123
y=3−3e−2ty = 3 - 3e^{-2t}01.8963622.5939942.9450532.992564

The equation is also y′=2(3−y)y' = 2(3 - y), the equilibrium case of lesson 6.1.1 with k=2k = 2 and M=3M = 3, and separation gives the same answer. Whenever pp and qq are constants, the solution is y=qp+(y0−qp)e−pty = \frac{q}{p} + \left(y_0 - \frac{q}{p}\right)e^{-pt}.

A forcing that changes: y′+y=ty' + y = t

With y(0)=1y(0) = 1, the integrating factor is ete^{t}, so (ety)′=t et(e^t y)' = t\,e^t. Integration by parts (lesson 2.6.2) gives ∫t et dt=(t−1)et+C\int t\,e^t\,dt = (t - 1)e^t + C.

ety=(t−1)et+Cy=t−1+Ce−t\begin{aligned} e^t y &= (t - 1)e^t + C \\ y &= t - 1 + Ce^{-t} \end{aligned}

The start y(0)=1y(0) = 1 gives −1+C=1-1 + C = 1, so C=2C = 2 and y=t−1+2e−ty = t - 1 + 2e^{-t}. This equation does not separate: y′=t−yy' = t - y is a difference, not a product.

tt01246
yy10.7357591.2706713.0366315.004958
t−1t - 1−1-10135
2e−t2e^{-t}20.7357590.2706710.0366310.004958

Try it

linear_solve follows the rule: it builds μ\mu, integrates μq\mu q, adds CC and divides by μ\mu. Each line prints the general solution, the CC that matches the start, and the residual y′+py−qy' + py - q.

The four rows are this lesson's examples. The third, p=−1p = -1, is y′=t+yy' = t + y from lesson 6.1.1.

⌘/Ctrl + Enter runs the cell

A steady part plus a decaying part

Every solution of y′+y=ty' + y = t is t−1t - 1 plus Ce−tCe^{-t}. The first part is the same for every start. The second carries the start, and it dies away: by t=6t = 6 it is 0.004958 of the 5.004958 in the table.

Rule: Two parts of a solution

y=yp(t)+C e−∫p dty = y_{\text{p}}(t) + C\,e^{-\int p\,dt}

ypy_{\text{p}} is any one solution, called a particular solution; here the steady part t−1t - 1. The second part is the general solution of the homogeneous equation y′+py=0y' + py = 0. Where p>0p > 0 it decays, and the start is forgotten.

With a constant p>0p > 0, the start is forgotten at the rate pp. Every 1p\frac{1}{p} units of time, the gap to the steady part shrinks by a factor of ee. For y′+2y=6y' + 2y = 6 the steady part is the equilibrium 3; for y′+y=ty' + y = t it is a moving line.

With p=1p = 1, the part Ce−tCe^{-t} decays and every solution approaches t−1t - 1. With p=−1p = -1, the part CetCe^{t} grows, and only the start y(0)=−1y(0) = -1 stays on the line −t−1-t - 1.

When the second part grows

A negative pp turns decay into growth. y′=t+yy' = t + y is y′−y=ty' - y = t, with p=−1p = -1 and μ=e−t\mu = e^{-t}. Parts gives ∫t e−t dt=−(t+1)e−t\int t\,e^{-t}\,dt = -(t + 1)e^{-t}, so y=−t−1+Cety = -t - 1 + Ce^{t}.

From y(0)=1y(0) = 1, C=2C = 2 and y=2et−t−1y = 2e^t - t - 1: 3.436564 at t=1t = 1, 11.778112 at t=2t = 2 and 104.1963 at t=4t = 4. Only C=0C = 0, the start y(0)=−1y(0) = -1, stays on the line. Like the equilibrium 0 of y′=0.5yy' = 0.5y, the line is unstable.

Because the equation is linear, the difference of any two solutions solves the homogeneous equation. Module 6.2 builds the general solutions of second-order equations from the same fact.

A pp that changes with tt

Solve y′+2ty=2ty' + 2ty = 2t with y(0)=3y(0) = 3. Now ∫2t dt=t2\int 2t\,dt = t^2, so μ=et2\mu = e^{t^2} and (et2y)′=2t et2(e^{t^2}y)' = 2t\,e^{t^2}.

The substitution u=t2u = t^2, du=2t dtdu = 2t\,dt (lesson 2.6.1) gives ∫2t et2 dt=et2+C\int 2t\,e^{t^2}\,dt = e^{t^2} + C. So et2y=et2+Ce^{t^2}y = e^{t^2} + C, then y=1+Ce−t2y = 1 + Ce^{-t^2}, and the start fixes C=2C = 2.

tt00.511.52
y=1+2e−t2y = 1 + 2e^{-t^2}32.5576021.7357591.2107981.036631

The steady part is 1. The decaying part 2e−t22e^{-t^2} dies faster than any exponential e−kte^{-kt}, because p=2tp = 2t keeps growing. This equation is also y′=2t(1−y)y' = 2t(1 - y), which separates; both methods give the same answer.

Where pp breaks down

Take y′+yt=1y' + \frac{y}{t} = 1 with y(1)=1y(1) = 1. Here p=1tp = \frac{1}{t} has no value at t=0t = 0, so work on t>0t > 0. There ∫1t dt=ln⁡t\int \frac{1}{t}\,dt = \ln t (lesson 2.5.2), and μ=eln⁡t=t\mu = e^{\ln t} = t.

So (ty)′=t(ty)' = t, then ty=t22+Cty = \frac{t^2}{2} + C, and y(1)=1y(1) = 1 gives C=12C = \frac{1}{2}: y=t2+12ty = \frac{t}{2} + \frac{1}{2t}. It is 1.25 at t=2t = 2 and 5.05 at t=0.1t = 0.1, blowing up as tt approaches the break in pp. The decaying part 12t\frac{1}{2t} fades only like 1t\frac{1}{t}, slower than an exponential.

Case study 6.1.4 meets a homogeneous equation with a changing pp, y′=−(0.03+0.01t) yy' = -(0.03 + 0.01t)\,y, as the price of a bond.

Try it

The tables for y′+y=ty' + y = t and for its sign-flipped twin y′−y=ty' - y = t, both from y(0)=1y(0) = 1.

Then the changing pp: SymPy builds μ=t\mu = t for p=1tp = \frac{1}{t} when told t>0t > 0, and the last lines print both solutions at a few times.

⌘/Ctrl + Enter runs the cell

Solving numerically with solve_ivp

An integrating factor needs ∫μq dt\int \mu q\,dt, which often has no formula, and pp or qq may come only as data. Then the equation is solved numerically. SciPy's solve_ivp(fun, t_span, y0) does it far better than Euler's method of lesson 6.1.1.

  • fun(t, y) returns the slope f(t,y)f(t, y), with t first.
  • t_span is the interval, such as (0, 6), and y0 the list of starting values. The solver also handles several equations at once, so one value still goes in a list.
  • The answer sol.y has one row per unknown, so sol.y[0] holds yy at the times sol.t. t_eval asks for chosen times.

It chooses its own steps. Its default method, RK45, estimates each step's error and shrinks the step until that estimate is small enough. Where the solution is smooth, it lengthens the step again.

Rule: Tolerances

error per step≲atol+rtol×∣y∣defaults: rtol=10−3, atol=10−6\begin{gathered} \text{error per step} \lesssim \text{atol} + \text{rtol} \times |y| \\[4pt] \text{defaults: rtol} = 10^{-3}, \ \text{atol} = 10^{-6} \end{gathered}

rtol is the relative accuracy asked for, and atol an absolute floor, which matters when yy is near 0. The defaults give about three digits. Set both whenever the digits matter.

For y′=t−yy' = t - y with y(0)=1y(0) = 1, the exact value at t=6t = 6 is 5.004958. Tighter tolerances cost more evaluations of fun, but far fewer than Euler's method needs:

methodevaluations of fferror at t=6t = 6
solve_ivp, default tolerances442×10−42 \times 10^{-4}
rtol=1e-6, atol=1e-91164×10−74 \times 10^{-7}
rtol=1e-10, atol=1e-126207×10−117 \times 10^{-11}
Euler, h=0.0001h = 0.000160,0001.5×10−61.5 \times 10^{-6}

With the tight tolerances, the values at tt = 1, 2, 4 and 6 match the table above to all six decimals.

Try it

f is the slope t−yt - y. The loop solves to t=6t = 6 with three settings of the tolerances and prints the number of evaluations, sol.nfev, and the error, rounded to one figure.

Then 60,000 Euler steps for comparison, and the values at chosen times with t_eval.

⌘/Ctrl + Enter runs the cell

When atol decides the answer

Take y′=−10yy' = -10y with y(0)=1y(0) = 1, whose exact value at t=3t = 3 is e−30≈9.36×10−14e^{-30} \approx 9.36 \times 10^{-14}. With the defaults, solve_ivp returns about 2.12×10−72.12 \times 10^{-7}, two million times too large.

Nothing failed: the solution fell below atol=10−6\text{atol} = 10^{-6} by t≈1.38t \approx 1.38, and from then on any value that small counts as accurate enough. Even atol=10−12\text{atol} = 10^{-12} leaves the answer off by about half, because 10−1310^{-13} is still below it. With atol=10−25\text{atol} = 10^{-25}, far below the solution, the relative error drops to about 7×10−87 \times 10^{-8}, as rtol=10−8\text{rtol} = 10^{-8} roughly asks.

Try it

The same equation with three settings. Each line prints the answer and its relative error, both rounded.

The last line prints e−30e^{-30} and the time ln⁡10610\frac{\ln 10^6}{10} at which e−10te^{-10t} falls to 10−610^{-6}.

⌘/Ctrl + Enter runs the cell

Common mistakes

  1. Use the standard form first. y′=3y+1y' = 3y + 1 is y′−3y=1y' - 3y = 1, so p=−3p = -3; 2y′+4y=62y' + 4y = 6 must be divided by 2.
  2. The factor is e∫p dte^{\int p\,dt}, not epe^{p} or ∫p dt\int p\,dt. For p=2tp = 2t it is et2e^{t^2}, not e2te^{2t}.
  3. Multiply the forcing too. The right side becomes μq\mu q, not qq.
  4. Add CC before dividing by μ\mu. y′+y=ty' + y = t gives t−1+Ce−tt - 1 + Ce^{-t}, not t−1+Ct - 1 + C.
  5. The steady part is not the whole answer. The decaying part carries the start, and when p<0p < 0 it grows instead.
  6. solve_ivp wants fun(t, y) in that order, y0 as a list, and returns yy as sol.y[0].
  7. The default tolerances give about three digits, and none at all once ∣y∣|y| falls below atol. Set rtol, and atol well below the smallest size of yy you care about.

Linear equations and integrating factors

0 of 12 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
  9. 9.
  10. 10.
  11. 11.
  12. 12.
How to type answers
  • Powers with ^: x^7, 2^(1/3), a^-2.
  • Multiply with * or by writing side by side: 3x^2, 2(x+1).
  • ln(x) is the natural log, log(x, 2) is log base 2, e is Euler's number, sqrt(x) is a square root.
  • sin(x), cos(x) and tan(x) take radians; write pi for π and sin(x)^2 for sin²x.
  • Your first answer to each question is saved: it counts towards your score and brings the question back for review.
  • Exact answers like ln(20)/ln(3) are best. Decimals need about 4 significant figures.

Summary card

  • A linear first-order equation in standard form is y′+p(t) y=q(t)y' + p(t)\,y = q(t); squares or products of yy are not linear. With q=0q = 0 it is homogeneous and separable.
  • The integrating factor μ=e∫p dt\mu = e^{\int p\,dt} makes the left side (μy)′(\mu y)'; then y=1μ(∫μq dt+C)y = \frac{1}{\mu}\left(\int \mu q\,dt + C\right).
  • Constant pp and qq: y=qp+(y0−qp)e−pty = \frac{q}{p} + \left(y_0 - \frac{q}{p}\right)e^{-pt}.
  • Every solution is a particular solution plus Ce−∫p dtCe^{-\int p\,dt}. With p>0p > 0 the second part decays and the start is forgotten; with p<0p < 0 it grows. A pp like 1t\frac{1}{t} limits the solution to where pp is defined.
  • solve_ivp(fun, t_span, [y0], rtol=…, atol=…) adapts its steps to keep each error below about atol+rtol ∣y∣\text{atol} + \text{rtol}\,|y|. The defaults, 10−310^{-3} and 10−610^{-6}, give about three digits; set atol below the size of the solution.

Free further reading

Optional. This lesson covers what you need; these add more examples and practice. All are free to read.