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 |
import os import matplotlib.pyplot as plt import numpy as np from scipy import interpolate os.chdir(r'D:\data\scripts\web1\ex26') os.getcwd() def runge(x): return 1/(1 + 25 * x**2) x = np.linspace(-1, 1, 11) y = runge(x) f = interpolate.interp1d(x, y, kind=3) xx = np.linspace(-1, 1, 100) fig, ax = plt.subplots(figsize=(8, 4)) ax.plot(xx, runge(xx), 'k', lw=1, label="Runge's function") ax.plot(x, y, 'ro', label='sample points') ax.plot(xx, f(xx), 'r--', lw=2, label='spline order 3') ax.legend() ax.set_ylim(0, 1.1) ax.set_xticks([-1, -0.5, 0, 0.5, 1]) ax.set_ylabel(r"$y$", fontsize=18) ax.set_xlabel(r"$x$", fontsize=18) fig.tight_layout() ax.set_title("Spline Interpolation") fig.tight_layout() plt.savefig("example26.png", dpi=100) plt.show() plt.close() <img class=" wp-image-131 aligncenter" src="https://gantovnik.com/bio-tips/wp-content/uploads/2019/01/example26.png" alt="example26" width="626" height="313" /> |
Recent Comments