1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import os import matplotlib.pyplot as plt import numpy as np from numpy import polynomial as P from scipy import linalg os.chdir(r'D:\data\scripts\web1\ex23') os.getcwd() x = np.array([1, 2, 3, 4]) y = np.array([1, 3, 5, 4]) deg = len(x) - 1 A = P.polynomial.polyvander(x, deg) c = linalg.solve(A, y) f1 = P.Polynomial(c) A = P.chebyshev.chebvander(x, deg) c = linalg.solve(A, y) f2 = P.Chebyshev(c) xx = np.linspace(x.min(), x.max(), 100) fig, ax = plt.subplots(1, 1, figsize=(8, 4)) ax.plot(xx, f1(xx), 'b', lw=2, label='Power basis interp.') ax.plot(xx, f2(xx), 'r--', lw=2, label='Chebyshev basis interp.') ax.scatter(x, y, label='data points') ax.legend(loc=4) ax.set_xticks(x) ax.set_ylabel(r"$y$", fontsize=18) ax.set_xlabel(r"$x$", fontsize=18) ax.set_title("Polynomial interpolation") fig.tight_layout() plt.savefig("example23.png", dpi=100) plt.show() plt.close() <img class=" wp-image-122 aligncenter" src="https://gantovnik.com/bio-tips/wp-content/uploads/2019/01/example23.png" alt="example23" width="586" height="293" /> |
Recent Comments