# Solve a differential equation using the power series method

I used the power series method to solve the differential equation y’’+y=0 with y=0 and y’=1 using the following code. In this code “y” is considered as “y = Sum[c[i] x^i, {i, 0, n}] + O[x]^(n + 1)”. In this case the boundary conditions resulted to c=0 and c=1 (8th line). I don’t have any problem with this code and it works pretty well.

ClearAll["Global*"]
n=20;
y = Sum[c[i] x^i, {i, 0, n}] + O[x]^(n + 1);
de1 = D[y, {x, 2}] + y == 0;
coeffeqns = LogicalExpand[de1];
coeffs = Solve[coeffeqns, Table[c[i], {i, 2, n}]];
y = y /. coeffs;
mm[x_] := Normal[y] /. {c -> 0, c -> 1};
Plot[Evaluate[mm[x]], {x, -2 Pi, 2 Pi}, PlotRange -> All,
PlotRangeClipping -> True, Frame -> True]


Now, I am going to change the boundary condition with y[L/2]=0 and y[-L/2]=P. (L and P are constant). In this case “y” is considered as “y = Sum[c[i] (x-L/2)^i, {i, 0, n}] + O[x]^(n + 1)”. Using the new boundary conditions resulted to c=0 and -c L+c L^2-c L^3+c L^4-…..=P or Sum[c[i] (-L)^i, {i, 1, n}]=P. I will appreciate if somebody help me to modify the aforementioned code to fit into my new problem.

• interspersing code and text makes your post difficult to read Jul 7, 2015 at 4:05
• I've only seen the power series method applied to initial value problems, never to boundary value problems. If you can tell me how to do it mathematically, I can probably help you write the code. But I doubt it can be done mathematically. Jul 7, 2015 at 5:14

This only works with the assumed n !!!. Let's n = 6.

 ClearAll["Global*"]
n = 6;
y = Sum[c[i] x^i, {i, 0, n}] + O[x]^(n + 1)
de1 = D[y, {x, 2}] + y == 0;
coeffeqns = LogicalExpand[de1]
coeffs = Solve[coeffeqns, Table[c[i], {i, 2, n}]]
y = y /. coeffs

SS = First@
Solve[{Normal[y] == 0 /. x -> L/2,
Normal[y] == P /. x -> -L/2}, {c, c}]


$\left\{c(0)\to -\frac{23040 P}{L^6-120 L^4+5760 L^2-46080},c(1)\to -\frac{1920 P}{L \left(L^4-80 L^2+1920\right)}\right\}$

SOL = Normal[y] /. SS


Yours Solution:

$\left\{-\frac{16 P x^5}{L \left(L^4-80 L^2+1920\right)}+\frac{320 P x^3}{L \left(L^4-80 L^2+1920\right)}-\frac{1920 P x}{L \left(L^4-80 L^2+1920\right)}+\frac{32 P x^6}{L^6-120 L^4+5760 L^2-46080}-\frac{960 P x^4}{L^6-120 L^4+5760 L^2-46080}+\frac{11520 P x^2}{L^6-120 L^4+5760 L^2-46080}-\frac{23040 P}{L^6-120 L^4+5760 L^2-46080}\right\}$

And

 P = 1;
L = 3;
sol = First@NDSolve[{x''[t] + x[t] == 0, x[L/2] == 0, x[-L/2] == P},
x[t], {t, -L/2, L/2}]; Plot[{x[t] /. sol, SOL /. x -> t}, {t, -L/2,
L/2}, PlotLegends -> {"NDSolve solution", " Yours SUM"}] If you put $L=\pi$ in yours equation seeks to $\infty$

General Solution:

$$x''(t)+x(t)=0,x\left(\frac{L}{2}\right)=0,x\left(-\frac{L}{2}\right)=P$$

   Clear[L, P];
yyy = First@DSolve[x''[t] + x[t] == 0, x[t], t];
XSOL = First@
Solve[{(x[t] /. yyy) == 0 /. t -> L/2, (x[t] /. yyy) == P /.
t -> -L/2}, {C, C}];
yyy /. XSOL


$\left\{x(t)\to \frac{1}{2} P \sec \left(\frac{L}{2}\right) \cos (t)-\frac{1}{2} P \csc \left(\frac{L}{2}\right) \sin (t)\right\}$

  Plot[Evaluate[
x[t] /. (yyy /. XSOL) /. {P -> 1, L -> {3, 3.1, 3.14}}], {t, -Pi,
Pi}, PlotLegends -> {"3", "3.1", "3.14"}, PlotRange -> All] Or:

 Limit[(1/2 P Cos[t] Sec[L/2] - 1/2 P Csc[L/2] Sin[t]) /. {t -> L,
P -> 1}, L -> Pi]


is $\infty$.

• Thanks a lot. It was very helpful.