Course Test · University: first two years

Ordinary differential equations: RatMath answers

The same twenty exercises, answered with RatMath Core and the Shelf libraries only. The code below was run when this page was built; its output is shown as printed, and compared with the independent answer.

Agrees 20

Libraries used here: core, analytic, symbolic. In this code each library is in scope under its own name (linalg.det, stats.tTest), and Core's names (R, Rational, …) are in scope directly.

  1. 1.SeparableAgrees
    Solve y′=xy2y' = xy^2, y(0)=1y(0) = 1.
    Independent answer
    −1/y=x2/2+C-1/y = x^2/2 + C, C=−1C = -1: y=22−x2y = \dfrac{2}{2 - x^2}.
    RatMath code
    analytic.firstOrder("x*y^2").through(0, 1).solution
    Output
    -(2/(x^2 - 2))
  2. 2.Linear, integrating factorAgrees
    Solve y′+2y=e−xy' + 2y = e^{-x}, y(0)=0y(0) = 0.
    Independent answer
    (ye2x)′=ex(ye^{2x})' = e^x: y=e−x−e−2xy = e^{-x} - e^{-2x}.
    RatMath code
    analytic.firstOrder("exp(-x) - 2*y").through(0, 0).solution
    Output
    exp(-x) - exp(-2*x)
  3. 3.Exact equationAgrees
    Solve (2xy+3) dx+(x2−1) dy=0(2xy + 3)\,dx + (x^2 - 1)\,dy = 0.
    Independent answer
    My=2x=NxM_y = 2x = N_x. ψ=x2y+3x−y=C\psi = x^2y + 3x - y = C.
    RatMath code
    analytic.exactEquation("2*x*y + 3", "x^2 - 1").implicit
    Output
    y*x^2 + 3*x - y = C
  4. 4.BernoulliAgrees
    Solve y′=y2−yy' = y^2 - y.
    Independent answer
    v=1/yv = 1/y: v′−v=−1v' - v = -1, v=1+Cexv = 1 + Ce^x: y=11+Cexy = \dfrac{1}{1 + Ce^x} (and y=0y = 0).
    RatMath code
    analytic.firstOrder("y^2 - y").general
    Output
    y = 1/(C*exp(x) + 1)
  5. 5.HomogeneousAgrees
    Solve y′=x+yxy' = \dfrac{x + y}{x} for x>0x > 0.
    Independent answer
    y=vxy = vx: v+xv′=1+vv + xv' = 1 + v, v=ln⁡x+Cv = \ln x + C: y=xln⁡x+Cxy = x\ln x + Cx.
    RatMath code
    analytic.firstOrder("(x + y)/x").general
    Output
    y = log(abs(x))*x + C*x
    firstOrder recognises the equation as linear (and it is also homogeneous) but returns no solution.
    Fix: ratmath/analytic#18, exact starting values, pointwise limits, sec/csc/cot, integrating factors with 1/∣x∣1/|x|, ODESolution exported
  6. 6.Distinct real rootsAgrees
    Solve y′′−5y′+6y=0y'' - 5y' + 6y = 0.
    Independent answer
    r2−5r+6=0r^2 - 5r + 6 = 0, r=2,3r = 2, 3: y=C1e2x+C2e3xy = C_1e^{2x} + C_2e^{3x}.
    RatMath code
    analytic.linearODE([1, -5, 6]).general
    Output
    y = C1*exp(2*x) + C2*exp(3*x)
  7. 7.Repeated rootAgrees
    Solve y′′+4y′+4y=0y'' + 4y' + 4y = 0, y(0)=1y(0) = 1, y′(0)=0y'(0) = 0.
    Independent answer
    y=(C1+C2x)e−2xy = (C_1 + C_2x)e^{-2x}; C1=1C_1 = 1, C2−2=0C_2 - 2 = 0: y=(1+2x)e−2xy = (1 + 2x)e^{-2x}.
    RatMath code
    analytic.linearODE([1, 4, 4]).initial([1, 0]).solution
    Output
    y = 2*x*exp(-2*x) + exp(-2*x)
  8. 8.Complex rootsAgrees
    Solve y′′+2y′+5y=0y'' + 2y' + 5y = 0.
    Independent answer
    r=−1±2ir = -1 \pm 2i: y=e−x(C1cos⁡2x+C2sin⁡2x)y = e^{-x}(C_1\cos 2x + C_2\sin 2x).
    RatMath code
    analytic.linearODE([1, 2, 5]).general
    Output
    y = C1*cos(2*x)*exp(-x) + C2*sin(2*x)*exp(-x)
  9. 9.Undetermined coefficientsAgrees
    Find a particular solution of y′′−3y′+2y=4xy'' - 3y' + 2y = 4x.
    Independent answer
    yp=Ax+By_p = Ax + B: 2A=42A = 4, −3A+2B=0-3A + 2B = 0: yp=2x+3y_p = 2x + 3.
    RatMath code
    analytic.linearODE([1, -3, 2], "4*x").particular
    Output
    2*x + 3
  10. 10.ResonanceAgrees
    Find a particular solution of y′′+y=cos⁡xy'' + y = \cos x.
    Independent answer
    yp=x2sin⁡xy_p = \tfrac{x}{2}\sin x.
    RatMath code
    analytic.linearODE([1, 0, 1], "cos(x)").particular
    Output
    1/2*x*sin(x)
  11. 11.Variation of parametersAgrees
    Find a particular solution of y′′+y=sec⁡xy'' + y = \sec x.
    Independent answer
    yp=xsin⁡x+cos⁡xln⁡∣cos⁡x∣y_p = x\sin x + \cos x\ln|\cos x|.
    RatMath code
    analytic.linearODE([1, 0, 1], "sec(x)").particular
    Output
    log(abs(cos(x)))*cos(x) + x*sin(x)
    Bug: sec(x) was read as the product of a variable sec and xx, so the answer came back as sec*x. Written as 1/cos(x) it was solved correctly.
    Fix: ratmath/analytic#18, exact starting values, pointwise limits, sec/csc/cot, integrating factors with 1/∣x∣1/|x|, ODESolution exported
  12. 12.Laplace transformAgrees
    Find L{t2e3t}\mathcal L\{t^2e^{3t}\}.
    Independent answer
    2(s−3)3\dfrac{2}{(s - 3)^3}.
    RatMath code
    analytic.laplace("t^2*exp(3*t)")
    Output
    2/(s - 3)^3
  13. 13.Inverse Laplace transformAgrees
    Find L−1{s+1s2+4s+13}\mathcal L^{-1}\left\{\dfrac{s + 1}{s^2 + 4s + 13}\right\}.
    Independent answer
    (s+2)−1(s+2)2+9\dfrac{(s + 2) - 1}{(s + 2)^2 + 9}: e−2t(cos⁡3t−13sin⁡3t)e^{-2t}\left(\cos 3t - \tfrac13\sin 3t\right).
    RatMath code
    analytic.inverseLaplace("(s + 1)/(s^2 + 4*s + 13)").result
    Output
    cos(3*t)*exp(-2*t) + -1/3*sin(3*t)*exp(-2*t)
  14. 14.Step forcingAgrees
    Solve y′+y=u(t−1)y' + y = u(t - 1), y(0)=0y(0) = 0, where uu is the unit step.
    Independent answer
    Y=e−ss(s+1)Y = \dfrac{e^{-s}}{s(s + 1)}: y=u(t−1)(1−e−(t−1))y = u(t - 1)\left(1 - e^{-(t - 1)}\right).
    RatMath code
    analytic.laplaceSolve([1, 1], "heaviside(t - 1)", [0]).solution
    Output
    heaviside(t - 1)*(1 - exp(-t + 1))
  15. 15.Linear systemAgrees
    Solve x′=(1221)x\mathbf x' = \begin{pmatrix} 1 & 2 \\ 2 & 1 \end{pmatrix}\mathbf x and classify the origin.
    Independent answer
    Eigenvalues 3 with (1,1)(1, 1), −1-1 with (1,−1)(1, -1): x=c1e3t(1,1)+c2e−t(1,−1)\mathbf x = c_1e^{3t}(1, 1) + c_2e^{-t}(1, -1). A saddle, unstable.
    RatMath code
    const L = analytic.linearSystem([[1, 2], [2, 1]]);
    [L.eigenvalues, L.eigenvectors, L.kind, L.stability]
    Output
    [["-1", "3"], [["2", "-2"], ["2", "2"]], "saddle", "unstable"]
  16. 16.Classifying an equilibriumAgrees
    Classify the origin for x′=yx' = y, y′=−x−yy' = -x - y.
    Independent answer
    Trace −1-1, determinant 1, discriminant 1−4<01 - 4 < 0: a stable spiral.
    RatMath code
    const L = analytic.linearSystem([[0, 1], [-1, -1]]);
    [L.kind, L.stability]
    Output
    ["stable spiral", "asymptotically stable"]
  17. 17.Nonlinear phase planeAgrees
    Find and classify the equilibria of x′=x(3−x−y)x' = x(3 - x - y), y′=y(x−1)y' = y(x - 1) with x,y≥0x, y \ge 0.
    Independent answer
    (0,0)(0, 0) saddle; (3,0)(3, 0) saddle; (1,2)(1, 2): Jacobian (−1−120)\begin{pmatrix} -1 & -1 \\ 2 & 0 \end{pmatrix}, trace −1-1, det 2: stable spiral.
    RatMath code
    const mid = (iv) => Math.round(iv.low.add(iv.high).toNumber() / 2);
    analytic.planarSystem(["x*(3 - x - y)", "y*(x - 1)"]).equilibria({ x: ["-1", "4"], y: ["-1", "3"] }).map((e) => [e.point.map(mid), e.kind])
    Output
    [[[0, 0], "saddle"], [[1, 2], "stable spiral"], [[3, 0], "saddle"]]
  18. 18.Series solutionAgrees
    Find the first terms of the power series solutions of Airy's equation y′′−xy=0y'' - xy = 0 about 0.
    Independent answer
    an+2=an−1(n+2)(n+1)a_{n+2} = \dfrac{a_{n-1}}{(n + 2)(n + 1)}: 1+x36+x6180+⋯1 + \tfrac{x^3}{6} + \tfrac{x^6}{180} + \cdots and x+x412+x7504+⋯x + \tfrac{x^4}{12} + \tfrac{x^7}{504} + \cdots.
    RatMath code
    analytic.seriesSolution({ p: 0, q: "-x", order: 7 }).solutions.map((s) => s.text)
    Output
    ["1 + 1/6*x^3 + 1/180*x^6 + …", "x + 1/12*x^4 + 1/504*x^7 + …"]
  19. 19.Euler's methodAgrees
    Use Euler's method with h=0.25h = 0.25 on y′=yy' = y, y(0)=1y(0) = 1 to estimate y(1)y(1).
    Independent answer
    yn+1=1.25 yny_{n+1} = 1.25\,y_n: 1.254=2.441406251.25^4 = 2.44140625 (true value e≈2.71828e \approx 2.71828).
    RatMath code
    analytic.odeMethod("y", { method: "euler", y0: 1, until: 1, steps: 4 }).steps.at(-1).y
    Output
    2.44140625
  20. 20.Newton's law of coolingAgrees
    Coffee at 90° cools in a 20° room, T′=−k(T−20)T' = -k(T - 20). After 10 minutes it is 60°. What is it after 20 minutes?
    Independent answer
    T−20=70e−ktT - 20 = 70e^{-kt}, e−10k=47e^{-10k} = \tfrac47. At 20 minutes: 20+70⋅1649=3007≈42.86°20 + 70\cdot\tfrac{16}{49} = \tfrac{300}{7} \approx 42.86°.
    RatMath code
    PRINT(analytic.firstOrder("-k*(T - 20)", { y: "T", x: "t" }).general);   // T = 20 + C e^(−kt), and C = 70 from T(0) = 90
    const k = symbolic.solve("70*exp(-10*k) = 40", "k").solutions[0].value;
    PRINT("k =", k);
    String(symbolic.simplify(`20 + 70*exp(-20*(${k}))`))
    Output
    300/7
    The general solution comes from firstOrder, kk from the data by solve, and T(20)T(20) is simplified exactly to 300/7300/7. Reading off C=70C = 70 from T(0)=90T(0) = 90 is done by hand.