Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- import pylab
- from math import pi, cos, sin
- n = 8
- x = np.linspace(-1,1,100)
- x_nodes = np.linspace(-1,1,8)
- x_nodes_ch = [cos(i*pi/n) for i in range(n+1)]
- def l_i(i,x,x_nodes):
- li=1
- for j in range(len(x_nodes)):
- if j != i:
- li=li*(x-x_nodes[j])/(x_nodes[i]-x_nodes[j])
- return li
- def L(x,x_nodes,y_nodes):
- Lx=0
- for i in range(len(x_nodes)):
- Lx += y_nodes[i]*l_i(i,x,x_nodes)
- return Lx
- def f(x):
- return 1/(1+25*x**2)
- y_nodes_ch = [f(k) for k in x_nodes_ch]
- y_nodes = [f(k) for k in x_nodes]
- def Ll(a):
- return L(a,x_nodes,y_nodes)
- def Ll_ch(a):
- return L(a,x_nodes_ch,y_nodes_ch)
- fig, ax = plt.subplots()
- ax.set_xlabel("$x$")
- ax.set_ylabel("$y$")
- ax.plot(x,f(x),label="f(x)")
- ax.plot(x,Ll(x))
- ax.grid()
- plt.show()
- fig, ax = plt.subplots()
- ax.set_xlabel("$x$")
- ax.set_ylabel("$y$")
- ax.plot(x,f(x),label="f(x)")
- ax.plot(x,Ll_ch(x))
- ax.grid()
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement