Advertisement
Sitisom

Численные методы

Sep 15th, 2020 (edited)
1,324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. from numpy import arange
  2. from math import fabs
  3.  
  4.  
  5. def lagranz(x, y, t):
  6.     z = 0
  7.     for j in range(len(y)):
  8.         p = 1
  9.         for i in range(len(x)):
  10.             if i != j:
  11.                 p *= (t-x[i])/(x[j]-x[i])
  12.         z += p * y[j]
  13.     return z
  14.  
  15.  
  16. def tabulate_point(x, e):
  17.     a = x
  18.     y = a
  19.     n = 0
  20.  
  21.     while fabs(a) > e:
  22.         q = - pow(x, 2) * (2*n+1) / (pow(2*n+3, 2)*(2*n+2))
  23.         a = q * a
  24.         y += a
  25.         n += 1
  26.  
  27.     return y
  28.  
  29.  
  30. def tabulation(a, b, count, e):
  31.     x_points = []
  32.     f_arr = []
  33.     h = (b - a) / count
  34.     for x in arange(a, b, h):
  35.         f = tabulate_point(x, e)
  36.         x_points.append(x)
  37.         f_arr.append(f)
  38.     return x_points, f_arr
  39.  
  40.  
  41. x_arr, y_arr = tabulation(0, 4, 10, pow(10, -6))
  42. x_arr1, y_arr1 = tabulation(0, 4, 5, pow(10, -6))
  43. for i, j in zip(x_arr, y_arr):
  44.     z = lagranz(x_arr1, y_arr1, i)
  45.     print('%.1f : %f : %f' % (i, z, z - j))
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement