Advertisement
lutaco

Untitled

Jun 15th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import pylab
  4. from math import pi, cos, sin
  5.  
  6. n = 8
  7. x = np.linspace(-1,1,100)
  8. x_nodes = np.linspace(-1,1,n)
  9. x_nodes_ch = [cos(i*pi/n) for i in range(n+1)]
  10.  
  11. def l_i(i,x,x_nodes):
  12. li=1
  13. for j in range(len(x_nodes)):
  14. if j != i:
  15. li=li*(x-x_nodes[j])/(x_nodes[i]-x_nodes[j])
  16. return li
  17.  
  18. def L(x,x_nodes,y_nodes):
  19. Lx=0
  20. for i in range(len(x_nodes)):
  21. Lx += y_nodes[i]*l_i(i,x,x_nodes)
  22. return Lx
  23.  
  24.  
  25. def f(x):
  26. return 1/(1+25*x**2)
  27.  
  28. y_nodes_ch = [f(k) for k in x_nodes_ch]
  29. y_nodes = [f(k) for k in x_nodes]
  30.  
  31.  
  32. fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(10, 5))
  33. axes[0].set_xlabel("$x$")
  34. axes[0].set_ylabel("$y$")
  35. axes[0].plot(x,f(x),label="f(x)")
  36. axes[0].plot(x,L(x,x_nodes, y_nodes))
  37. axes[0].plot(x_nodes, y_nodes, 'go', c='pink')
  38. axes[0].grid()
  39.  
  40. axes[1].set_xlabel("$x$")
  41. axes[1].set_ylabel("$y$")
  42. axes[1].plot(x,f(x),label="f(x)")
  43. axes[1].plot(x,L(x,x_nodes_ch, y_nodes_ch))
  44. axes[1].plot(x_nodes_ch, y_nodes_ch, 'go', c='r')
  45. axes[1].grid()
  46. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement