MATH76108/31/2018

Lab02: 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]}

{f(h+x)-f(x)h,f(x-h)+f(h+x)-2f(x)h2,f(x-3h)-54f(x-2h)+783f(x-h)+783f(h+x)-54f(2h+x)+f(3h+x)-1460f(x)576h2}

In[2]:=

Series[qxx4,{h,0,4}]

f''(x)-3320h4f(6)(x)+O(h5)

In[3]:=

Note that centered formulas for approximating the second derivative of comparable accuracy may be obtained by using a smaller stencil.

In[3]:=

dc[f_[x_], p_, k_] := Module[{z,n,zn,ser,serc,fd},

ser = Expand[(Normal[2/h Series[ArcSinh[z/2], {z, 0, 2 p - 1}]])^k];

n = Exponent[ser,z]; zn = Coefficient[ser,z,n] z^n;

serc = ser - zn;

fd = serc /. Table[z^j -> Nest[d, f[x], j], {j, 1, (2 p - 1)k}];

Return[Together[fd]]];

qxx4=dc[f[x],2,2]

-f(x-2h)+16f(x-h)+16f(h+x)-f(2h+x)-30f(x)12h2

In[4]:=

Series[qxx4,{h,0,4}]

f''(x)-190h4f(6)(x)+O(h5)

In[5]:=

2Eigenvalues of matrix from semi-discretization

From the PDE qt=qxx an ODE system

𝐐'(t)=1h2𝐀𝐐(t) (1)

is obtained. The eigenvalues of 𝐀 are necessary for stability analysis of various time integration schemes to solve (1). Since 1h2𝐀x2, the eigenvectors of 𝐀 are discrete approximations of the eigenfunctions of x2.

2.1Homogeneous Dirichlet boundary conditions

Separation of variables applied to qt=qxx, q(t,x)=T(t)X(x) gives

T'T=X''X=-λ2,λ,X(0)=X(1)=0

with eigenfunctions Xk(x)=sin(kπx), λk=kπ.

In[31]:=

op = -X”[x];

lftBC = DirichletCondition[X[x]==0,x==0];

rgtBC = DirichletCondition[X[x]==0,x==1];

DEigensystem[{op,lftBC,rgtBC},X[x],{x,0,1},7]

( π2 4π2 9π2 16π2 25π2 36π2 49π2 sin() sin(2) sin(3) sin(4) sin(5πx) sin(6πx) sin(7πx) )

In[48]:=

qxx2

f(x-h)+f(h+x)-2f(x)h2

In[49]:=

xk[x_] = Sin[k Pi x];

Axki = qxx2 /. {f->xk, x-> i h}

-2sin(πhik)+sin(πk(hi-h))+sin(πk(hi+h))h2

In[55]:=

lambda2[h_]=FullSimplify[Axki/xk[i h]]

2(cos(πhk)-1)h2

In[56]:=

Limit[lambda2[h],h->0]

π2(-k2)

In[57]:=

2.2Semi-homogeneous Dirichlet boundary conditions

Separation of variables applied to qt=qxx, q(t,x)=T(t)X(x) gives

T'T=X''X=-λ2,λ,X(0)=0,X(1)=1

with eigenfunctions Xk(x)=sin[(4k+1)πx/2], λk=(4k+1)π/2.

In[57]:=

qxx2

f(x-h)+f(h+x)-2f(x)h2

In[58]:=

xk[x_] = Sin[(4k+1) Pi x/2];

Axki = qxx2 /. {f->xk, x-> i h}

-2sin(12πhi(4k+1))+sin(12π(4k+1)(hi-h))+sin(12π(4k+1)(hi+h))h2

In[59]:=

lambda2[h_]=FullSimplify[Axki/xk[i h]]

-4sin2(πhk+πh4)h2

In[60]:=

Limit[lambda2[h],h->0]

-14(4πk+π)2

In[61]:=