Need help coding this in Python
In Newton's form of the interpolation polynomial, we need to compute the coefficients. The coefficients are denoted as c0=f[0], c1=f[x1], ..., cn=f[xn]. In the table of divided differences, we proceed column by column, and the needed coefficients are in the uppermost diagonal. A simple 1D array, c, of size n + 1, can be used to store and compute these values. We just have to compute them from bottom to top to avoid losing values we have already computed. The following pseudocode does precisely this:
for j=0, 1, ..., n:
c[j] = f[j]
for A=1, ..., n:
for j=n, n-1, ..., A:
c[j] = (c[j] - c[j-1]) / (x[j] - x[j-A])
The evaluation of the interpolation polynomial in Newton's form can be then done with the Horner-like scheme seen in class:
p = c[n]
for j=n-1, n-2, ..., 0:
p = c[j] + (x - x[j]) * p
(a) Write computer codes to compute the coefficients c0, c1, ..., cn, and to evaluate the corresponding interpolation polynomial at an arbitrary point x. Test your codes and turn in a run of your test.
(b) Consider the function f(x) = e^x for x in [1, 1] and the nodes xi = 1 + j(2/10), j = 0, 1, ..., 10. Use your code(s) in (a) to evaluate p(x) at the points xi = -1 + j(2/100), j = 0, 1, ..., 100 and plot the error f(x) - p(x).