In[]:=
Clear["Global`*"]
a)
Αccurate solution of the problem
In[]:=
eigen[n_]:=n+1/2
In[]:=
Her[n_]:=HermiteH[n,x]
In[]:=
Y[n_]:=Exp[-x^2/2]*Her[n]
(*definethefunction*)
In[]:=
f[x_]=Y[1]+0.2Y[4]+0.01Y[6]
Out[]=
2
-
2
x
2

x+0.2
-
2
x
2

(12-48
2
x
+16
4
x
)+0.01
-
2
x
2

(-120+720
2
x
-480
4
x
+64
6
x
)
In[]:=
c[n_]=((2*n+1)/2)*Integrate[f[x]*LegendreP[n,x],{x,-1,1}]
Out[]=
1
2
(1+2n)
1
∫
-1
2
-
2
x
2

x+0.2
-
2
x
2

(12-48
2
x
+16
4
x
)+0.01
-
2
x
2

(-120+720
2
x
-480
4
x
+64
6
x
)LegendreP[n,x]x
In[]:=
TableForm[Table[{n,N[c[n]]},{n,1,10}],TableHeadings{None,{"n","
c
n
"}}]
Out[]//TableForm=
n
c
n
1
1.49456
2
-1.76788
3
-0.3047
4
0.164605
5
0.0243538
6
0.0321935
7
-0.00119651
8
-0.00532758
9
0.0000423111
10
0.000375739
​
In[]:=
S[x_,N_]:=Sum[c[n]*Exp[-x^2/2]*LegendreP[n,x],{n,1,N}]
In[]:=
fig5=Ap[x_]=S[x,2]
Out[]=
1.49456
-
2
x
2

x-0.883941
-
2
x
2

(-1+3
2
x
)
In[]:=
Plot[{f[x],Ap[x]},{x,-1,1},PlotStyle{{Blue,Thick},{Red,Dashed}}]
Out[]=
-1.0
-0.5
0.5
1.0
-2
-1
1
​
B)
Νumerical solution of the problem
(*definePDE*)
In[]:=
L=21;
In[]:=
PDE=
∂
t
u[x,t]-
1
2
*
∂
x,x
u[x,t]+
1
2
*
2
x
*u[x,t]0
Out[]=
1
2
2
x
u[x,t]+
(0,1)
u
[x,t]-
1
2
(2,0)
u
[x,t]0
(*InitialCondition*)
In[]:=
IC=u[x,0]f[x]
Out[]=
u[x,0]2
-
2
x
2

x+0.2
-
2
x
2

(12-48
2
x
+16
4
x
)+0.01
-
2
x
2

(-120+720
2
x
-480
4
x
+64
6
x
)
In[]:=
BCs={(u[x,t]/.x-L)0,(u[x,t]/.xL)0}
Out[]=
{u[-21,t]0,u[21,t]0}
In[]:=
sol=NDSolve[Join[{PDE},BCs,{IC}],u,{x,-L,L},{t,0,360},MaxStepSize10000,MaxSteps1000,Method->"BDF"]
Out[]=
uInterpolatingFunction
Domain: {{-21.,21.},{0.,360.}}
Output: scalar

Arithmetic solution graph
In[]:=
Plot3D[u[x,t]/.sol,{t,0,10},{x,-L,L}]
Out[]=