differential equations25 min

First-Order ODEs

Ordinary differential equations that relate a function to its first derivative, and how to solve them numerically with Euler method

0/9Not Started

Why This Matters

An ordinary differential equation (ODE) describes how a quantity changes over time. When a physicist models a falling object, when a biologist tracks population growth, or when an engineer simulates a circuit, they are all writing ODEs. A first-order ODE involves only the first derivative -- it says "the rate of change of y depends on x and y."

The key question is: given a rule for how something changes, can you figure out what actually happens? That is the initial value problem: you know the starting point and the rate of change at every instant, and you want to trace the entire trajectory. Even when you cannot find a neat formula, you can always approximate the answer numerically using Euler method -- stepping forward in tiny increments, using the derivative to predict the next value.

Define Terms

Visual Model

ODE: dy/dx = f(x, y)Rate of change rule
Initial Conditiony(x0) = y0
Initial Value ProblemODE + starting point
Exact SolutionAnalytical formula
Euler MethodNumerical stepping
Solution Curve y(x)The trajectory

The full process at a glance. Click Start tour to walk through each step.

An initial value problem combines an ODE with a starting condition. You solve it exactly or numerically to trace the solution curve.

Code Example

Code
// First-order ODE: dy/dx = 2x, y(0) = 1
// Exact solution: y = x^2 + 1

// Euler method: step forward using slope
function eulerMethod(f, x0, y0, xEnd, stepSize) {
  let x = x0;
  let y = y0;
  const points = [{ x, y }];

  while (x < xEnd) {
    const slope = f(x, y);
    y = y + stepSize * slope;
    x = x + stepSize;
    points.push({ x: Math.round(x * 1000) / 1000, y: Math.round(y * 1000) / 1000 });
  }
  return points;
}

// dy/dx = 2x
const f = (x, y) => 2 * x;

// Solve from x=0 to x=3, starting at y=1, step size 0.5
const result = eulerMethod(f, 0, 1, 3, 0.5);
console.log("Euler approximation:");
result.forEach(p => console.log(`  x=${p.x}, y=${p.y}`));

// Compare with exact solution y = x^2 + 1
console.log("\nExact values:");
for (let x = 0; x <= 3; x += 0.5) {
  console.log(`  x=${x}, y=${x * x + 1}`);
}

// Smaller step size = better accuracy
const fine = eulerMethod(f, 0, 1, 3, 0.1);
const lastFine = fine[fine.length - 1];
console.log(`\nStep 0.5 final: y=${result[result.length - 1].y}`);
console.log(`Step 0.1 final: y=${lastFine.y}`);
console.log(`Exact final:    y=${3 * 3 + 1}`);

Interactive Experiment

Try these exercises:

  • Change the ODE to dy/dx = -y (exponential decay). Start at y(0) = 10 and run Euler method. Does y approach zero?
  • Try step sizes of 1.0, 0.1, and 0.01 for dy/dx = 2x from x=0 to x=2. Compare the final y values with the exact answer y = x^2 + 1. How does accuracy change?
  • Modify the ODE to dy/dx = y (exponential growth). Start at y(0) = 1. What happens to y as x increases?
  • What happens if you use a negative step size? Does Euler method work backwards in time?
  • Plot the Euler approximation points alongside the exact curve. Where is the error largest?

Quick Quiz

Coding Challenge

Euler Method Solver

Write a function called `eulerSolve` that takes a slope function f(x, y), initial values x0 and y0, a final x value xEnd, and a number of steps n. It should return the approximate value of y at xEnd using Euler method. The step size is (xEnd - x0) / n. Formula: y_new = y_old + stepSize * f(x, y). Test it on dy/dx = x + y with y(0) = 1, solving to x = 1 with 1000 steps.

Loading editor...

Real-World Usage

First-order ODEs appear everywhere in science and engineering:

  • Physics: Newton second law for velocity: dv/dt = F/m. Given a force, you solve for how velocity changes over time.
  • Biology: Population models like dP/dt = rP describe exponential growth. Adding limiting terms gives logistic growth.
  • Chemistry: Reaction rates are modeled as first-order ODEs. Radioactive decay follows dN/dt = -lambda * N.
  • Finance: Compound interest with continuous compounding follows dA/dt = rA, giving exponential growth of investments.
  • Computer science: Euler method and related numerical solvers are the backbone of physics engines in games and simulations.

Connections