Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 68 ยท Differential Equations

Ordinary Differential Equations: First Steps

Start with ODEs: model change with equations, read direction fields, solve separable equations by hand, handle initial values and check answers with sympy dsolve.

Most laws of nature do not tell you where something is; they tell you how fast it is changing. A warm cup cools at a rate proportional to how much hotter it is than the room. A bank balance grows at a rate proportional to the balance. A falling object accelerates according to the forces on it. Each of these is a statement about a derivative, and an equation that relates an unknown function to its derivatives is a differential equation.

It is called an ordinary differential equation (ODE) when the unknown depends on a single variable, usually time. (With several variables you get partial differential equations, covered later.) This lesson shows how to read an ODE, picture its solutions, solve the simplest family by hand, and let sympy confirm your work. You will need derivatives and integration techniques.

What an ODE says

Take the equation dy/dx = x - y. It does not give y; it gives the slope of the graph of y at every point (x, y) in the plane. A solution is a function y(x) whose slope matches that rule everywhere. Typically there is a whole family of solutions, one for each constant of integration, just as an antiderivative is only defined up to + C.

Some vocabulary:

  • The order is the highest derivative that appears. y' = x - y is first order; y'' + 4y = 0 is second order.
  • An ODE is linear if the unknown and its derivatives appear only to the first power and are not multiplied together. y' = x - y is linear; y' = y**2 is not.
  • The general solution contains arbitrary constants (as many as the order). An initial value problem (IVP) adds conditions such as y(0) = 2, which pick a single member of the family.
flowchart LR
    M["Model: rate = f(t, y)"] --> E["ODE"]
    E --> G["General solution with C"]
    I["Initial condition y(t0) = y0"] --> P["Particular solution"]
    G --> P

The ODE gives a family of curves; the initial condition selects one of them.

Direction fields

You can understand an ODE's solutions without solving it. At each point of a grid, draw a short line segment with the slope that the equation prescribes. The result is a direction field (or slope field). A solution curve is any curve that is tangent to the segments everywhere, like a leaf floating on a stream.

For y' = x - y the slope is 0 along the line y = x, positive below it and negative above it. In the plot below, the thin segments are the field and the curves are members of the exact family y = x - 1 + C*exp(-x), which you will learn to find in the next lesson. Notice how all curves are pulled toward the line y = x - 1.

Python Direction field with solution 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.

Direction fields are also the quickest way to see equilibrium solutions, constant solutions where the slope is zero everywhere on the line, and to judge whether neighbouring solutions move toward them (stable) or away (unstable).

Separable equations

The easiest equations to solve by hand are separable: you can write them as dy/dx = g(x) * h(y). Then move all y terms to one side and all x terms to the other, and integrate both sides:

dy / h(y) = g(x) dx gives integral of 1/h(y) dy = integral of g(x) dx + C.

Example. Solve dy/dx = x*y with y(0) = 2.

  1. Separate: dy / y = x dx (assuming y is not zero).
  2. Integrate: ln|y| = x**2/2 + C.
  3. Exponentiate: y = A*exp(x**2/2), where A = ยฑe**C absorbs the sign and constant.
  4. Apply y(0) = 2: 2 = A*e**0, so A = 2.

The solution is y = 2*exp(x**2/2). The step "divide by y" quietly dropped the constant solution y = 0, which is also valid (it is the case A = 0). Remember to check what you divided by.

Two classic models are separable:

  • Exponential growth or decay: dy/dt = k*y gives y = y0*exp(k*t). Positive k is growth; negative k is decay, as in radioactive material.
  • Newton's law of cooling (next lesson): dT/dt = -k*(T - T_room).

Solving with sympy

sympy.dsolve solves many ODEs symbolically. You declare the unknown as a function with sp.Function, write the equation with sp.Eq, and pass initial conditions through ics. Always verify the answer by substituting back, which sp.checkodesol does for you.

Python dsolve, initial values and verification

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 first two lines show C1*exp(x**2/2) and then the member of that family with C1 = 2, matching the hand solution. checkodesol reports True together with 0, meaning that substituting the answer leaves a residual of zero.

Solutions can blow up

The third example, y' = y**2 with y(0) = 1, has the solution y = 1/(1 - x). It looks harmless until x approaches 1, where it shoots to infinity. This is finite-time blow-up, and it cannot happen in a linear equation. It also reminds you that a solution is only valid on an interval: the formula 1/(1 - x) exists for x > 1 too, but the actual solution that starts at x = 0 is trapped on x < 1.

Whether a solution exists and is unique is guaranteed by the Picard-Lindelof theorem: if f(x, y) and its partial derivative with respect to y are continuous near the starting point, the IVP y' = f(x, y), y(x0) = y0 has exactly one solution nearby. This is why solution curves in a direction field never cross.

Modelling: from words to an ODE

Setting up the equation is often the hardest step, so practise reading rates.

  • "The population grows 3% per year" means dP/dt = 0.03*P.
  • "A tank loses water at a rate proportional to the square root of the depth" means dh/dt = -k*sqrt(h). This is separable too.
  • "A skydiver is pulled by gravity and resisted by drag proportional to speed" means m*dv/dt = m*g - c*v.

The last one is a linear equation with a constant forcing term. Its solution levels off at the terminal velocity m*g/c, the equilibrium where the slope is zero. You can read that off the equation without solving it.

Python Skydiver: terminal velocity

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 curve rises steeply at first and flattens as it approaches the dashed line at 65.4 m/s.

Not every ODE has a closed-form solution. When dsolve returns an unevaluated result or an error, numerical methods are the way forward; see Solving ODEs Numerically.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the order of the equation y'' + 3y' + 2y = 0?

    The order is the highest derivative present, here y'' so the order is 2.
  2. 2. Which equation is separable?

    Only dy/dx = sin(x) * y**2 factors as g(x) * h(y), so the variables can be moved to opposite sides.
  3. 3. The general solution of dy/dt = 5y is:

    Separating gives dy/y = 5 dt, so ln|y| = 5t + C and y = A*exp(5t).
  4. 4. What does an initial condition such as y(0) = 2 do?

    It fixes the arbitrary constant. Solutions can still blow up in finite time, as y' = y**2 shows.
  5. 5. In a direction field, where are the equilibrium solutions of y' = f(y)?

    A constant solution has zero derivative, so it sits at a value of y where f(y) = 0.

Summary

  • An ODE relates an unknown function to its derivatives; the order is the highest derivative.
  • The general solution has arbitrary constants; initial conditions turn it into one particular solution.
  • A direction field draws the slope prescribed at each point, and solution curves follow it.
  • Separable equations y' = g(x) * h(y) are solved by separating variables and integrating both sides.
  • sympy.dsolve solves many ODEs symbolically, and checkodesol verifies the result.
  • Solutions are valid on an interval and can blow up in finite time when the equation is nonlinear.

Next: First-Order ODEs in Depth.