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,n)
- 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]
- fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(10, 5))
- axes[0].set_xlabel("$x$")
- axes[0].set_ylabel("$y$")
- axes[0].plot(x,f(x),label="f(x)")
- axes[0].plot(x,L(x,x_nodes,y_nodes))
- axes[0].grid()
- axes[1].set_xlabel("$x$")
- axes[1].set_ylabel("$y$")
- axes[1].plot(x,f(x),label="f(x)")
- axes[1].plot(x,L(x,x_nodes_ch,y_nodes_ch))
- axes[1].grid()
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement