Extra exercise

I also tried to reproduce the figure 2.4(d) here.


In [1]:
import numpy
from matplotlib import pyplot
% matplotlib inline

In [2]:
import os, sys
sys.path.append(os.path.split(os.path.split(os.getcwd())[0])[0])

In [3]:
import utils.elems.one_d as elem

In [4]:
Q = numpy.arange(2, 16)
C = numpy.zeros((3, 14), dtype=numpy.float64)

In [5]:
for i, Qi in enumerate(Q):
    E = []
    E.append(elem.MomentElem([-1, 1], Qi))
    E.append(elem.LagrangeElem([-1, 1], Qi))
    E.append(elem.PureLegendreElem([-1, 1], Qi))
    
    for j in range(3):
        C[j, i] = numpy.linalg.norm(E[j].M, 2) * \
        numpy.linalg.norm(numpy.linalg.inv(E[j].M), 2)

In [6]:
pyplot.figure(figsize=(6, 8))
pyplot.semilogy(Q, C[0, :], 'k.-', lw=1.5, label='Mement expansion')
pyplot.semilogy(Q, C[1, :], 'kx-', lw=1.5, label='Euqally-spaced Lagrange expansion')
pyplot.semilogy(Q, C[2, :], 'k^-', lw=1.5, label='Pure Legendre expansion')
pyplot.xlim(1, 15)
pyplot.legend(loc=0);