Topic

First-order equations: the methods

The validated solver answers "what is y(2)?" with a proof and no formula. A course asks for the formula and for the method that finds it. firstOrder recognises the standard types of equation y′=f(x,y)y' = f(x, y), carries out the method, and reports every step. exactEquation does the same for M dx+N dy=0M\,dx + N\,dy = 0.

const s = firstOrder("x - 2*y");          // y' = x - 2y
s.type;       // "linear"
s.general;    // "y = 1/2*x - 1/4 + C*exp(-2*x)"
s.steps;      // [{ what, formula }, …]

const p = s.through(0, 1);                // the solution with y(0) = 1
p.solution;   // "1/2*x - 1/4 + 5/4*exp(-2*x)"
p.check(1);   // { agree: true, numeric: […], formula: […] }

A formula that has been checked

Every closed form here can be compared with the validated numerical solution. check(x1) solves the initial value problem to x1x_1 with proven error bounds and evaluates the formula there with proven error bounds; the two enclosures of y(x1)y(x_1) must meet. For a solution given only as a relation G(x,y)=CG(x, y) = C, the relation is evaluated on the numerical enclosure and must contain the constant. A wrong formula fails this test, so a mistake in the algebra cannot pass unnoticed. A check takes from a fraction of a second to some seconds, depending on the equation.

The types

typeformmethod
lineary′=a(x)+b(x) yy' = a(x) + b(x)\,ymultiply by μ=e−∫b dx\mu = e^{-\int b\,dx}; then (μy)′=μa(\mu y)' = \mu a
separabley′=g(x) h(y)y' = g(x)\,h(y)∫dy/h(y)=∫g(x) dx+C\int dy/h(y) = \int g(x)\,dx + C
Bernoulliy′=p(x) y+q(x) yny' = p(x)\,y + q(x)\,y^nv=y1−nv = y^{1-n} makes it linear
homogeneousy′=F(y/x)y' = F(y/x)y=vxy = vx makes it separable: xv′=F(v)−vx v' = F(v) - v
exactM dx+N dy=0M\,dx + N\,dy = 0 with My=NxM_y = N_xfind φ\varphi with φx=M\varphi_x = M, φy=N\varphi_y = N; solutions are φ=C\varphi = C

An equation can be of several types: y′=xyy' = xy is linear and separable. types lists all that apply and type is the one used; pass { method } to choose. A linear equation is solved as linear because that gives yy outright. A separable equation gives a relation between xx and yy, reported as implicit, and where the relation is easily solved (a logarithm, a reciprocal, an arctangent) also as general.

Dividing by h(y)h(y) loses the constant solutions y=y0y = y_0 with h(y0)=0h(y_0) = 0. They are listed in singular.

Why the integrating factor works

For y′−b y=ay' - b\,y = a, multiply by μ(x)\mu(x). The left side becomes μy′−μb y\mu y' - \mu b\,y, which is the derivative of the product μy\mu y exactly when μ′=−μb\mu' = -\mu b, that is μ=e−∫b\mu = e^{-\int b}. Then (μy)′=μa(\mu y)' = \mu a is solved by one integration:

y=1μ(∫μ a dx+C). y = \frac{1}{\mu}\Bigl(\int \mu\,a\,dx + C\Bigr).

Exact equations

If φ(x,y)\varphi(x, y) is constant along a solution then φx dx+φy dy=0\varphi_x\,dx + \varphi_y\,dy = 0. So M dx+N dy=0M\,dx + N\,dy = 0 is solved at once if M=φxM = \varphi_x and N=φyN = \varphi_y for some φ\varphi, and since mixed partials agree that requires My=NxM_y = N_x. When the test fails, a factor μ\mu may repair it: if (My−Nx)/N(M_y - N_x)/N depends on xx alone then μ(x)=e∫(My−Nx)/N dx\mu(x) = e^{\int (M_y - N_x)/N\,dx} works, and likewise with the roles exchanged. exactEquation tries both.

const e = exactEquation("3*x*y + y^2", "x^2 + x*y");
e.exact;       // false
e.factor;      // "x"
e.potential;   // "y*x^3 + 1/2*y^2*x^2"     solutions: that = C

The integrals

The methods need antiderivatives. antiderivative(tree, name) covers polynomials, rational functions by partial fractions, exponentials and sines of linear arguments with polynomial factors, and one level of substitution; it returns nothing unless differentiating its answer gives the integrand back. When an integral is not found, type is null and the validated solver is still there. The fuller engine is in the symbolic package.

callreturns
firstOrder(rhs, { x = "x", y = "y", method }){ equation, type, types, steps, general, implicit, singular, through }
exactEquation(M, N, { x, y })the same, with { exact, factor, potential }
s.through(x0, y0){ constant, solution, relation, at(x), check(x1) }
antiderivative(tree, name){ tree, trust: "proven" | "checked" } or null
Try itEdit the code and run it with @ratmath/analytic and Core's R, Rational, RationalInterval in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs