MATH76109/07/2018

Lab03: Analysis of finite difference schemes

1Finite difference approximations

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]:=

2Interpolating polynomial

The Newton form of the interpolating polynomial of data 𝒟={(tn=nh,Qn),n=0,1,,r} is

pr(t)=Q0+[Q1,Q0](t-t0)++[Qr,,Q1,Q0](t-t0)(t-tr-1)

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

q(0) t(q(h)-q(0))h+q(0) t(-2q(h)+q(2h)+q(0))(t-h)2h2+t(q(h)-q(0))h+q(0) 3tq(h)(6h2-5ht+t2)+(h-t)(3tq(2h)(t-3h)+(2h-t)(tq(3h)+3hq(0)-q(0)t))6h3

In[3]:=

Verify the above implementation for the first few interpolating polynomials

In[3]:=

p1[t_] = pNewton[q[t],1]

t(q(h)-q(0))h+q(0)

In[4]:=

{p1[0],p1[h]}

{q(0),q(h)}

In[5]:=

p2[t_] = pNewton[q[t],2]

t(-2q(h)+q(2h)+q(0))(t-h)2h2+t(q(h)-q(0))h+q(0)

In[6]:=

Simplify[{p2[0],p2[h],p2[2h]}]

{q(0),q(h),q(2h)}

In[7]:=

p3[t_] = pNewton[q[t],3]

t(q(h)-q(2h)-2(q(2h)-q(h))+q(3h)-q(0))(t-2h)(t-h)6h3+t(-2q(h)+q(2h)+q(0))(t-h)2h2+t(q(h)-q(0))h+q(0)

In[8]:=

Simplify[{p3[0],p3[h],p3[2h],p3[3h]}]

{q(0),q(h),q(2h),q(3h)}

In[9]:=

3Linear multistep methods

In a linear multistep method (LMM) the solution to ODE q'=f(q,t) is sought by linear combination of values Qnq(tn), Fn(tn,q(tn)) with tn=nk.

3.1Adams-Bashforth

Adams-Bashforth schemes are obtained by integrating the ODE over interval [tn+r,tn+r-1]

Qn+1+r=Qn+r+(n+r)k(n+1+r)kf(t,q(t))dt,

and approximating f by the interpolating polynomial pr(t). The following computes the lhs, rhs of the above formula, setting n=0.

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

q(k) f(0)k+q(0) q(2k) q(k)-12k(f(0)-3f(k)) q(3k) 112k(-16f(k)+23f(2k)+5f(0))+q(2k) q(4k) q(3k)-124k(-37f(k)+59f(2k)-55f(3k)+9f(0)) q(5k) 1720k(-1274f(k)+2616f(2k)-2774f(3k)+1901f(4k)+251f(0))+q(4k)

In[10]:=

3.2Adams-Moulton

Adams-Moulton are are obtained by integrating the ODE over interval [tn+r-1,tn+r]

Qn+r=Qn+r-1+(n+r-1)k(n+r)kf(t,q(t))dt,

and approximating f by the interpolating polynomial pr(t). The following computes the lhs, rhs of the above formula, setting n=0.

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

q(0) f(0)k+q(-k) q(k) 12k(f(k)+f(0))+q(0) q(2k) q(k)-112k(-8f(k)-5f(2k)+f(0)) q(3k) 124k(-5f(k)+19f(2k)+9f(3k)+f(0))+q(2k) q(4k) q(3k)-1720k(-106f(k)+264f(2k)-646f(3k)-251f(4k)+19f(0))

In[11]:=

4Stability analysis

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]]]

-z+ζ-1

In[12]:=

Compute and display the characteristic polynomials for Adams-Bashforth, Adams-Moulton schemes

In[12]:=

ABCPs = Map[CharPoly[#]&,ABschemes];

ABCPs // TableForm

-z+ζ-1 (ζ-1)ζ-12z(3ζ-1) (ζ-1)ζ2-112z(23ζ2-16ζ+5) (ζ-1)ζ3-124z(55ζ3-59ζ2+37ζ-9) (ζ-1)ζ4-1720z(1901ζ4-2774ζ3+2616ζ2-1274ζ+251)

In[13]:=

AMCPs = Map[CharPoly[#]&,AMschemes];

AMCPs // TableForm

-z-1ζ+1 -12z(ζ+1)+ζ-1 (ζ-1)ζ-112z(5ζ2+8ζ-1) (ζ-1)ζ2-124z(9ζ3+19ζ2-5ζ+1) (ζ-1)ζ3-1720z(251ζ4+646ζ3-264ζ2+106ζ-19)

In[14]:=

Define a function to extract the boundary locus |ζ|=1, i.e. ζ=eiθ

In[14]:=

BLocus[p_] := z /. Solve[ p == 0, z][[1]] /. zeta->Exp[I theta];

BLocus[ABCPs[[1]]]

-1+eiθ

In[15]:=

Compute and display the boundary locii for Adams-Bashforth, Adams-Moulton schemes

In[15]:=

ABBLs = Map[BLocus[#]&, ABCPs];

ABBLs // TableForm

-1+eiθ 2eiθ(-1+eiθ)-1+3eiθ 12e2iθ(-1+eiθ)-16eiθ+23e2iθ+5 24e3iθ(-1+eiθ)37eiθ-59e2iθ+55e3iθ-9 720e4iθ(-1+eiθ)-1274eiθ+2616e2iθ-2774e3iθ+1901e4iθ+251

In[16]:=

AMBLs = Map[BLocus[#]&, AMCPs];

AMBLs // TableForm

e-iθ(-1+eiθ) 2(-1+eiθ)1+eiθ 12eiθ(-1+eiθ)8eiθ+5e2iθ-1 24e2iθ(-1+eiθ)-5eiθ+19e2iθ+9e3iθ+1 720e3iθ(-1+eiθ)106eiθ-264e2iθ+646e3iθ+251e4iθ-19

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]

/home/student/courses/MATH761/AdamsBashforthBoundaryLocii.pdf

In[18]:=

AMBLplots = BLocusPlot[AMBLs,"Adams-Moulton boundary locii"];

Export["/home/student/courses/MATH761/AdamsMoultonBoundaryLocii.pdf",AMBLplots]

/home/student/courses/MATH761/AdamsMoultonBoundaryLocii.pdf

In[19]:=

Figure 1. Boundary locii for LMMs