Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- import scipy.interpolate
- import scipy.integrate
- x = np.array([0, 1, 2, 3, 4, 5], dtype = float)
- y = np.array([0.1, 0.5, 1, 6, 2, - 2.5], dtype = float)
- def lagranz(x, y, t):
- z = 0
- for j in range(len(y)):
- p1 = 1; p2 = 1
- for i in range (len(x)):
- if i == j:
- p1 = p1*1; p2 = p2*1
- else:
- p1 = p1*(t - x[i])
- p2 = p2*(x[j] - x[i])
- z = z + y[j]*p1/p2
- return z
- def integr(a, b, dx):
- points = np.arange(a, b, dx)
- summ = 0
- for x_cur in points:
- summ += lagranz(x,y, x_cur)*dx
- return summ
- xnew = np.linspace(np.min(x),np.max(x),100)
- ynew = [lagranz(x,y,i) for i in xnew]
- plt.plot(xnew,ynew, label='lagrange')
- cubic_spline = scipy.interpolate.interp1d(x, y, kind='cubic')
- ynew = cubic_spline(xnew)
- plt.plot(xnew,ynew, label='cubic')
- akima = scipy.interpolate.Akima1DInterpolator(x, y)
- ynew = akima(xnew)
- plt.plot(xnew,ynew, label='akima')
- plt.grid(True)
- plt.legend()
- plt.show()
- print integr(np.min(x), np.max(x), 0.1)
- print integr(-2, 10, 0.01)
- akima_1_2_integral_value,akima_1_2_integral_error = scipy.integrate.quad(akima, 1, 2)
- print 'Value', akima_1_2_integral_value
- print 'Error', akima_1_2_integral_error
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement