# Series solution of a differential equation

Calculate the series solution of a differential equation:

$$\frac{\mathrm{d} y}{\mathrm{~d} x}=-y-x$$, ( $$\left.y\right|_{x=0}=2$$)

AsymptoticDsolvevalue can calculate the series solution of a differential equation, but it can only get the result of a finite term:

AsymptoticDSolveValue[{y'[x] == -y[x] - x, y[0] == 2},
y[x], {x, 0, 10}]


2 - 2 x + x^2/2 - x^3/6 + x^4/24 - x^5/120 + x^6/720 - x^7/5040 +
x^8/40320 - x^9/362880 + x^10/3628800

AsymptoticDSolveValue[{y'[x] == -y[x] - x, y[0] == 2},
y[x], {x, 0, n}]


AsymptoticDSolveValue[{Derivative[1][y][x] == -x - y[x], y[0] == 2}, y[x], {x, 0, n}]

How to get the series solution of with n term of a differential equation?

$$\frac{\mathrm{d} y}{\mathrm{~d} x}=-y-x$$, ( $$\left.y\right|_{x=0}=2$$)

result:

\begin{aligned} y &=2-2 x+\frac{1}{2 !} x^{2}-\frac{1}{3 !} x^{3}+\cdots+(-1)^{n} \frac{1}{n !} x^{n}+\cdots \\ &=1-x+\left[1-x+\frac{1}{2 !} x^{2}-\frac{1}{3 !} x^{3}+\cdots+(-1)^{n} \frac{1}{n !} x^{n}+\cdots\right] \end{aligned}

$$=1-x+\sum_{n=0}^{\infty} (-1)^{n} \frac{1}{n !} x^{n}$$

I tried to solve this problem with FindSequenceFunction, but the output-formatting of the result is not factorial.

Clear["Global*"];
sol[n_] :=
AsymptoticDSolveValue[{y'[x] == -y[x] - x, y[0] == 2},
y[x], {x, 0, n}]
Table[sol[n], {n, 1, 17}] // FullSimplify

FindSequenceFunction[%,n]


(E^-x (-E^x Gamma[1 + n] + E^x x Gamma[1 + n] - Gamma[1 + n, -x]))/Gamma[1 + n]

Is there a simpler way or not to use FindSequenceFunction to get factorial results?

a = AsymptoticDSolveValue[{y'[x] == -y[x] - x, y[0] == 2}, y[x], {x, 0, 20}]
(*    2 - 2 x + x^2/2 + ... + x^20/2432902008176640000    *)

c = CoefficientList[a, x]
(*    {2, -2, 1/2, ..., 1/2432902008176640000}    *)

cc[n_] = FindSequenceFunction[c[[3 ;;]], n - 1] // FullSimplify
(*    (-1)^n/Gamma[1 + n]    *)


The problem is that the first two coefficients don't fit the pattern. Looking for a pattern for $$n\ge2$$ works with FindSequenceFunction though.

c[[1]] + c[[2]] x + Sum[cc[n] x^n, {n, 2, ∞}]
(*    2 - 2 x + E^-x (1 - E^x + E^x x)    *)


To extend the Sum explicitly to all $$\mathbb{N}_0$$, we can do

(c[[1]] - cc[0]) + (c[[2]] - cc[1]) x + Defer[Sum][cc[n] x^n, {n, 0, ∞}]
(*    1 - x + Sum[((-1)^n x^n)/Gamma[1 + n], {n, 0, ∞}]    *)


Alternatively, FindGeneratingFunction removes one intermediate step:

c[[1]] + c[[2]] x + x^2 FindGeneratingFunction[c[[3 ;;]], x]
(*    2 - 2 x + E^-x (1 - E^x + E^x x)    *)

• Thanks! c = MonomialList[a, {x}, "DegreeLexicographic"];d = Reverse[c];e = FindSequenceFunction[d[[3 ;;]], n - 1] // FullSimplify;FullSimplify[e, n [Element] Integers && n > 0, ComplexityFunction -> ((LeafCount@# + 10 Count[#, _Gamma | _Pochhammer, {0, [Infinity]}]) &)] Commented Mar 18, 2022 at 10:13
• The outform of the solution I want to get is $1-x+\sum_{n=0}^{\infty}(-1)^{n} \frac{1}{n !} x^{n}$. Commented Mar 18, 2022 at 10:48
• @lotus2019 see update Commented Mar 18, 2022 at 15:39
• Great! Thanks a lot! Commented Mar 19, 2022 at 1:20
series[expr_, x_, x0_] :=
Defer[Sum[#, {n, 0, ∞}]] &[
FullSimplify@SeriesCoefficient[expr, {x, x0, n}, Assumptions -> {n >= 0}] (x - x0)^n]

DSolveValue[{y'[x] == -y[x] - x, y[0] == 2}, y[x], x] // Simplify
% /. E^a_ :> series[E^a, x, 0]


series` is based on https://mathematica.stackexchange.com/a/71593/1871

• This is really a good way. The solution of the differential equation is obtained first, and then transformed into series. Thanks! Commented Mar 19, 2022 at 1:23