def plot(self, res=1001): """ Plots the linear interpolation of the coordinate lists provided. Resolution is optional and defaults to 1001. """ LP.graph(self.xp, self.yp, resolution=res)
def test_p_L(self, f=np.sin, x=[0, np.pi], n=11): """ Tests the polynomial interpolation function on the given x values, which should return exactly the given y values. The function uses the values on sin(x) default """ p_L = LagrangeInterpolation(f, x, n) LP.test_p_l(p_L.xp, p_L.yp)
def plot(self, res = 1001): """ Plots the linear interpolation of the coordinate lists provided. Resolution is optional and defaults to 1001. """ LP.graph(self.xp, self.yp, resolution = res)
def test_p_L(self, f = np.sin, x = [0, np.pi], n = 11): """ Tests the polynomial interpolation function on the given x values, which should return exactly the given y values. The function uses the values on sin(x) default """ p_L = LagrangeInterpolation(f, x, n) LP.test_p_l(p_L.xp, p_L.yp)
def test_p_L(self, xp=np.linspace(0, np.pi, 5), yp=np.array([np.sin(y) for y in np.linspace(0, np.pi, 5)])): """ Tests the polynomial interpolation function on the given x values, which should return exactly the given y values. The function uses the values on sin(x) default """ LP.test_p_l(xp, yp)
def multigrapher(): legender1 = ['F2', '2', 'F4', '4', 'F6', '6', 'F10', '10'] n_list = [2, 4, 6, 10] figure(1) p2.graph2(np.abs, n_list, -2, 2, legender1) legender2 = ['F13', '13', 'F20', '20'] n_list2 = [13, 20] figure(2) p2.graph2(np.abs, n_list2, -2, 2, legender2)
def __call__(self, x): """ Evaluates the interpolation model at point x """ return LP.p_L(x, self.xp, self.yp)
import Lagrange_poly2 as lp import numpy as np import matplotlib.pyplot as plt nlist=(2,4,6,10) plt.figure() for n in nlist: lp.graph(np.abs, n, -2, 2, resolution=1001) plt.legend(["n=%d" %n for n in nlist ]) #plt.savefig("filename.pdf") if I wanted to save the figure plt.figure() nlist=(13,20) for n in nlist: lp.graph(np.abs, n, -2, 2, resolution=1001) plt.legend(["n=%d" %n for n in nlist ]) plt.show()
#!/usr/bin/python """ File: Lagrange_poly2b.py Copyright (c) 2016 Michael Seaman License: MIT Implements the past Lagrange poly modules and tests them out with f(x) = abs(x) """ import Lagrange_poly2 as poly2 import math poly2.graph(math.fabs, 2, -2, 2) poly2.graph(math.fabs, 4, -2, 2) poly2.graph(math.fabs, 6, -2, 2) poly2.graph(math.fabs, 10, -2, 2) poly2.graph(math.fabs, 13, -2, 2) poly2.graph(math.fabs, 20, -2, 2)
def plot(self): LP2.graph(np.sin, 5, 0, np.pi)
""" Exercise 5.25: Investigate the behaviour of Langrange's interpolating polynomials Author: Weiyun Lu """ import Lagrange_poly2 from scitools.std import hold, figure figure() for n in [2,4,6,10]: Lagrange_poly2.graph(abs, n, -2, 2) hold('on') hold('off') figure() for n in [13,20]: Lagrange_poly2.graph(abs, n, -2, 2) hold('on') hold('off')
def graph2(): n = [13, 20] p1.graph2(np.abs, n, -2, 2) plt.title('f(x) = |x| and Interpolation Points') plt.xlim(-2.0, 2.0) plt.ylim(-4.5, 5.5)
def graph1(): n = [2, 4, 6, 10] p1.graph2(np.abs, n, -2, 2) plt.title('f(x) = |x| and Interpolation Points') plt.xlim(-2.0, 2.0) plt.ylim(0.0, 2.1)
def problem_5_25(): L2.graph(absolute, 2, -2, 2, [-3,3,0,1]) L2.graph(absolute, 4, -2, 2, [-3,3,0,1]) L2.graph(absolute, 6, -2, 2, [-3,3,0,1]) L2.graph(absolute, 10, -2, 2, [-3,3,0,1]) L2.graph(absolute, 13, -2, 2, [-3,3,0,1]) L2.graph(absolute, 20, -2, 2, [-3,3,0,1])
def test_p_L(self, xp = np.linspace(0,np.pi, 5), yp = np.array([np.sin(y) for y in np.linspace(0,np.pi, 5)]) ): """ Tests the polynomial interpolation function on the given x values, which should return exactly the given y values. The function uses the values on sin(x) default """ LP.test_p_l(xp, yp)
def problem_5_25(): L2.graph(absolute, 2, -2, 2, [-3, 3, 0, 1]) L2.graph(absolute, 4, -2, 2, [-3, 3, 0, 1]) L2.graph(absolute, 6, -2, 2, [-3, 3, 0, 1]) L2.graph(absolute, 10, -2, 2, [-3, 3, 0, 1]) L2.graph(absolute, 13, -2, 2, [-3, 3, 0, 1]) L2.graph(absolute, 20, -2, 2, [-3, 3, 0, 1])