Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 68 ยท Differential Equations

First-Order ODEs in Depth

Solve linear first-order ODEs with an integrating factor, test for exact equations, and model growth, decay, mixing tanks and Newton cooling with sympy checks.

Separable equations cover a lot of ground, but many first-order equations are not separable. The two most important extra families are linear equations, solved with an integrating factor, and exact equations, solved by finding a potential function. Between them with separable equations, you can handle the first-order models that appear in most engineering courses: growth and decay, mixing problems, cooling, simple circuits and falling bodies with drag.

This lesson develops both methods, then applies them to realistic models. Each solution is checked with sympy, and the plots let you see the behaviour that the formulas hide.

Linear equations and the integrating factor

A first-order linear ODE can always be written in standard form

y' + p(x)*y = q(x)

The trick is to multiply by a function mu(x) chosen so that the left side becomes the derivative of a product. By the product rule, (mu*y)' = mu*y' + mu'*y. That matches mu*(y' + p*y) exactly when mu' = p*mu, which is a separable equation whose solution is

mu(x) = exp(integral of p(x) dx).

This mu is the integrating factor. The recipe:

  1. Put the equation in standard form with coefficient 1 on y'.
  2. Compute mu = exp(integral of p dx) (no constant needed).
  3. Multiply through: (mu*y)' = mu*q.
  4. Integrate: mu*y = integral of mu*q dx + C, then divide by mu.

Example. y' + (2/x)*y = x for x > 0. Here p = 2/x, so mu = exp(2*ln x) = x**2. Multiplying gives (x**2 * y)' = x**3, so x**2 * y = x**4/4 + C and

y = x**2/4 + C/x**2.

flowchart LR
    A["y' + p(x) y = q(x)"] --> B["mu = exp(integral of p)"]
    B --> C["(mu y)' = mu q"]
    C --> D["integrate and divide by mu"]

The integrating factor turns a linear ODE into a plain integration problem.

The solution always has two parts: a homogeneous part (the terms with C, the solution of y' + p*y = 0) that carries the initial condition and often fades away, and a particular part that responds to the forcing q(x).

Python Integrating factor step by step

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The hand-built answer and dsolve should agree up to the name of the constant. With y(1) = 3 the constant is pinned down: 1/4 + C = 3, so C = 11/4.

Exact equations

Sometimes a first-order equation is written as a differential form

M(x, y) dx + N(x, y) dy = 0.

It is exact if it is the total differential of some function F(x, y), meaning F_x = M and F_y = N. Then solutions are the level curves F(x, y) = C, just like contour lines of a surface (recall gradients). Because mixed partial derivatives are equal (F_xy = F_yx), there is a quick test:

M_y = N_x (the equation is exact).

To find F: integrate M with respect to x, giving F = integral of M dx + g(y), then differentiate with respect to y and match it with N to determine g(y).

Example. (2xy + 3) dx + (x**2 + 4y) dy = 0. Here M_y = 2x and N_x = 2x, so it is exact. Integrating M: F = x**2*y + 3x + g(y). Then F_y = x**2 + g'(y) must equal x**2 + 4y, so g = 2y**2 and the solutions are x**2*y + 3x + 2y**2 = C.

If the test fails, the equation may still be made exact by multiplying by a suitable integrating factor, but there is no universal recipe; separable and linear forms are usually tried first.

Python Exact equation and its level curves

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Growth and decay

The simplest linear ODE is y' = k*y, whose solution is y = y0*exp(k*t). For decay, a useful number is the half-life t_half = ln(2)/|k|, the time for the quantity to halve. A constant is also added by many real models: a savings account with deposits, a drug infused at a steady rate, a capacitor charging. They all share the form y' = -a*y + b, which settles at the equilibrium y = b/a and approaches it exponentially, with time constant 1/a. After one time constant the gap to equilibrium has shrunk to about 37% (1/e) of its starting size; after five it is below 1%.

Mixing problems

A tank holds 100 litres of water with 2 kg of dissolved salt. Brine containing 0.5 kg of salt per litre flows in at 4 litres per minute, and the well-mixed solution flows out at the same rate. Let S(t) be the salt mass in kg. The balance law "rate of change = rate in - rate out" gives

  • rate in: 0.5 * 4 = 2 kg/min,
  • rate out: (S/100) * 4 = 0.04*S kg/min,

so S' = 2 - 0.04*S, S(0) = 2. The equilibrium is S = 50 kg (the inflow concentration times the volume) and the time constant is 1/0.04 = 25 minutes.

Newton's law of cooling

Newton's law says an object's temperature changes at a rate proportional to the difference from its surroundings: T' = -k*(T - T_env). The solution is T(t) = T_env + (T0 - T_env)*exp(-k*t). One measurement lets you find k, after which you can predict any time. This is how a cooling coffee cup, a hot component on a circuit board or the cooling of a cast part is first estimated.

Python Mixing tank and cooling coffee

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Units matter in every model above. If the tank rate is in litres per minute and you read the time constant in seconds, the answer is off by a factor of 60. Check that each term of the equation has the same unit before solving.

Choosing a method

If the equation is... Try
y' = g(x)*h(y) separation of variables
y' + p(x)*y = q(x) integrating factor
M dx + N dy = 0 with M_y = N_x exact: find the potential F
none of the above substitution, dsolve, or numerical methods

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. For y' + 3y = e**(2x), what is the integrating factor?

    mu = exp(integral of p dx) with p = 3, so mu = exp(3x).
  2. 2. Which condition makes M dx + N dy = 0 exact?

    The mixed partial derivatives of a potential F are equal, so M_y = F_xy = F_yx = N_x.
  3. 3. A tank has constant inflow concentration 0.5 kg/L and constant volume. What is the long-term salt concentration inside?

    At equilibrium the outflow concentration equals the inflow concentration, so S/V = 0.5 kg/L.
  4. 4. A cup of coffee follows T' = -k(T - 20). What happens to the rate of cooling as T approaches 20?

    The rate is proportional to T - 20, which goes to zero. The cup approaches room temperature ever more slowly.
  5. 5. What is the time constant of y' = -a*y + b with a > 0?

    The deviation from equilibrium decays like exp(-a*t), so it falls to 1/e of its size after a time 1/a.

Summary

  • A linear equation y' + p*y = q is solved by multiplying by mu = exp(integral of p), which makes the left side (mu*y)'.
  • M dx + N dy = 0 is exact when M_y = N_x; its solutions are level curves of a potential F.
  • y' = -a*y + b settles at b/a with time constant 1/a.
  • Mixing problems come from "rate in minus rate out"; cooling follows T' = -k*(T - T_env).
  • Verify every symbolic solution with sympy.checkodesol and keep units consistent.

Next: Second-Order Linear ODEs.