Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. from math import *
  2. import numpy as np
  3.  
  4.  
  5. def lin_intp(x, fx, num):
  6. '''
  7. :param x: x values (np array!)
  8. :param fx: f(x) exact values
  9. :param num: num to find
  10. :return: [y, r] where y - f(num), r - mistake
  11. '''
  12. def find_nearest(array, value):
  13. idx = (np.abs(array - value)).argmin()
  14. return idx
  15. nearest = x[find_nearest(x, num)]
  16. q = (num - nearest) / (x[1] - x[0])
  17. y0 = fx[find_nearest(x, num)]
  18. dlty = fx[find_nearest(x, num) + 1] - fx[find_nearest(x, num)]
  19. y = y0 + q * dlty
  20. delta = [dlty, fx[find_nearest(x, num) + 2] - fx[find_nearest(x, num) + 1]]
  21. delta = round(round(delta[1], 4) - round(delta[0], 4), 4)
  22. r = abs((delta * y0) / 2 * q * (q - 1))
  23. return [y, r]
  24.  
  25.  
  26. x = np.array([0.5, 0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59, 0.6])
  27. fx = [round(exp(i), 4) for i in x]
  28. num = 0.541
  29. answer = lin_intp(x, fx, num)
  30. print(f'Ответ: {answer[0]}')
  31. print(f'Ошибка: {answer[1]}')
  32.  
  33.  
  34. def sq_intp(x, fx, num):
  35. '''
  36. :param x: x values (np array!)
  37. :param fx: f(x) exact values
  38. :param num: num to find
  39. :return: [y, r] where y - f(num), r - mistake
  40. '''
  41. def find_nearest(array, value):
  42. idx = (np.abs(array - value)).argmin()
  43. return idx
  44. nearest = x[find_nearest(x, num)]
  45. q = (num - nearest) / (x[1] - x[0])
  46. y0 = fx[find_nearest(x, num)]
  47. dlty1 = fx[find_nearest(x, num) + 1] - fx[find_nearest(x, num)]
  48. dlty2 = abs(dlty1 - (fx[find_nearest(x, num)] - fx[find_nearest(x, num) - 1]))
  49. dlty3 = dlty2 - (abs(fx[find_nearest(x, num) + 2] - fx[find_nearest(x, num) + 1]) - dlty1)
  50. y = y0 + q * dlty1 + abs(q*(q-1)/2) * dlty2
  51. r = dlty3/6 * abs(q*(q-1)*(q-2))
  52. return [y, r]
  53.  
  54.  
  55. x = np.array([1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5])
  56. fx = [0.89121, 0.93204, 0.96356, 0.98545, 0.99749, 0.99957, 0.99166, 0.97385, 0.9463, 0.9093, 0.86321, 0.8085, 0.74571, 0.67546, 0.59847]
  57. num = 1.538
  58. answer = sq_intp(x, fx, num)
  59. print(f'Ответ: {answer[0]}')
  60. print(f'Ошибка: {answer[1]}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement