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[]=
2x+0.2(12-48+16)+0.01(-120+720-480+64)
-
2
x
2
-
2
x
2
2
x
4
x
-
2
x
2
2
x
4
x
6
x
In[]:=
c[n_]=((2*n+1)/2)*Integrate[f[x]*LegendreP[n,x],{x,-1,1}]
Out[]=
1
2
1
∫
-1
-
2
x
2
-
2
x
2
2
x
4
x
-
2
x
2
2
x
4
x
6
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.49456x-0.883941(-1+3)
-
2
x
2
-
2
x
2
2
x
In[]:=
Plot[{f[x],Ap[x]},{x,-1,1},PlotStyle{{Blue,Thick},{Red,Dashed}}]
Out[]=
B)
Νumerical solution of the problem
(*definePDE*)
In[]:=
L=21;
In[]:=
PDE=u[x,t]-*u[x,t]+**u[x,t]0
∂
t
1
2
∂
x,x
1
2
2
x
Out[]=
1
2
2
x
(0,1)
u
1
2
(2,0)
u
(*InitialCondition*)
In[]:=
IC=u[x,0]f[x]
Out[]=
u[x,0]2x+0.2(12-48+16)+0.01(-120+720-480+64)
-
2
x
2
-
2
x
2
2
x
4
x
-
2
x
2
2
x
4
x
6
x
In[]:=
BCs={(u[x,t]/.x-L)0,(u[x,t]/.xL)0}
Out[]=
{u[-21,t]0,u[21,t]0}
In[]:=
sol=NDSolve[Join[{PDE},BCs,{IC}],u,{x,-L,L},{t,0,360},MaxStepSize10000,MaxSteps1000,Method->"BDF"]
Out[]=
uInterpolatingFunction
Arithmetic solution graph
In[]:=
Plot3D[u[x,t]/.sol,{t,0,10},{x,-L,L}]
Out[]=