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 33 34 35 36 37 38 39 40 41 42 |
import os import matplotlib.pyplot as plt import numpy as np from numpy import polynomial as P os.chdir(r'D:\data\scripts\web1\ex25') os.getcwd() # In the mathematical field of numerical analysis, Runge's phenomenon # is a problem of oscillation at the edges of an interval that occurs # when using polynomial interpolation with polynomials of high degree # over a set of equispaced interpolation points. def runge(x): return 1/(1 + 25 * x**2) def runge_interpolate(n): x = np.linspace(-1, 1, n+1) p = P.Polynomial.fit(x, runge(x), deg=n) return x, p xx = np.linspace(-1, 1, 250) fig, ax = plt.subplots(1, 1, figsize=(8, 4)) ax.plot(xx, runge(xx), 'k', lw=2, label="Runge's function") n = 13 x, p = runge_interpolate(n) ax.plot(x, runge(x), 'ro') ax.plot(xx, p(xx), 'r', label='interp. order %d' % n) n = 14 x, p = runge_interpolate(n) ax.plot(x, runge(x), 'go') ax.plot(xx, p(xx), 'g', label='interp. order %d' % n) ax.legend(loc=8) ax.set_xlim(-1.1, 1.1) ax.set_ylim(-1, 2) ax.set_xticks([-1, -0.5, 0, 0.5, 1]) ax.set_ylabel(r"$y$", fontsize=18) ax.set_xlabel(r"$x$", fontsize=18) ax.set_title("Runge problem") fig.tight_layout() plt.savefig("example25.png", dpi=100) plt.show() plt.close() <img class=" wp-image-128 aligncenter" src="https://gantovnik.com/bio-tips/wp-content/uploads/2019/01/example25.png" alt="example25" width="604" height="302" /> |
Recent Comments