"""
MATH3476 Example 1.4
Newton divided difference for u=sqrt(x)
"""
import numpy as np

n = 4
x = np.linspace(2,2.4,n+1,dtype=np.double)   # n+1 eqaully-spaced nodes
u = np.sqrt(x)                                        # function values

D = u.copy()                         # array for the divided differences

for i in range(1,n+1):                   # start from 1 since D(0)=uu(0)
    for j in range(n,i-1,-1):                   # note the reverse order
        D[j]=(D[j] - D[j-1])/(x[j] - x[j-i])

print(D)
