import numpy as np import matplotlib.pyplot as plt from nest import nest from newtdd import newtdd x0 = np.array([0.,10.,20.,40.,50.,60.])+1960. y0 = [3015470894.,3694683794.,4447606236.,6171702993.,7021732148.,7887001292.] x0 = x0[2:4] y0 = y0[2:4] x = np.linspace(1960.,2020.,100) y = nest(newtdd(x0,y0),x,x0) print(nest(newtdd(x0,y0),1990.,x0))return \(5,309,654,615\) as the 1990 population linear estimate, compared to the actual \(5,327,803,110\), a difference of about 18 million.
1 (b) Follow the steps in (a), but use the three data points and construct a degree two interpolating polynomial. The resulting 1980 estimate is \(5,273,279,302\).
1 (c) Following the steps in (a) for all six data points results in an estimate of \(5,294,680,748\).
1 (d) Using the above code, but replacing 1990 with 2025 gives overly-optimistic estimates of 8,326,823,939, 8,736,046,201, and 8,388,285,131, repsectively. The linear projection comes the closest to the actual 2025 estimate.
def polyinterp(x,y,x0):
len = x.shape[0]
c=newtdd(x,y)
return nest(c,x0,x)
5 (b) The \(\tan x\) function is periodic with period \(\pi\), or in other words, \(\tan(x+\pi)=\tan x\). Therefore if we can compute \(\tan x\) on the domain \((-\frac{\pi}{2}, \frac{\pi}{2})\), all other \(x\) can be referred to this interval by subtracting integer multiples of \(\pi\).
Likewise, the identity \(\tan(\frac{\pi}{2}-x) = 1/\tan x\) shows that for \(x\) satisfying \(\frac{\pi}{4}< x< \frac{\pi}{2}\), \(\tan x = 1/\tan(\frac{\pi}{2}-x)\) where \(\frac{\pi}{2}-x\) lies in \([0,\frac{\pi}{4}]\). Furthermore, \(\tan x\) for \(x\) in \((-\frac{\pi}{2},0]\) is the negative of \(\tan |x|\).
Summarizing, given any real number, first subtract enough integer multiples of \(\pi\) to locate a corresponding \(x\) in \((-\frac{\pi}{2},\frac{\pi}{2})\). If \(x<0\), replace \(x\) by \(|x|\) and remember to attach a minus sign at the final step. If the positive \(x\) is less than \(\frac{\pi}{4}\), compute \(\tan\) from the fundamental domain, and if \(x>\frac{\pi}{4}\), set \(\tan x = 1/\tan(\frac{\pi}{2}-x)\).
5 (c) The following Matlab code moves the input \(x\) to the fundamental domain \([0,\pi/4]\) and carries out the evaluation of the interpolating polynomial.
import numpy as np
def tan1(x):
b=np.pi*np.array(list(range(0,4)))/12.
yb=np.tan(b)
c=newtdd(b,yb)
s=1
d=1
x=(x+np.pi/2) % np.pi - np.pi/2
if x < 0:
s=-1
x=np.abs(x)
if x > np.pi/4:
d=-1;
x=np.pi/2-x
return s*nest(c,x,b)**d
5 (d) The maximum error in the interval \([0,\pi/4]\) is approximately \(0.003\).