Advertisement
Serafim_

Полином Лагранжа 2

Mar 14th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. import scipy.interpolate
  5. import scipy.integrate
  6.  
  7. x = np.array([0, 1, 2, 3, 4, 5], dtype = float)
  8. y = np.array([0.1, 0.5, 1, 6, 2, - 2.5], dtype = float)
  9.  
  10. def lagranz(x, y, t):
  11.     z = 0
  12.     for j in range(len(y)):
  13.         p1 = 1; p2 = 1
  14.         for i in range (len(x)):
  15.             if i == j:
  16.                 p1 = p1*1; p2 = p2*1  
  17.             else:
  18.                 p1 = p1*(t - x[i])
  19.                 p2 = p2*(x[j] - x[i])
  20.         z = z + y[j]*p1/p2
  21.     return z
  22.  
  23. def integr(a, b, dx):
  24.     points = np.arange(a, b, dx)
  25.     summ = 0
  26.     for x_cur in points:
  27.         summ += lagranz(x,y, x_cur)*dx
  28.     return summ
  29.  
  30.  
  31. xnew = np.linspace(np.min(x),np.max(x),100)
  32. ynew = [lagranz(x,y,i) for i in xnew]
  33. plt.plot(xnew,ynew, label='lagrange')
  34. cubic_spline = scipy.interpolate.interp1d(x, y, kind='cubic')
  35. ynew = cubic_spline(xnew)
  36. plt.plot(xnew,ynew, label='cubic')
  37. akima = scipy.interpolate.Akima1DInterpolator(x, y)
  38. ynew = akima(xnew)
  39. plt.plot(xnew,ynew, label='akima')
  40. plt.grid(True)
  41. plt.legend()
  42. plt.show()
  43.  
  44.  
  45. print integr(np.min(x), np.max(x), 0.1)
  46. print integr(-2, 10, 0.01)
  47.  
  48.  
  49.  
  50. akima_1_2_integral_value,akima_1_2_integral_error = scipy.integrate.quad(akima, 1, 2)
  51. print 'Value', akima_1_2_integral_value
  52. print 'Error', akima_1_2_integral_error
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement