Mathematics RU

Solver

Differential equation solver: the general solution and initial value problems

First-order equations (separable, linear, Bernoulli, homogeneous) and second-order linear equations with constant coefficients. The solver recognises the type, solves it step by step, finds the constants from the initial conditions and checks the answer by substitution.

Loading the solver…

What you can type

Derivatives are written with primes, y', y'', or as fractions, dy/dx, d^2y/dx^2. You don’t have to put the equation in standard form: the solver rewrites x y' + y = x^2 itself. Initial conditions follow after commas: y'' + 4y = 8x^2, y(0) = 0, y'(0) = 1 is an initial value problem (also called a Cauchy problem), and the answer is one particular solution. If the function is called $x(t)$, as in physics, write it that way: x'' + 9x = 0.

The solver takes first-order equations of five types: $y' = f(x)$, separable, linear, Bernoulli and homogeneous. Of second-order equations, it takes linear ones with constant coefficients, $a y'' + b y' + c y = f(x)$, with any right-hand side whose integrals can be found. Equations with parameters ($y' = ay$) need numbers put in.

How to recognise the type and what to do with it

Separable: $y' = f(x)\,g(y)$. Write $y' = \frac{dy}{dx}$, collect everything with $y$ on the left and everything with $x$ on the right, $\frac{dy}{g(y)} = f(x)\,dx$, and integrate both sides. Remember: dividing by $g(y)$ is allowed only where $g(y) \ne 0$; the constant functions $y = c$ with $g(c) = 0$ are solutions too, and they’re easy to lose.

Linear first order: $y' + p(x)\,y = q(x)$. First solve the homogeneous equation $y' + py = 0$; it’s separable, $y = C e^{-\int p\,dx}$. Then “bring the constant to life”: look for $y = C(x)\,e^{-\int p\,dx}$. After substituting, the terms with $C(x)$ cancel, leaving $C'(x)\,e^{-\int p} = q$, and $C(x)$ comes from a single integral. This is Lagrange’s method of variation of parameters. (English textbooks more often use an integrating factor, $e^{\int p\,dx}$, which gives the same result.)

A Bernoulli equation ($y' + py = q y^n$) becomes linear after the substitution $z = y^{1 - n}$, and a homogeneous one ($y' = F(y/x)$) becomes separable after the substitution $y = ux$.

Second order with constant coefficients: $a y'' + b y' + c y = f(x)$. The general solution is made of two parts: $y = y_c + y_p$, where $y_c$ (the complementary function) is the general solution of the homogeneous equation, with zero on the right, and $y_p$ (a particular integral) is any one solution of the full equation. The difference of two solutions of the full equation solves the homogeneous one, so knowing one solution $y_p$ gives you all of them.

Why the characteristic equation works

The exponential is a function that differentiation only multiplies by a number: $(e^{\lambda x})' = \lambda e^{\lambda x}$, $(e^{\lambda x})'' = \lambda^2 e^{\lambda x}$. Substitute $y = e^{\lambda x}$ into the homogeneous equation:

$$a\lambda^2 e^{\lambda x} + b\lambda e^{\lambda x} + c\,e^{\lambda x} = (a\lambda^2 + b\lambda + c)\,e^{\lambda x} = 0.$$

The exponential never vanishes, so solutions arise exactly for the $\lambda$ that make the bracket zero. The differential equation has turned into a quadratic.

The exponent of the exponential we’re looking for: each derivative turns into a factor $\lambda$. The discriminant decides what the answer looks like. When $D > 0$ there are two roots and two exponentials: $y_c = C_1 e^{\lambda_1 x} + C_2 e^{\lambda_2 x}$. When $D = 0$ there’s one root, and the second solution is the same exponential times $x$: $y_c = (C_1 + C_2 x)e^{\lambda x}$. When $D < 0$ the roots are $\alpha \pm \beta i$, and by Euler’s formula $y_c = e^{\alpha x}(C_1 \cos \beta x + C_2 \sin \beta x)$: oscillations. Example: $y'' - 3y' + 2y = 0$ gives $\lambda^2 - 3\lambda + 2 = 0$, with roots $1$ and $2$, and the solution $y = C_1 e^{x} + C_2 e^{2x}$. Check one exponential: for $y = e^{2x}$ we have $4e^{2x} - 3 \cdot 2e^{2x} + 2e^{2x} = 0$.

A particular solution for a right-hand side of the form “polynomial $\times\ e^{\alpha x} \times$ cosine or sine of $\beta x$” is looked for in the same form with unknown coefficients: $y_p = e^{\alpha x}\bigl(P(x)\cos\beta x + Q(x)\sin\beta x\bigr)$, where $P$ and $Q$ are polynomials of the same degree with letters instead of numbers. If the “control number” $\alpha + \beta i$ turns out to be a root of the characteristic equation, the guess is multiplied by $x$ (by $x^2$ for a double root): otherwise it solves the homogeneous equation itself and gives zero. This is resonance, the very thing that makes a swing go higher when you push it in time.

Worked examples

Example 1. Separating the variables: $y' = 2xy$

$\frac{dy}{y} = 2x\,dx$; integrate: $\ln|y| = x^2 + C$. Exponentiate: $|y| = e^{C}e^{x^2}$, and absorb the sign and $e^{C}$ into a new constant: $y = Ce^{x^2}$. The solution $y = 0$, lost when dividing by $y$, is included here with $C = 0$.

Example 2. A linear equation: $y' + y = x$

The homogeneous equation $y' + y = 0$ gives $y = Ce^{-x}$. Look for $y = C(x)e^{-x}$: substituting leaves $C'(x)e^{-x} = x$, that is, $C'(x) = xe^{x}$. By parts, $C(x) = e^{x}(x - 1) + C$, and

$$y = e^{-x}\bigl(e^{x}(x - 1) + C\bigr) = x - 1 + Ce^{-x}.$$

Check: $y' = 1 - Ce^{-x}$, and $y' + y = 1 - Ce^{-x} + x - 1 + Ce^{-x} = x$.

Example 3. An initial value problem: $y'' + 4y = 8x^2$, $y(0) = 0$, $y'(0) = 1$

The characteristic equation is $\lambda^2 + 4 = 0$, with roots $\pm 2i$, so $y_c = C_1 \cos 2x + C_2 \sin 2x$. The right-hand side is a polynomial of degree two, and zero isn’t a root, so we look for $y_p = Ax^2 + Bx + C$. Substitute: $2A + 4(Ax^2 + Bx + C) = 8x^2$. Equate coefficients: $4A = 8$, $4B = 0$, $2A + 4C = 0$, so $A = 2$, $B = 0$, $C = -1$.

The general solution is $y = C_1 \cos 2x + C_2 \sin 2x + 2x^2 - 1$. From $y(0) = 0$ we get $C_1 - 1 = 0$, and from $y'(0) = 1$, $2C_2 = 1$. Answer:

$$y = \cos 2x + \frac12 \sin 2x + 2x^2 - 1.$$

Example 4. Resonance: $y'' + 9y = \cos 3x$

The roots are $\pm 3i$, and the right-hand side is $\cos 3x$: the control number $3i$ coincides with a root. The guess $A\cos 3x + B\sin 3x$ solves the homogeneous equation and gives zero on the left, so we take $y_p = x(A\cos 3x + B\sin 3x)$. Substituting gives $-6A\sin 3x + 6B\cos 3x = \cos 3x$, so $A = 0$, $B = \frac16$:

$$y = C_1\cos 3x + C_2 \sin 3x + \frac{x}{6}\sin 3x.$$

The amplitude of the particular solution grows with $x$: a system pushed at its natural frequency swings higher and higher without limit.

Common mistakes

  • The constant in the wrong place. $\ln|y| = x^2 + C$ gives $y = Ce^{x^2}$, not $y = e^{x^2} + C$: the constant sits in the exponent, and after exponentiating it becomes a factor.
  • Lost solutions. In $y' = y^2$ people divide by $y^2$ and get $y = -\frac{1}{x + C}$. But $y = 0$ is a solution too, and the formula doesn’t give it for any $C$.
  • A repeated root without the $x$. When $\lambda_1 = \lambda_2 = \lambda$, writing $C_1 e^{\lambda x} + C_2 e^{\lambda x}$ gives a single function with constant $C_1 + C_2$. You need $(C_1 + C_2 x)e^{\lambda x}$.
  • Complex exponentials in the answer. With roots $\alpha \pm \beta i$ the answer is written with $e^{\alpha x}\cos\beta x$ and $e^{\alpha x}\sin\beta x$: real functions giving the same set of solutions.
  • Forgetting resonance. If the control number is a root of the characteristic equation, there’s no point substituting the guess without the factor $x$: you’ll get $0 = f(x)$.
  • Applying the initial conditions to $y_c$ alone. The constants $C_1$, $C_2$ are found from the general solution, with $y_p$ already included; otherwise the solution won’t satisfy the conditions.

See also

Direction fields, Euler’s method, logistic growth, a vibrating spring and epidemic models are in the chapter on differential equations. Why the exponential is the only function equal to its own derivative is told in the chapter on the number e, and Euler’s formula $e^{i\varphi} = \cos\varphi + i\sin\varphi$, which turns complex roots into oscillations, in its section on the circle and in the chapter on complex numbers. The integrals you meet along the way can be worked through separately in the integral solver, and the characteristic equation in the quadratic equation solver.

Where this is explained

Other solvers