Linear equations
A first-order equation is linear when and appear only on their own, each multiplied by a function of alone. Every such equation can be written with first, with coefficient 1:
Rule: Linear first-order equation
This is the standard form. multiplies , and , the forcing, holds every term without . Either may be a constant.
Bring an equation to standard form before reading off and :
- from lesson 6.1.1 becomes , so and .
- becomes . Moving to the left flips its sign: .
- becomes after dividing by 2.
Squares and products of break linearity. , the logistic , and are not linear.
When , the equation is homogeneous. It is separable as well (lesson 6.1.1): gives . When it usually does not separate, and needs a new idea.
The integrating factor
The product rule (lesson 2.3.2) gives for any function , written with the Greek letter mu. Multiply the standard form by :
The left side is exactly when . That is a separable equation for , solved by , since (lesson 2.3.4). This is the integrating factor.
Rule: Integrating factor
Multiply by , integrate both sides, then divide by . Any antiderivative of will do: another constant only rescales , and the rescaling cancels. The is divided by too.
A constant
Solve with .
- 1
- 2
- 3
- 4
| 0 | 0.5 | 1 | 2 | 3 | |
|---|---|---|---|---|---|
| 0 | 1.896362 | 2.593994 | 2.945053 | 2.992564 |
The equation is also , the equilibrium case of lesson 6.1.1 with and , and separation gives the same answer. Whenever and are constants, the solution is .
A forcing that changes:
With , the integrating factor is , so . Integration by parts (lesson 2.6.2) gives .
The start gives , so and . This equation does not separate: is a difference, not a product.
| 0 | 1 | 2 | 4 | 6 | |
|---|---|---|---|---|---|
| 1 | 0.735759 | 1.270671 | 3.036631 | 5.004958 | |
| 0 | 1 | 3 | 5 | ||
| 2 | 0.735759 | 0.270671 | 0.036631 | 0.004958 |
Try it
linear_solve follows the rule: it builds , integrates , adds and divides by . Each line prints the general solution, the that matches the start, and the residual .
The four rows are this lesson's examples. The third, , is from lesson 6.1.1.
A steady part plus a decaying part
Every solution of is plus . The first part is the same for every start. The second carries the start, and it dies away: by it is 0.004958 of the 5.004958 in the table.
Rule: Two parts of a solution
is any one solution, called a particular solution; here the steady part . The second part is the general solution of the homogeneous equation . Where it decays, and the start is forgotten.
With a constant , the start is forgotten at the rate . Every units of time, the gap to the steady part shrinks by a factor of . For the steady part is the equilibrium 3; for it is a moving line.
With , the part decays and every solution approaches . With , the part grows, and only the start stays on the line .
When the second part grows
A negative turns decay into growth. is , with and . Parts gives , so .
From , and : 3.436564 at , 11.778112 at and 104.1963 at . Only , the start , stays on the line. Like the equilibrium 0 of , 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 that changes with
Solve with . Now , so and .
The substitution , (lesson 2.6.1) gives . So , then , and the start fixes .
| 0 | 0.5 | 1 | 1.5 | 2 | |
|---|---|---|---|---|---|
| 3 | 2.557602 | 1.735759 | 1.210798 | 1.036631 |
The steady part is 1. The decaying part dies faster than any exponential , because keeps growing. This equation is also , which separates; both methods give the same answer.
Where breaks down
Take with . Here has no value at , so work on . There (lesson 2.5.2), and .
So , then , and gives : . It is 1.25 at and 5.05 at , blowing up as approaches the break in . The decaying part fades only like , slower than an exponential.
Case study 6.1.4 meets a homogeneous equation with a changing , , as the price of a bond.
Try it
The tables for and for its sign-flipped twin , both from .
Then the changing : SymPy builds for when told , and the last lines print both solutions at a few times.
Solving numerically with solve_ivp
An integrating factor needs , which often has no formula, and or 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 , withtfirst.t_spanis the interval, such as(0, 6), andy0the list of starting values. The solver also handles several equations at once, so one value still goes in a list.- The answer
sol.yhas one row per unknown, sosol.y[0]holds at the timessol.t.t_evalasks 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
rtol is the relative accuracy asked for, and atol an absolute floor, which matters when is near 0. The defaults give about three digits. Set both whenever the digits matter.
For with , the exact value at is 5.004958. Tighter tolerances cost more evaluations of fun, but far fewer than Euler's method needs:
| method | evaluations of | error at |
|---|---|---|
solve_ivp, default tolerances | 44 | |
rtol=1e-6, atol=1e-9 | 116 | |
rtol=1e-10, atol=1e-12 | 620 | |
| Euler, | 60,000 |
With the tight tolerances, the values at = 1, 2, 4 and 6 match the table above to all six decimals.
Try it
f is the slope . The loop solves to 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.
When atol decides the answer
Take with , whose exact value at is . With the defaults, solve_ivp returns about , two million times too large.
Nothing failed: the solution fell below by , and from then on any value that small counts as accurate enough. Even leaves the answer off by about half, because is still below it. With , far below the solution, the relative error drops to about , as 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 and the time at which falls to .
Common mistakes
- Use the standard form first. is , so ; must be divided by 2.
- The factor is , not or . For it is , not .
- Multiply the forcing too. The right side becomes , not .
- Add before dividing by . gives , not .
- The steady part is not the whole answer. The decaying part carries the start, and when it grows instead.
solve_ivpwantsfun(t, y)in that order,y0as a list, and returns assol.y[0].- The default tolerances give about three digits, and none at all once falls below
atol. Setrtol, andatolwell below the smallest size of you care about.
Linear equations and integrating factors
0 of 12 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 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,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for 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 ; squares or products of are not linear. With it is homogeneous and separable.
- The integrating factor makes the left side ; then .
- Constant and : .
- Every solution is a particular solution plus . With the second part decays and the start is forgotten; with it grows. A like limits the solution to where is defined.
solve_ivp(fun, t_span, [y0], rtol=…, atol=…)adapts its steps to keep each error below about . The defaults, and , give about three digits; setatolbelow the size of the solution.