|
| 1 | +import numpy as np |
| 2 | +import matplotlib.pyplot as plt |
| 3 | + |
| 4 | +from cubic_splines import calculate_cubic_value, create_cubic_function, calculate_cubic_value_not_a_knot |
| 5 | +from quadratic_splines import create_quadratic_function, calculate_quadratic_value, calculate_quadratic_value_not_a_knot |
| 6 | +from function import fun |
| 7 | +from errors import print_error |
| 8 | +from points import get_points |
| 9 | + |
| 10 | + |
| 11 | +if __name__ == '__main__': |
| 12 | + start = -2.*np.pi |
| 13 | + end = 2.*np.pi |
| 14 | + step = 0.05 |
| 15 | + N = 8 |
| 16 | + function = fun |
| 17 | + |
| 18 | + points = get_points(N-1, start, end, function) |
| 19 | + |
| 20 | + x_points, y_points = zip(*points) |
| 21 | + |
| 22 | + x = np.arange(start, end, step) |
| 23 | + y = [function(x) for x in x] |
| 24 | + x_len = len(x) |
| 25 | + |
| 26 | + quadratic_spline_natural = create_quadratic_function(x_points, y_points, calculate_quadratic_value) |
| 27 | + quadratic_spline_notaknot = create_quadratic_function(x_points, y_points, calculate_quadratic_value_not_a_knot) |
| 28 | + cubic_spline_natural = create_cubic_function(x_points, y_points, calculate_cubic_value) |
| 29 | + cubic_spline_not_a_knot = create_cubic_function(x_points, y_points, calculate_cubic_value_not_a_knot) |
| 30 | + |
| 31 | + quadratic_spline_natural_values = [quadratic_spline_natural(x) for x in x] |
| 32 | + quadratic_spline_notaknot_values = [quadratic_spline_notaknot(x) for x in x] |
| 33 | + cubic_spline_natural_values = [cubic_spline_natural(x) for x in x] |
| 34 | + cubic_spline_notaknot_values = [cubic_spline_not_a_knot(x) for x in x] |
| 35 | + |
| 36 | + print('label,N,mean_sqaure_error,max_error') |
| 37 | + print_error(N, x_len, y, quadratic_spline_natural_values, 'quadratic_natural') |
| 38 | + print_error(N, x_len, y, quadratic_spline_notaknot_values, 'quadratic_notaknot') |
| 39 | + print_error(N, x_len, y, cubic_spline_natural_values, 'cubic_natural') |
| 40 | + print_error(N, x_len, y, cubic_spline_notaknot_values, 'cubic_notaknot') |
| 41 | + |
| 42 | + plt.plot(x, function(x), 'b', linewidth=2.5, label="function") |
| 43 | + plt.plot(x, cubic_spline_natural_values, 'r--', linewidth=2.5, label='cubic natural') |
| 44 | + plt.plot(x, cubic_spline_notaknot_values, 'r--', color='#880000', linewidth=2.5, label='cubic notaknot') |
| 45 | + plt.plot(x, quadratic_spline_natural_values, 'g--', linewidth=2.5, label='quad natural') |
| 46 | + plt.plot(x, quadratic_spline_notaknot_values, 'g--', color='#808000', linewidth=2.5, label='quad notaknot') |
| 47 | + plt.plot(x_points, y_points, 'yo') |
| 48 | + plt.legend(loc='upper left') |
| 49 | + plt.show() |
0 commit comments