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: max_diff = abs(fx - y_coord) max_x = x_coord poly_values[i] = y_coord print("The maximum error found on the test points was:", max_diff) # max difference print("This maximum error was found at the x value:", max_x) print("Plotting the graph...") figure(1) plot(x, poly_values, 'b-', x, f(x), 'g-', cheb_zeros, values, 'or') xlabel("Abscissas", fontsize=20)
# 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 y = [pb1.get_value_of_polynomial(newton_poly, points, x_coord), pb1.get_value_of_polynomial(cheb_newton_poly, cheb_zeros, x_coord), pb3.evaluate_spline(spline_coeff, points, x_coord)] for j in range(len(y)): # We check if the current error of each approximation is bigger than the last error y_coord = y[j] fx = f(x_coord) if abs(fx - y_coord) > max_error[j]: max_error[j] = abs(fx - y_coord) # If yes, we record the new biggest error max_x[j] = x_coord # We also record the x value at which the biggest error occurred. for k in range(len(max_errors)): max_errors[k][N - 3] = max_error[k] # For personal observation, I stored each of the errors in a separate file for easier inspection.