MATH661 HW08 - Linear operator approximation

Posted: 10/25/23

Due: 11/06/23, 11:59PM

The basic idea in linear operator approximation is to apply the exact operator to an approximation of the input. These exercises explore and reinforce this concept.

1Track 1

  1. Use Taylor series expansions to verify the approximations

    f'(t)112h[-f(t+2h)+8f(t+h)-8f(t-h)+f(t-2h)], (1)
    f'(t)12h[-3f(t)+4f(t+h)-f(t+2h)]. (2)

    Determine the error term. Construct the polynomial approximant pn(t)f(t) whose derivative leads to the above formula. Conduct a convergence study as h0 for f(t){sin(πt/4),e10t,e-10t} at t0=1, and compare the observed order of convergence with the theoretical estimate.

    Solution. For hand computation, organize Taylor series expansions into a table with c the coefficients from the finite difference formula. Observe that Taylor series terms of even powers in h cancel out, and those of odd powers from -kh simply double those from kh parts of the expansions. The power of h multiplying f(k) is hk-1. Observing and exploiting such symmetries reduces the number of calculations considerably.

    k 1 3 5 term c 2c f'(t) 2cf'(t) f'''(t) 2cf'''(t) f(5)(t) 2cf(5)(t) f(t+2h) -112 -16 2 -13 8 -43 32 -163 f(t+h) 812 43 43 1 43 43 Sum 1 0 -4 Sum/k! 1 0 -130

    Deduce leading-order error

    ε1=-h4f(5)(ξ)30.

    This can be verified in Mathematica.

    Set origin at t=0. Formula (1) contains data 𝒟={(kh,fk),k=-2,-1,1,2}{0,f0'} with fk=f(kh). for a total of 5 conditions that can be satisfied by polynomial p(t) of degree n4.

    p(-2h)=f-2,p(-h)=f-1,p(h)=f1,p(2h)=f2,

    p'(0)=f0'=112h[-f2+8f1-8f-1+f-2].

    This exercise highlights use of the Lagrange form of a Hermite interpolating polynomial with differing types of information at each node. The Lagrange basis functions

    ai(t)=ai(αh)=[1-2(α-i)hi'(ih)]i2(αh),b0(t)=b0(αh)=αh02(αh),

    i(t)=i(αh)=k=-2,k0,i2αh-khih-kh=k=-2,k0,i2α-ki-k,i=-2,-1,1,2,

    0(t)=0(αh)=k=-2,k02αh-kh(-kh)=14(α2-1)(α2-2)

    satisfy the desired conditions leading to the polynomial

    p(t)=p(αh)=f0'b0(αh)+i=-2,i02fiai(αh).

    A similar analysis can be applied to the second formula giving the error term

    ε2=-h2f(3)(ξ)3.

    Steps for the convergence study:

    Define the finite difference derivative approximations

    Define the specified study functions

    Define a function to construct the convergence plot by: evaluating the error

    uk=lg(ek)=lg|Dh/2f-Dhf|p-qlghk

    for hk=h02-k, k=1,,N, extracting the order of convergence p by least squares (linear regression)

    𝑨=[ 1 lgh ]=[ 1 1 1 2 1 N ];min(p,q)||𝑨[ p q ]-𝒖||[ p q ]=𝑨\𝒖,

    and plotting the observed behavior.

    function conv(h0,t,f,D,N,fname,figname)
      df=zeros(N+1,1); u=zeros(N,1); h=h0; lgh=zeros(N+1,1)
      for k=1:N+1
        h=h/2; lgh[k]=log10(h); df[k]=D(t,f,h);
      end
      A=ones(N,2); x=-lgh[1:N]; A[:,2]=x;
      u[1:N]=log10.(abs.(df[2:N+1]-df[1:N]));
      q = floor((A\u)[2]*1000)/1000;
      plot(x,u,"o"); grid("on"); title(figname*" q="*string(q))
      xlabel(L"$-\lg(h)$"); ylabel(L"\lg(e_k)")
      savefig(fname)
      return q
    end;

    Figure 1. Convergence study results. Top row for f1(t)=sin(πt/4): (1a) Results exhibit loss of precision as h decreases, and q-1 is affected by error increase for small h; (1b) The first few step sizes after h0=1/8 are not yet in the asymptotic regime, hence the observed order of convergence is q=3.81, less than the theoretical 𝒪(h4) order. (1c) The asymptotic regime is reached at h0=1/32, and the floating point precision is sufficient up to h=h0/24 to obtain q=3.94, close to the theoretical order. Second row: (2a) Again for h0=1/32 to h=h0/28 the observed order for f2(t)=exp(10t) is q=3.9, close to the theoretical prediction. The amplification of ||f2||e102.2×104 allows more accurate significant digits in floating point, but the overall precision is small (2b) The same h range for f3 with ||f3||4×10-5 not only achieves predicted order of accuracy, q=3.97, but also much better accuracy with machine precision ϵ10-16 achieved when h=1/32/2810-4. (2c) Second finite difference formula results for f1(),f2(),f3(). The observed order of conergence is close to the theoretical 𝒪(h2) in all cases, albeit with different overall errors.

    figdir=homedir()*"/courses/MATH661/homework/H08/";
    clf(); h0=1; t0=1; conv(h0,t0,f1,D1,20,figdir*"H08Fig1a.png","Fig1a");
    clf(); h0=1/8; conv(h0,t0,f1,D1,7,figdir*"H08Fig1b.png","Fig1b");
    clf(); h0=1/32; conv(h0,t0,f1,D1,4,figdir*"H08Fig1c.png","Fig1c");
    clf(); h0=1/32; conv(h0,t0,f2,D1,8,figdir*"H08Fig1d.png","Fig1d");
    clf(); h0=1/32; conv(h0,t0,f3,D1,8,figdir*"H08Fig1e.png","Fig1e");
    clf(); h0=1/32; conv(h0,t0,f1,D2,8,figdir*"H08Fig1f.png","Fig1f");
    h0=1/32; conv(h0,t0,f2,D2,8,figdir*"H08Fig1f.png","Fig1f");
    h0=1/32; conv(h0,t0,f3,D2,8,figdir*"H08Fig1f.png","Fig1f");
  2. As above for

    f''(t)112h2[-f(t+2h)+16f(t+h)-30f(t)+16f(t-h)-f(t-2h)],
    f'''(t)1h3[f(t+3h)-3f(t+2h)+3f(t+h)-f(t)].

    Solution. Symbolic computation in Mathematica readily gives 𝒪(h4), 𝒪(h) behavior respectively.

    Define the finite difference derivative approximations

    Figure 2. Convergence study results. Left: f'' formula results for f1(),f2(),f3(), fourth order achieved for certain h ranges. Right: f''' formula results for f1(),f2(),f3(), with q1

  3. Construct a recursive function RecInt(a,b,err,f,Q) that has arguments scalars a,b,err and functions f,Q and approximates

    I(f)=abf(t)dt

    through repeated application of quadrature rule Q(f,a,b) according to the algorithm

    Algorithm

    Recursive quadrature

    RecInt(a,b,err,f,Q)

    c=a+(b-a)/2

    Qab=Q(f,a,b);Qac=Q(f,a,c);Qcb=Q(f,c,b)

    e=|Qac+Qcb-Qab|/|Qac+Qcb|

    if e<err

    return Qac+Qcb

    else

    return RecInt(a,c,err,f,Q)+RecInt(c,b,err,f,Q)

    Test the recursive integration procedure with trapezoid, Simpson, and Gauss-Legendre rules of orders 2,3 on the integral

    -11cos(1t).

    For each case, present plots of the integrand and the evaluation points used in the recursive quadrature algorithm. Construct convergence plots by executing the algorithm for various error thresholds εk and recording the number of evaluation points nk. Plot (lognk,logεk) and comment on whether the observed order of convergence is that predicted by theoretical quadrature error estimates.

    Solution. Define:

    Recursive quadrature procedure

    function RecInt(a,b,eps,f,Q,level)
      global nf,nmax,lmax
      c=a+(b-a)/2; Qab=Q(a,b,f); Qac=Q(a,c,f); Qcb=Q(c,b,f)
      new=Qac+Qcb; old=Qab;
      if nf > nmax/2 return new end
      if level > lmax return new end
      err=abs((new-old)/new)
      if err<eps
        return new
      else
        return RecInt(a,c,eps,f,Q,level+1)+RecInt(c,b,eps,f,Q,level+1)
      end
    end;

    Quadrature rules:

    Trapezoid

    abf(t)dt=b-a2[f(a)+f(b)].
    function trapezoid(a,b,f)
      return 0.5*(b-a)*(f(a)+f(b))
    end;

    Simpson

    abf(t)dt=b-a6[f(a)+4f(a+b2)+f(b)].
    function simpson(a,b,f)
      return (b-a)/6 * (f(a)+4*f(0.5*(a+b))+f(b))
    end;

    Gauss-Legendre 2

    -11f(t)dt=f(-3)+f(3).
    function gl2(a,b,f)
      s=(b-a)/2; z(t)=s*(t+1)+a; s3=sqrt(1/3);
      return s*(f(z(-s3))+f(z(s3)))
    end;

    Gauss-Legendre 3

    -11f(t)dt=59f(-35)+89f(0)+59f(35)
    function gl3(a,b,f)
      s=(b-a)/2; z(t)=s*(t+1)+a; s35=sqrt(3/5); w1=5/9; w0=8/9
      return s*(w1*f(z(-s35)) + w0*f(z(0)) + w1*f(z(s35)))
    end;

    Define the integrand. Each time the integrand is called record the evaluation point and increment a counter of function evaluations. The integrand is singular at t=0, but over an interval of measure zero in the limit. Hence setting the integrand value to zero at t=0 does not affect the integral value. Also define a simpler integrand g for overall testing.

    function f(t)
      global nf,tvals,fvals
      if abs(t)<1.0e-6
        fval=0.
      else
        fval=cos(1/t)
      end
      nf = nf+1; tvals[nf]=t; fvals[nf]=fval
      return fval
    end;
    function g(t)
      global nf,tvals,fvals
      fval = cos(t)
      nf = nf+1; tvals[nf]=t; fvals[nf]=fval
      return fval
    end;

    Initialize function evaluation counter, values, call the routine for simple integrand

    0π/2cos(t)dt=1

    Figure 3. For integrand g(t)=cos(t) function evaluation points are close to an equidistant interval partition. Three accurate digits (ε<10-3) are attained with nf=186,27,18,9 function evaluations, highlighting the effectiveness of high-order quadrature.

    Initialize function evaluation counter, values, call the routine for singular integrand

    1-1cos(1/t)dt=2-π+01sinttdt-0.1688

    Figure 4. For integrand f(t)=cos(1/t) function evaluation points are clustered in regions of rapid variation of the function. Within a limit on the number of function evaluation nf5×105, only the GL3 quadrature gives a value of -0.16 with two accurate digits. This example highlights both recursive quadrature and the need for specialized numerical quadrature procedures for highly oscillatory or singular integrands.

    global nf,nmax,lmax
    nf=0; nmax=100000; lmax=8; tvals=zeros(nmax,1); fvals=zeros(nmax,1);
    Qg=RecInt(-1,1,10.0^(-1),f,trapezoid,0); [nf Qg]

    [ 690.0 -0.14465818164911176 ] (7)

    clf(); plot(tvals[1:nf],fvals[1:nf],".");
    xlabel(L"$t$"); ylabel(L"$f(t)$"); grid("on");
    title("RecInt eval pts, nf="*string(nf));
    figdir=homedir()*"/courses/MATH661/homework/H08/";
    savefig(figdir*"Fig3fT.png");
    nf=0; nmax=1000000; lmax=32; tvals=zeros(nmax,1); fvals=zeros(nmax,1);
    Qg=RecInt(-1,1,10.0^(-2),f,trapezoid,0); [nf Qg]

    [ 500058.0 -0.1574321351531846 ] (8)

    nf=0; nmax=1000000; lmax=32; tvals=zeros(nmax,1); fvals=zeros(nmax,1);
    Qg=RecInt(-1,1,10.0^(-2),f,simpson,0); [nf Qg]

    [ 500067.0 -0.37476168010680994 ] (9)

    nf=0; nmax=1000000; lmax=32; tvals=zeros(nmax,1); fvals=zeros(nmax,1);
    Qg=RecInt(-1,1,10.0^(-2),f,gl2,0); [nf Qg]

    [ 500046.0 -0.4505206896512667 ] (10)

    nf=0; nmax=1000000; lmax=32; tvals=zeros(nmax,1); fvals=zeros(nmax,1);
    Qg=RecInt(-1,1,10.0^(-2),f,gl3,0); [nf Qg]

    [ 500103.0 -0.16578576294469366 ] (11)

2Track 2

  1. Use the finite difference expressions of the derivative

    ddt=1h(Δ-12Δ2+)=1h(+122+)=1h(δ-δ324+)

    to obtain the approximations.

    f'(t)112h[-f(t+2h)+8f(t+h)-8f(t-h)+f(t-2h)],
    f'(t)12h[-3f(t)+4f(t+h)-f(t+2h)].

    Conduct a convergence study as h0 for f(t){sin(πt/4),e10t,e-10t} at t0=1, and compare the observed order of convergence with theoretical estimates. How do the three functions differ, and what effect does this have on derivative approximation?

    Solution. See Track 1 above for the standard convergence study. The Track 2 aspect of this exercise is to highlight how research problems arise.

    The results

    ddt=1hln(I+Δ)=-1hln(I-)=2harcsinh(δ2),

    Δ=E-I,=I-E-1,δ=E1/2-E-1/2

    indicate multiple ways to express the differentiation operator D=d/dt in terms of the translation operator E. One consideration in constructing finite difference approximations of a derivative is the number of function sample points, known as the stencil size. For example, a fourth order accurate formula is obtained with a stencil size of 5 from hD=ln(I+Δ), using sample points t+kh, k=0,1,,4. The formula

    ddtf(t)112h(-E2+8E-8E-1+E2)f(t),

    is of fourth order accuracy, but has a stencil size of 4. This suggests exploring what additional expressions of D in terms of functions of E,Δ,,δ might be found. This is also the typical research scenario: when a result does not conform to known theory, seek extensions of the theory.

    Carry out calculations to obtain the requested approximation of the derivative in terms of finite differences

    D=ddt=(E-E-1)12h[8I-(E+E-1)]=12[1h(Δ-16Δ2)+1h(+162)].

    This suggests an average of one-sided finite differences, truncated to first two terms,

    D=12(F+B),F=1h(Δ-16Δ2+),B=1h(+162+).

    Take an additional research step and ask how the above may be extended. Can a function be found from the first few terms of its power series? Consider the following Mathematica result relevant for hD=ln(I-).

    The generating function has been found using only 4 terms. Extra credit (4 pts). Can you find generating functions for F,B?

  2. As above for

    f''(t)112h2[-f(t+2h)+16f(t+h)-30f(t)+16f(t-h)-f(t-2h)],
    f'''(t)1h3[f(t+3h)-3f(t+2h)+3f(t+h)-f(t)].

    Use the series products

    d2dt2=ddtddt=1h2(Δ-12Δ2+)(Δ-12Δ2+).

    Solution. As above.

  3. Romberg integration is a combination of trapezoid quadrature over decreasing subintervals and Aitken extrapolation. Implement Romberg integration and test on

    01etcos(πt)dt.

    Present a convergence sutdy. What is the observed order of convergence?

    Solution.