NotebookDirectory[]

Before reading, you can evaluate the entire notebook by using the menu item Evaluate -> Evaluate Notebook, shortcut Alt+v o 
Since the notebook is large, it might take a few minutes for the notebook to evaluate.

After you done working with the notebook it is a prudent practice to delete all output using the menu item Cell -> Delete All Output, shortcut Alt+c l

Three BCs

The heat equation with the Dirichlet boundary conditions

Few solutions of the heat equation with the Dirichlet boundary conditions

Using the method of separation of variables we found “few” solutions of the heat equation with the Dirichlet boundary conditions:

((∂u)/(∂t))= κ(((∂^2)u)/(∂(x^2)))  on   {(x,t) : 0≤x ≤L, t ≥0}

subject to

BCs  u(0,t) =0, u(L,t) =0

These few solutions are

Exp[-κ (((((n π)/L)))^2)t]* Sin[((n π)/L)x]

where  n  is any positive integer.

Verify that these functions solve the equation and satisfy BCs in Mathematica

FullSimplify[
D[Exp[-κ (((((n π)/L)))^2)t]* Sin[((n π)/L)x],t]-κ  D[Exp[-κ (((((n π)/L)))^2)t]* Sin[((n π)/L)x],{x,2}]]

Or, using a more sophisticated code, called Pure Function (# is like a place holder for a function in the differential equation):

FullSimplify[
(D[#,t]-κ  D[#,{x,2}])&[Exp[-κ (((((n π)/L)))^2)t]* Sin[((n π)/L)x]]]

Two boundary conditions are satisfied:

FullSimplify[(Exp[-κ (((((n π)/L)))^2)t]* Sin[((n π)/L)x])/.{x->{0,L}},n∈Integers]

From these few solutions we get many solutions by using the superposition principle:  for arbitrary constants (Subscript[b, n]) the linear combination

u(x,t) =(∑_(n=1)^nn) (Subscript[b, n ])Exp[-κ (((((n π)/L)))^2)t]* Sin[((n π)/L)x]

is also a solution.

To satisfy the initial condition  u(x,0) = f(x)  we will need to find (Subscript[b, n]) such that

f (x)  =(∑_(n=1)^nn) (Subscript[b, n ])Sin[((n π)/L)x]

The idea is to use the orthogonality of  the functions

Sin[((n π)/L)x]

FullSimplify[Integrate[Sin[((n Pi)/L)x]Sin[((m Pi)/L)x],x]]

FullSimplify[Integrate[Sin[((n Pi)/L)x]Sin[((m Pi)/L)x],{x,0,L}]]

FullSimplify[Integrate[Sin[((n Pi)/L)x]Sin[((m Pi)/L)x],{x,0,L}],And[n∈Integers,m∈Integers]]

The above calculation is clearly wrong when n=m

FullSimplify[Integrate[Sin[((n Pi)/L)x]Sin[((n Pi)/L)x],{x,0,L}],And[n∈Integers]]

The orthogonality of the first ten Sin functions is nicely seen from the table below:

MatrixForm[Table[Integrate[Sin[((n Pi)/L)x]Sin[((m Pi)/L)x],{x,0,L}],{n,1,10},{m,1,10}]]

With L= π we have

Plot[
Evaluate[Table[Sin[((n Pi x)/Pi)],{n,1,10}]],
{x,0,Pi},FrameTicks->{Range[-Pi,3 Pi,(Pi/2)],Range[-2,2],{},{}},Frame->True,PlotRange->{-1.1,1.1}]

Manipulate[Plot[Evaluate[Sin[n x]],{x,0,Pi},FrameTicks->{Range[-Pi,3 Pi,(Pi/2)],Range[-2,2],{},{}},Frame->True,PlotRange->{-1.1,1.1}],{{n,1},Range[1,10]},ControlType->Setter,ControlPlacement->Top]

Using the orthogonality we calculate that a good candidate for an approximation of  f(x) is

f (x)  ∼ (∑_(n=1)^nn) (Subscript[b, n ])Sin[((n π)/L)x]

where

(Subscript[b, n ])=(2/L)((∫_0)^L)f(x) Sin[((n π)/L)x]dx

We will explore these approximations below.

Symbolic calculation of the solution

Example 1

(* In this cell nn1 stands for the number of terms that we use to approximate a given function ff1 which is defined by a formula on a given interval LL1 *) 

Clear[nn1,ff1,LL1]; nn1=20;LL1=1; ff1[x_]=x (((x-1))^2);

Plot[Evaluate[{ff1[x]}
],{x,0,LL1},PlotStyle->{{Blue,Thickness[0.01]},{Red,Thickness[0.005]}},Frame->True,PlotRange->{-.02,.17}]

Calculate the coefficients symbolically.

Clear[bb1]; 
bb1[n_]=(2/LL1)Integrate[ff1[s] Sin[((n Pi)/LL1) s],{s,0,LL1}]

nn1=3;

(* nn1 stands for the number of terms that we use to approximate ff1 *)

Plot[Evaluate[{ff1[x],Sum[bb1[n] Sin[((n Pi)/LL1) x],{n,1,nn1}]}
],{x,0,LL1},PlotStyle->{{Blue,Thickness[0.01]},{Red,Thickness[0.005]}},Frame->True,PlotRange->{-.02,.17}]

Manipulate[Plot[Evaluate[{ff1[x],Sum[bb1[n] Sin[((n Pi)/LL1) x],{n,1,jj}]}
],{x,0,LL1},PlotStyle->{{Blue,Thickness[0.01]},{Red,Thickness[0.005]}},Frame->True,PlotRange->{-.02,.17}],{{jj,5},Range[1,20]},ControlType->Setter,ControlPlacement->Top]

nn1=10;Plot3D[Evaluate[{Sum[bb1[n] Exp[-(((((n Pi)/LL1)))^2) t] Sin[((n Pi)/LL1) x],{n,1,nn1}]}
],{x,0,LL1},{t,0,.2},PlotRange->{-0.01,.16}]

Clear[t,Movie1]; 
Movie1[t_]:=
 Plot[Evaluate[{Sum[bb1[n] Exp[-(((((n Pi)/LL1)))^2)t] Sin[((n Pi)/LL1) x],{n,1,nn1}]}
],{x,0,LL1},PlotRange->{-0.01,.16}]

ListAnimate[Table[Movie1[t],{t,0,.5,.01}],AnimationRunning->False,AnimationRepetitions->2,ControlPlacement->Top]

Example 2

Clear[nn2,ff2,LL2]; nn2=20;LL2=1; ff2[x_]=5(Sqrt[x]) (((1-x))^4);

Plot[ff2[x],{x,0,LL2}]

Calculate the coefficients symbolically. If this does not work do it numerically

Clear[bb2]; 

bb2[n_]=FullSimplify[(2/LL2)Integrate[ff2[s] Sin[((n Pi)/LL2) s],{s,0,LL2}]]

nn2=50; (* increasing nn improves accuracy *)

Plot[Evaluate[{ff2[x],Sum[N[bb2[n]] Sin[((n Pi)/LL2) x],{n,1,nn2}]}
],{x,0,LL2},PlotStyle->{{Blue,Thickness[0.01]},{Red,Thickness[0.005]}},Frame->True,PlotRange->{-.2,1.2}]

Plot3D[Evaluate[{Sum[N[bb2[n]] Exp[-(((((n Pi)/LL2)))^2) t] Sin[((n Pi)/LL2) x],{n,1,nn2}]}
],{x,0,LL2},{t,0,.1},PlotRange->{-0.01,1.2}]

Clear[t,Movie2];

Movie2[t_]:=Plot[Evaluate[{Sum[N[bb2[n]]Chop[Exp[-(((((n Pi)/LL2)))^2)t]] Sin[((n Pi)/LL2) x],{n,1,nn2}]}
],{x,0,LL2},PlotRange->{-0.01,1.2}];

Movie2[0.001]

Off[General::munfl];ListAnimate[Table[Movie2[t],{t,0,1,.01}],AnimationRunning->False,AnimationRepetitions->2,ControlPlacement->Top]

Example 3

Clear[nn3,ff3,LL3]; nn3=20;LL3=Pi; ff3[x_]=UnitStep[(Pi/2)-x];

Plot[ff3[x],{x,0,LL3}]

Calculate the coefficients symbolically. If this does not work do it numerically.

Clear[bb3]; 

bb3[n_]=(2/LL3)Integrate[ff3[s] Sin[((n Pi)/LL3) s],{s,0,LL3}]

nn3=50;
Plot[Evaluate[{ff3[x],Sum[bb3[n] Sin[((n Pi)/LL3) x],{n,1,nn3}]}
],{x,0,LL3},PlotStyle->{{Blue,Thickness[0.01]},{Red,Thickness[0.005]}},Frame->True,PlotRange->{-.2,1.4}]

Plot3D[Evaluate[{Sum[bb3[n] Exp[-(((((n Pi)/LL3)))^2) t] Sin[((n Pi)/LL3) x],{n,1,nn3}]}
],{x,0,LL3},{t,0,1},PlotRange->{-0.2,1.25}]

Clear[t,Movie3];
Movie3[t_]:=Plot[Evaluate[{Sum[bb3[n] Exp[-(((((n Pi)/LL3)))^2)t] Sin[n x],{n,1,nn3}]}
],{x,0,LL3},PlotRange->{-0.2,1.3}]

ListAnimate[Table[Movie3[t],{t,0,1,.01}],AnimationRunning->False,AnimationRepetitions->2,ControlPlacement->Top]

Example 4

Clear[nn4,ff4,LL4]; nn4=7;LL4=Pi; ff4[x_]=((Sin[x])^7);

Calculate the coefficients symbolically.

Clear[bb4]; 

bb4[n_]=(2/LL4)Integrate[ff4[s] Sin[((n Pi)/LL4) s],{s,0,LL4}]

There are problems with this formula!

bb4[3]

Factor[Denominator[bb4[n]]]

bbl4=Table[Limit[bb4[n],n-> k],{k,1,12,1}]

The same numbers would have been obtained if we calculate

Table[(2/LL4)Integrate[ff4[s] Sin[((n Pi)/LL4) s],{s,0,LL4}],{n,1,12}]

Thus in this case we have the exact solution if we use seven terms in the expansion of  ff[x].

nn4=7;
Plot[Evaluate[{ff4[x],Sum[bbl4[[n]] Sin[((n Pi)/LL4) x],{n,1,nn4}]}
],{x,0,LL4},PlotStyle->{{Blue,Thickness[0.01]},{Red,Thickness[0.005]}},Frame->True,PlotRange->{-.2,1.1}]

In fact we have the exact formula for ff in this case

FullSimplify[ff4[x]==Sum[bbl4[[n]] Sin[((n Pi)/LL4) x],{n,1,nn4}]]

Plot3D[Evaluate[{Sum[bbl4[[n]] Exp[-(((((n Pi)/LL4)))^2) t] Sin[((n Pi)/LL4) x],{n,1,nn4}]}
],{x,0,LL4},{t,0,1},PlotRange->{-0.2,1.1}]

Clear[t,Movie4];
Movie4[t_]:=Plot[Evaluate[{Sum[bbl4[[n]] Exp[-(((((n Pi)/LL4)))^2)t] Sin[n x],{n,1,nn4}]}
],{x,0,LL4},PlotRange->{-0.1,1.1}]

ListAnimate[Table[Movie4[t],{t,0,1,.01}],AnimationRunning->False,AnimationRepetitions->2,ControlPlacement->Top]

Numeric calculation of an approximation for the solution

Example 5

Clear[mm5,gg5,LL5]; mm5=20;LL5=Pi; gg5[x_]=((Sin[x])^(1/2));

Calculate the coefficients symbolically. (Not working.)

(* bb[n_]=(2/LL)Integrate[gg[s] Sin[((n Pi)/LL) s],{s,0,LL},Assumptions->And[n∈Integers,n>0]] 
*)

Clear[bbl5];

bbl5=Chop[Table[(2/LL5)NIntegrate[
gg5[s] Sin[((n Pi)/LL5) s],
{s,0,LL5},
Method->{Automatic},MaxRecursion->200,AccuracyGoal->12,PrecisionGoal->16],{n,1,mm5}]]

Plot[Evaluate[{gg5[x],Sum[bbl5[[n]] Sin[((n Pi)/LL5) x],{n,1,mm5}]}
],{x,0,LL5},PlotStyle->{{Blue,Thickness[0.01]},{Red,Thickness[0.005]}},Frame->True,PlotRange->{-.2,1.15}]

Plot3D[Evaluate[Sum[bbl5[[n]] Exp[-(((((n Pi)/LL5)))^2) t] Sin[((n Pi)/LL5) x],{n,1,mm5}]
],{x,0,LL5},{t,0,2},PlotRange->{-0.1,1.1}]

Clear[t,Movie5];

Movie5[t_]:=Plot[Evaluate[Sum[bbl5[[n]] Exp[-(((((n Pi)/LL5)))^2) t] Sin[((n Pi)/LL5) x],{n,1,mm5}]
],{x,0,LL5},PlotRange->{-0.1,1.1}]

ListAnimate[Table[Movie5[t],{t,0,2,.05}],AnimationRunning->False,AnimationRepetitions->2,ControlPlacement->Top]

Example 6

Clear[mm6,gg6,LL6]; mm6=20;LL6=Pi; gg6[x_]=((Sin[x])^(1/10));

Calculate the coefficients numerically.

Clear[bbl6];

bbl6=Chop[Table[(2/LL6)NIntegrate[gg6[s] Sin[((n Pi)/LL6) s],{s,0,LL6},Method->{Automatic},MaxRecursion->200,AccuracyGoal->12,PrecisionGoal->16],{n,1,mm6,1}]]

Plot[Evaluate[{gg6[x],Sum[bbl6[[n]] Sin[((n Pi)/LL6) x],{n,1,mm6}]}
],{x,-0.,LL6},PlotStyle->{{Blue,Thickness[0.01]},{Red,Thickness[0.005]}},Frame->True,PlotRange->{{-0.1,LL6+.1},{-.2,1.25}},Exclusions->None,PlotPoints->400]

Plot3D[Evaluate[{Sum[bbl6[[n]] Exp[-(((((n Pi)/LL6)))^2) t] Sin[((n Pi)/LL6) x],{n,1,mm6}]}
],{x,0,LL6},{t,0,2},PlotRange->{-0.1,1.15},PlotPoints->200]

Clear[t,Movie6];
Movie6[t_]:=Plot[Evaluate[{Sum[bbl6[[n]] Exp[-(((((n Pi)/LL6)))^2) t] Sin[((n Pi)/LL6) x],{n,1,mm6}]}
],{x,0,LL6},PlotRange->{-0.1,1.1}]

ListAnimate[Table[Movie6[t],{t,0,2,.05}],AnimationRunning->False,AnimationRepetitions->2,ControlPlacement->Top]

The heat equation with the Neumann boundary conditions

Few solutions of the heat equation with the Neumann boundary conditions

Using the method of separation of variables we found “few” solutions of the heat equation with the Neumann boundary conditions:

((∂u)/(∂t))=κ(((∂^2)u)/(∂(x^2)))  on   {(x,t) : 0≤x ≤L, t ≥0}

subject to

BCs  ((∂u)/(∂x))(0,t) =0, ((∂u)/(∂x))(L,t) =0

These few solutions are

1

and

Exp[-κ (((((n π)/L)))^2)t]* Cos[((n π)/L)x]

where  n  is any positive integer.  From these few soluting we get many solutions by using the superposition principle:  for arbitrary constants (Subscript[a, n]) the linear combination

u(x,t) =(Subscript[a, 0])+(∑_(n=1)^nn) (Subscript[a, n ])Exp[-κ (((((n π)/L)))^2)t]* Cos[((n π)/L)x]

is also a solution.

To satisfy the initial condition  u(x,0) = f(x)  we will need to find (Subscript[a, n]) such that

f (x)  =(Subscript[a, 0])+(∑_(n=1)^nn) (Subscript[a, n ])Cos[((n π)/L)x]

The idea is to use the orthogonality of  the functions

Cos[((n π)/L)x]

Clear[L];FullSimplify[Integrate[Cos[((n Pi)/L)x]Cos[((m Pi)/L)x],x]]

Clear[L];FullSimplify[Integrate[Cos[((n Pi)/L)x]Cos[((m Pi)/L)x],{x,0,L}]]

Clear[L];FullSimplify[Integrate[Cos[((n Pi)/L)x]Cos[((m Pi)/L)x],{x,0,L}],And[n∈Integers,m∈Integers]]

The above calculation is clearly wrong when n=m

Clear[L]; FullSimplify[Integrate[Cos[((n Pi)/L)x]Cos[((n Pi)/L)x],{x,0,L}],And[n∈Integers]]

The orthogonality of the first ten Cos functions and the constant is nicely seen from the table below:

MatrixForm[Table[Integrate[Cos[((n Pi)/L)x]Cos[((m Pi)/L)x],{x,0,L}],{n,0,10},{m,0,10}]]

With L= π we have

Plot[
Evaluate[Table[Cos[((n Pi x)/Pi)],{n,0,10}]],
{x,0,Pi},FrameTicks->{Range[-Pi,3 Pi,(Pi/2)],Range[-2,2],{},{}},Frame->True,PlotRange->{-1.1,1.1}]

Manipulate[Plot[Evaluate[Cos[n x]],{x,0,Pi},FrameTicks->{Range[-Pi,3 Pi,(Pi/2)],Range[-2,2],{},{}},Frame->True,PlotRange->{-1.1,1.1}],{{n,1},Range[0,10]},ControlType->Setter,ControlPlacement->Top]

Using the orthogonality we calculate that a good candidate for approximation of  f(x) is

f (x)  ∼(Subscript[a, 0]) +(∑_(n=1)^nn) (Subscript[a, n ])Cos[((n π)/L)x]

where

(Subscript[a, 0 ])=(1/L)((∫_0)^L)f(x) dx ,  (Subscript[a, n ])=(2/L)((∫_0)^L)f(x) Cos[((n π)/L)x]dx, n > 0.

We will explore these approximations below.

Symbolic calculation of the solution

Example 7

Clear[nn7,ff7,LL7]; nn7=20;LL7=1; ff7[x_]=(x^2) (((x-2))^2);

Plot[Evaluate[ff7[x]],{x,0,LL7},PlotStyle->{{Blue,Thickness[0.01]},{Red,Thickness[0.005]}},Frame->True,PlotRange->{-.02,1.2}]

Calculate the coefficients symbolically.

Clear[aa7]; 

aa7[0]=(1/LL7)Integrate[ff7[s],{s,0,LL7}];

aa7[n_]=(2/LL7)Integrate[ff7[s] Cos[((n Pi)/LL7) s],{s,0,LL7}]

nn7=7;

(* nn7 stands for the number of terms that we use to approximate ff *)

Plot[Evaluate[{ff7[x],Sum[aa7[n] Cos[((n Pi)/LL7) x],{n,0,nn7}]}
],{x,0,LL7},PlotStyle->{{Blue,Thickness[0.01]},{Red,Thickness[0.005]}},Frame->True,PlotRange->{-.02,1.2}]

Plot3D[Evaluate[{Sum[aa7[n] Exp[-(((((n Pi)/LL7)))^2) t] Cos[((n Pi)/LL7) x],{n,0,nn7}]}
],{x,0,LL7},{t,0,.2},PlotRange->{-0.01,1.2}]

Clear[t,Movie7]; 
Movie7[t_]:=
 Plot[Evaluate[{Sum[aa7[n] Exp[-(((((n Pi)/LL7)))^2)t] Cos[((n Pi)/LL7) x],{n,0,nn7}]}
],{x,0,LL7},PlotRange->{-0.01,1.2}]

ListAnimate[Table[Movie7[t],{t,0,.2,.005}],AnimationRunning->False,AnimationRepetitions->2,ControlPlacement->Top]

Numeric calculation of the solution

Example 8

Clear[mm8,gg8,LL8]; mm8=20;LL8=Pi; gg8[x_]=(((((Cos[x/2])^8)))^(1/3));

Plot[Evaluate[{gg8[x]}
],{x,0,LL8},PlotStyle->{{Blue,Thickness[0.01]},{Red,Thickness[0.005]}},Frame->True,PlotRange->{-.2,1.15}]

Clear[aal8];

aal8=Chop[Prepend[Table[(2/LL8)NIntegrate[
gg8[s] Cos[((n Pi)/LL8) s],
{s,0,LL8},
Method->{Automatic},MaxRecursion->200,AccuracyGoal->12,PrecisionGoal->16],{n,1,mm8}],
(1/LL8)NIntegrate[
gg8[s],
{s,0,LL8},
Method->{Automatic},MaxRecursion->200,AccuracyGoal->12,PrecisionGoal->16]]]

Length[aal8]

Plot[Evaluate[{gg8[x],Sum[aal8[[n+1]] Cos[((n Pi)/LL8) x],{n,0,Length[aal8]-1}]}
],{x,0,LL8},PlotStyle->{{Blue,Thickness[0.01]},{Red,Thickness[0.005]}},Frame->True,PlotRange->{-.2,1.15}]

Plot3D[Evaluate[{Sum[aal8[[n+1]] Exp[-(((((n Pi)/LL8)))^2) t] Cos[((n Pi)/LL8) x],{n,0,Length[aal8]-1}]}
],{x,0,LL8},{t,0,3},PlotRange->{-0.1,1.1}]

Clear[t,Movie8];

Movie8[t_]:=Plot[Evaluate[{Sum[aal8[[n+1]] Exp[-(((((n Pi)/LL8)))^2) t] Cos[((n Pi)/LL8) x],{n,0,Length[aal8]-1}]}
],{x,0,LL8},PlotRange->{-0.1,1.1}]

ListAnimate[Table[Movie8[t],{t,0,3,.05}],AnimationRunning->False,AnimationRepetitions->2,ControlPlacement->Top]

The heat equation with the periodic  boundary conditions

Few solutions of the heat equation with the periodic boundary conditions

Using the method of separation of variables we found “few” solutions of the heat equation with the periodic boundary conditions:

((∂u)/(∂t))=κ(((∂^2)u)/(∂(x^2)))  on   {(x,t) : -L≤x ≤L, t ≥0}

subject to

BCs:  u(-L,t) =u(L,t) ,  ((∂u)/(∂x))(-L,t) =((∂u)/(∂x))(L,t)

These few solutions are

1

and

Exp[-κ (((((n π)/L)))^2)t]* Cos[((n π)/L)x]

and

Exp[-κ (((((n π)/L)))^2)t]* Sin[((n π)/L)x]

where  n  is any positive integer.  From these few soluting we get many solutions by using the superposition principle:  for arbitrary constants (Subscript[b, n]) the linear combination

u(x,t) =(Subscript[a, 0])+(∑_(n=1)^nn) (Subscript[a, n ])Exp[-κ (((((n π)/L)))^2)t]* Cos[((n π)/L)x] + (∑_(n=1)^nn) (Subscript[b, n ])Exp[-κ (((((n π)/L)))^2)t]* Sin[((n π)/L)x]

is also a solution.

To satisfy the initial condition  u(x,0) = f(x)  we will need to find (Subscript[a, n])s and (Subscript[b, n])s such that

f (x)  =(Subscript[a, 0])+(∑_(n=1)^nn) (Subscript[a, n ])Cos[((n π)/L)x]+(∑_(n=1)^nn) (Subscript[b, n ])Sin[((n π)/L)x]

The idea is to use the orthogonality of  the functions

1

and

Cos[((n π)/L)x]

and

Sin[((n π)/L)x]

on the interval  [-L,L].

Clear[L];FullSimplify[Integrate[Cos[((n Pi)/L)x]Cos[((m Pi)/L)x],{x,-L,L}]]

Clear[L];FullSimplify[Integrate[Cos[((n Pi)/L)x]Cos[((m Pi)/L)x],{x,-L,L}],And[n∈Integers,m∈Integers]]

The above calculation is clearly wrong when n=m

Clear[L]; FullSimplify[Integrate[Cos[((n Pi)/L)x]Cos[((n Pi)/L)x],{x,-L,L}],And[n∈Integers]]

Similarly

Clear[L];FullSimplify[Integrate[Sin[((n Pi)/L)x]Sin[((m Pi)/L)x],{x,-L,L}],And[n∈Integers,m∈Integers]]

The above calculation is clearly wrong when n=m

Clear[L]; FullSimplify[Integrate[Sin[((n Pi)/L)x]Sin[((n Pi)/L)x],{x,-L,L}],And[n∈Integers]]

and finally

Clear[L];FullSimplify[Integrate[Cos[((n Pi)/L)x]Sin[((m Pi)/L)x],{x,-L,L}],And[n∈Integers,m∈Integers]]

The orthogonality of the first ten Cos functions and the constant is nicely seen from the table below:

CoSiFunT=Join[Table[Cos[((n π)/L)x],{n,0,5}],Table[Sin[((n π)/L)x],{n,1,5}]]

MatrixForm[Table[Integrate[CoSiFunT[[j]] CoSiFunT[[k]],{x,-L,L}],{j,1,Length[CoSiFunT]},{k,1,Length[CoSiFunT]}]
]

Using the orthogonality we calculate that a good candidate for approximation of  f(x) is

f (x)  ∼(Subscript[a, 0]) +(∑_(n=1)^nn) (Subscript[a, n ])Cos[((n π)/L)x]+(∑_(n=1)^nn) (Subscript[b, n ])Sin[((n π)/L)x]

where

(Subscript[a, 0 ])=(1/(2L))((∫_(-L))^L)f(x) dx ,  (Subscript[a, n ])=(1/L)((∫_0)^L)f(x) Cos[((n π)/L)x]dx, (Subscript[b, n ])=(1/L)((∫_(-L))^L)f(x) Sin[((n π)/L)x]dx, n > 0.

We will explore these approximations below.

Symbolic calculation of the solution

Example 9

Clear[nn9,ff9,LL9]; nn9=20;LL9=1; ff9[x_]=((x^2)-1)(3x-1)+1;

Plot[Evaluate[{ff9[x]}
],{x,-LL9,LL9},PlotStyle->{{Blue,Thickness[0.01]},{Red,Thickness[0.005]}},Frame->True,PlotRange->{0,3}]

Calculate the coefficients symbolically.

Clear[aa9]; 

aa9[0]=(1/(2 LL9))Integrate[ff9[s],{s,-LL9,LL9}];

aa9[n_]=(1/LL9)Integrate[ff9[s] Cos[((n Pi)/LL9) s],{s,-LL9,LL9}];

bb9[n_]=(1/LL9)Integrate[ff9[s] Sin[((n Pi)/LL9) s],{s,-LL9,LL9}]

nn9=7;

(* nn stands for the number of terms that we use to approximate ff *)

Plot[Evaluate[{ff9[x],Sum[aa9[n] Cos[((n Pi)/LL9) x],{n,0,nn9}]+Sum[bb9[n] Sin[((n Pi)/LL9) x],{n,1,nn9}]}
],{x,-LL9,LL9},PlotStyle->{{Blue,Thickness[0.01]},{Red,Thickness[0.005]}},Frame->True,PlotRange->{0,3}]

Plot3D[Evaluate[{Sum[aa9[n] Exp[-(((((n Pi)/LL9)))^2) t] Cos[((n Pi)/LL9) x],{n,0,nn9}]+Sum[bb9[n] Exp[-(((((n Pi)/LL9)))^2) t] Sin[((n Pi)/LL9) x],{n,1,nn9}]}
],{x,-LL9,LL9},{t,0,.5},PlotRange->{0,3}]

Clear[t,Movie9]; 
Movie9[t_]:=
 Plot[Evaluate[{Sum[aa9[n] Exp[-(((((n Pi)/LL9)))^2)t] Cos[((n Pi)/LL9) x],{n,0,nn9}]+Sum[bb9[n] Exp[-(((((n Pi)/LL9)))^2)t] Sin[((n Pi)/LL9) x],{n,1,nn9}]}
],{x,-LL9,LL9},PlotRange->{0,3}]

ListAnimate[Table[Movie9[t],{t,0,1,.005}],AnimationRunning->False,AnimationRepetitions->2,ControlPlacement->Top]

Numeric calculation of the solution

Example 10

Clear[mm10,gg10,LL10]; mm10=10;LL10=1; gg10[x_]=((((-Sinh[1]x+Cosh[1])Exp[x]))^(3/2));

gg10[-1]==gg10[1]

Plot[Evaluate[{gg10[x]}
],{x,-LL10,LL10},PlotStyle->{{Blue,Thickness[0.01]},{Red,Thickness[0.005]}},Frame->True,PlotRange->{0,2.3}]

Try symbolic integration (slow)

(* (1/LL)Integrate[
gg[s] Cos[((n Pi)/LL) s],
{s,-LL,LL}] *)

Clear[aal10];

aal10=Chop[Prepend[Table[(1/LL10)NIntegrate[
gg10[s] Cos[((n Pi)/LL10) s],
{s,-LL10,LL10},
Method->{Automatic},MaxRecursion->200,AccuracyGoal->12,PrecisionGoal->16],{n,1,mm10}],
(1/(2LL10))NIntegrate[
gg10[s],
{s,-LL10,LL10},
Method->{Automatic},MaxRecursion->200,AccuracyGoal->12,PrecisionGoal->16]]]

Length[aal10]

bbl10=Chop[Table[(1/LL10)NIntegrate[
gg10[s] Sin[((n Pi)/LL10) s],
{s,-LL10,LL10},
Method->{Automatic},MaxRecursion->200,AccuracyGoal->12,PrecisionGoal->16],{n,1,mm10}]]

Plot[Evaluate[{gg10[x],Sum[aal10[[n+1]] Cos[((n Pi)/LL10) x],{n,0,Length[aal10]-1}]+Sum[bbl10[[n]] Sin[((n Pi)/LL10) x],{n,1,Length[bbl10]}]}
],{x,-LL10,LL10},PlotStyle->{{Blue,Thickness[0.01]},{Red,Thickness[0.005]}},Frame->True,PlotRange->{0,2.3}]

Plot3D[Evaluate[{Sum[aal10[[n+1]] Exp[-(((((n Pi)/LL10)))^2) t] Cos[((n Pi)/LL10) x],{n,0,Length[aal10]-1}]+Sum[bbl10[[n]] Exp[-(((((n Pi)/LL10)))^2) t] Sin[((n Pi)/LL10) x],{n,1,Length[bbl10]}]}
],{x,-LL10,LL10},{t,0,.2},PlotRange->{0.8,2.2}]

Clear[t,Movie10];

Movie10[t_]:=Plot[Evaluate[{Sum[aal10[[n+1]] Exp[-(((((n Pi)/LL10)))^2) t] Cos[((n Pi)/LL10) x],{n,0,Length[aal10]-1}]+Sum[bbl10[[n]] Exp[-(((((n Pi)/LL10)))^2) t] Sin[((n Pi)/LL10) x],{n,1,Length[bbl10]}]}
],{x,-LL10,LL10},PlotRange->{0.8,2.2}]

ListAnimate[Table[Movie10[t],{t,0,.5,.01}],AnimationRunning->False,AnimationRepetitions->2,ControlPlacement->Top]