MATH76109/07/2018
Mathematica implementation of finite difference calculus
In[1]:=
x \[Element] Reals; $Assumptions = h > 0;
Dp[f_[x_]] := f[x + h] - f[x];
Dm[f_[x_]] := f[x] - f[x - h];
d[f_[x_]] := f[x+h/2]-f[x-h/2];
Dp[a_ f_[x_]]=a Dp[f[x]]; Dp[a_+b_]=Dp[a]+Dp[b]; Dp[a_
(b_+c_)]=a Dp[b]+a Dp[c]; Dp[a_]=0;
Dm[a_ f_[x_]]=a Dm[f[x]]; Dm[a_+b_]=Dm[a]+Dm[b]; Dm[a_
(b_+c_)]=a Dm[b]+a Dm[c]; Dm[a_]=0;
d[a_ f_[x_]]=a d[f[x]]; d[a_+b_]= d[a]+ d[b]; d[a_
(b_+c_)]=a d[b]+a d[c]; d[a_]=0;
fdSort[expr_]:=Assuming[{h > 0, x > 0},
Sort[Level[expr, 1], Level[#1, {Depth[#1] - 1}] <
Level[#2, {Depth[#2] - 1}] &]];
Q[x_]=Subscript[Q,x];
Dp[f_[x_], p_] :=
Together[Normal[1/h Series[Log[1 + z], {z, 0, p}]] /.
Table[z^j -> Nest[Dp, f[x], j], {j, 1, p}]];
Dm[f_[x_], p_] :=
Together[Normal[-1/h Series[Log[1 - z], {z, 0, p}]] /.
Table[z^j -> Nest[Dm, f[x], j], {j, 1, p}]];
d[f_[x_], p_] := Together[Normal[2/h Series[ArcSinh[z/2],
{z, 0, 2 p - 1}]] /.
Table[z^j -> Nest[d, f[x], j], {j, 1, 2 p - 1}]];
Dp[f_[x_], p_, k_] :=
Together[Expand[(Normal[1/h Series[Log[1 + z], {z, 0,
p}]])^k] /.
Table[z^j -> Nest[Dp, f[x], j], {j, 1, p k}]];
Dm[f_[x_], p_, k_] :=
Together[Expand[(Normal[-1/h Series[Log[1 - z], {z, 0,
p}]])^k] /.
Table[z^j -> Nest[Dm, f[x], j], {j, 1, p k}]];
d[f_[x_], p_, k_] := Together[Expand[(Normal[2/h
Series[ArcSinh[z/2], {z, 0, 2 p - 1}]])^k]
/. Table[z^j -> Nest[d, f[x], j], {j, 1, (2 p - 1)k}]];
{qt1,qxx2,qxx4}={Dp[f[x],1],d[f[x],1,2],d[f[x],2,2]}
In[2]:=
The Newton form of the interpolating polynomial of data is
In[2]:=
pNewton[q_[t_],r_] := Sum[ Nest[Dp,q[0],j]/h^j Product[(t
- n h),{n,0,j-1}]/j!,{j,0,r}];
Table[Simplify[pNewton[q[t],r]],{r,0,3}] // TableForm
In[3]:=
Verify the above implementation for the first few interpolating polynomials
In[3]:=
p1[t_] = pNewton[q[t],1]
In[4]:=
{p1[0],p1[h]}
In[5]:=
p2[t_] = pNewton[q[t],2]
In[6]:=
Simplify[{p2[0],p2[h],p2[2h]}]
In[7]:=
p3[t_] = pNewton[q[t],3]
In[8]:=
Simplify[{p3[0],p3[h],p3[2h],p3[3h]}]
In[9]:=
In a linear multistep method (LMM) the solution to ODE is sought by linear combination of values , with .
Adams-Bashforth schemes are obtained by integrating the ODE over interval
and approximating by the interpolating polynomial . The following computes the lhs, rhs of the above formula, setting .
In[9]:=
AdamsBashforth[q_[t_],r_] :=
{q[(r+1)k],q[r k]+Integrate[ pNewton[f[t],r] /. h->k,
{t,r k,(r+1) k}]};
ABschemes = Table[AdamsBashforth[q[t],r],{r,0,4}] //
Apart;
ABschemes // TableForm
In[10]:=
Adams-Moulton are are obtained by integrating the ODE over interval
and approximating by the interpolating polynomial . The following computes the lhs, rhs of the above formula, setting .
In[10]:=
AdamsMoulton[q_[t_],r_] :=
{q[r k],q[(r-1) k]+Integrate[ pNewton[f[t],r] /.
h->k, {t,(r-1) k,r k}]};
AMschemes = Table[AdamsMoulton[q[t],r],{r,0,4}] // Apart;
AMschemes // TableForm
In[11]:=
Define a function to obtain the characteristic polynomial given as a pair {lhs,rhs}
In[11]:=
CharPoly[s_] := Module[{cp,g,F,Q},
g[t_]=f[q[t]]; F[q_] = lambda q; Q[t_] =
zeta^(t/k);
cp = Simplify[s[[1]] - s[[2]] /. f->g /.
f->F /. q->Q];
Return[Simplify[cp /. k->(z/lambda)]]
];
CharPoly[ABschemes[[1]]]
In[12]:=
Compute and display the characteristic polynomials for Adams-Bashforth, Adams-Moulton schemes
In[12]:=
ABCPs = Map[CharPoly[#]&,ABschemes];
ABCPs // TableForm
In[13]:=
AMCPs = Map[CharPoly[#]&,AMschemes];
AMCPs // TableForm
In[14]:=
Define a function to extract the boundary locus , i.e.
In[14]:=
BLocus[p_] := z /. Solve[ p == 0, z][[1]] /.
zeta->Exp[I theta];
BLocus[ABCPs[[1]]]
In[15]:=
Compute and display the boundary locii for Adams-Bashforth, Adams-Moulton schemes
In[15]:=
ABBLs = Map[BLocus[#]&, ABCPs];
ABBLs // TableForm
In[16]:=
AMBLs = Map[BLocus[#]&, AMCPs];
AMBLs // TableForm
Define a function to plot a family of boundary locii, and apply it to Adams-Bashforth and Adams-Moulton schemes
In[17]:=
BLocusPlot[bl_,blname_] :=
ParametricPlot[Evaluate[Map[{Re[#],Im[#]}&,bl]],{theta,0,2Pi},Axes->True,Frame->True,
FrameLabel->{"Re(z)","Im(z)"},GridLines->Automatic,AspectRatio->Automatic,
PlotLabel->blname,PlotLegends->Automatic,PlotRange->{{-3,3},{-3,3}}];
ABBLplots = BLocusPlot[ABBLs,"Adams-Bashforth
boundary locii"];
Export["/home/student/courses/MATH761/AdamsBashforthBoundaryLocii.pdf",ABBLplots]
In[18]:=
AMBLplots = BLocusPlot[AMBLs,"Adams-Moulton boundary
locii"];
Export["/home/student/courses/MATH761/AdamsMoultonBoundaryLocii.pdf",AMBLplots]
In[19]:=