Advertisement
myloyo

№3 интерполяционный многочлен в форме Ньютона

Oct 8th, 2024 (edited)
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | None | 0 0
  1. from itertools import zip_longest
  2.  
  3. dot = [0, 1, 2, 3]
  4. f = [8, 9, 16, 43]
  5. rr = []
  6. res = [[8, 9, 16, 43]]
  7. raz = 0
  8. for i in range(len(f) - 1):
  9.     rri = []
  10.     if i == 0:
  11.         raz += 1
  12.         for x in range(len(f) - 1):
  13.             rri.append((f[x + 1] - f[x]) / (dot[x + raz] - dot[x]))
  14.     else:
  15.         raz += 1
  16.         for x in range(len(rr[i - 1]) - 1):
  17.             rri.append((rr[i - 1][x + 1] - rr[i - 1][x]) / (dot[x + raz] - dot[x]))
  18.     res.append(rri)
  19.     rr.append(rri)
  20.  
  21. for i, row in enumerate(zip_longest(*res, fillvalue="")):
  22.     formatted_row = " - ".join(str(round(x, 3)) if isinstance(x, float) else str(x) for x in row)
  23.     print(f"f{i}: {formatted_row} ")
  24.  
  25. print()
  26.  
  27. dots = [0, 0.5, 1, 1.5, 2, 2.5, 3]
  28. for x in range(len(dots)):
  29.     ressum = res[0][0]
  30.     product_term = 1.0
  31.     for i in range(1, len(dot)):
  32.         product_term *= (dots[x] - dot[i - 1])
  33.         ressum += res[i][0] * product_term
  34.  
  35.     print(f"P({dots[x]}): {ressum}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement