Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.22 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from scipy import interpolate
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5.  
  6. x = np.arange(0, 20)
  7. y = np.sin(x)
  8.  
  9. print x
  10.  
  11. f = interpolate.interp1d(x, y)
  12. g = interpolate.interp1d(x, y, kind = 'cubic')
  13. h = interpolate.interp1d(x, y, kind = 'nearest')
  14. i = interpolate.interp1d(x, y, kind = 'zero')
  15.  
  16. # wyniki różnych metod przybliżeń:
  17. x1 = np.arange(0, 19, 0.1)
  18. yf = f(x1)
  19. yg = g(x1)
  20. yh = h(x1)
  21. yi = i(x1)
  22.    
  23.  
  24. def linear(x,y):
  25.     def result(tab):
  26.         mtab = np.copy(tab)
  27.         for index in range(len(mtab)):
  28.             mindex = sum(map(lambda x: x <= mtab[index], x))-1
  29.             print "{} < {} < {}".format(x[mindex], mtab[index], x[mindex+1])
  30.             a = (y[mindex+1]-y[mindex])/(x[mindex+1]-x[mindex])
  31.             b = y[mindex] - a*x[mindex]
  32.             mtab[index] = a*mtab[index] + b
  33.         return mtab
  34.     return result
  35.  
  36. j = linear(x,y)
  37. yj = j(x1)
  38.  
  39. a = plt.plot(x1, yj, label='linear', linestyle='-', linewidth=1.5, color='red')
  40. b = plt.plot(x1, yf, label='linear', linestyle='-', linewidth=1.5, color='blue')
  41.  
  42. plt.legend((a[0], b[0]),
  43.            ('$\Theta = 0.2$', '$\Theta = 0.7$'),
  44.            loc = (0.01, 0.1),  handlelength = -0.1)
  45.  
  46. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement