if __name__ == "__main__": N = 9 # number of points interval = [-5, 5] cheb_zeros = chebyshev(interval[0], interval[1], N) # retrieve the chebyshev zeros values = [f(x) for x in cheb_zeros] # calculate the function for the zeros print("The interpolation points are as follows:") print(cheb_zeros) print("Respectively, the function value at these points are:") print(values) P = pb1.newton_interpolation(values, cheb_zeros) X = cheb_zeros print("This gives the following interpolation polynomial:") pb1.print_polynomial(P, X) max_diff = 0 max_x = 0 poly_values = zeros(1001) x = linspace(interval[0], interval[1], 1001) for i in range(1001): x_coord = x[i] y_coord = pb1.get_value_of_polynomial(P, X, x_coord) fx = f(x_coord) if abs(fx - y_coord) > max_diff:
zeros(upper_bound+1 - lower_bound)] for N in N_values: # Loop as required by problem max_error = [0, 0, 0] # Used store the maximum error of each interpolation max_x = [0, 0, 0] # Used to store the abscicas at which the maximum error occurs # Generating the ordinates and absciccas interval = [-5, 5] spacing = 10 / N points = [interval[0] + spacing / 2 + spacing * m for m in range(N)] # creates the absiccas values = [f(x) for x in points] # creates the ordinates # Newton approximation: newton_poly = pb1.newton_interpolation(values, points) # This gives us our newton_polynomial # Newton with Chebyshev zeros: cheb_zeros = pb2.chebyshev(-5, 5, N) # Retrieves the Chebyshev zeros cheb_values = [f(x) for x in cheb_zeros] # Retrieves the ordinates for the Chebyshev zeros cheb_newton_poly = pb1.newton_interpolation(cheb_values, cheb_zeros) # Generates polynomial spline_coeff = pb3.SplineCoefficients(points, values) # Returns our spline interpolation coefficients x = linspace(interval[0], interval[1], 1001) for i in range(1001): # We loop through the 1001 points x_coord = x[i] # We evaluate all of the approximations at the iteration absica and store it in an array