Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.70 KB | None | 0 0
  1. class Spline:
  2.  
  3.     def __init__(self, x, f):  # точки и значения функции в них
  4.         self.x = x
  5.         self.f = f
  6.  
  7.     def set_first_boundary_condition(self, df_a, df_b):  # первая производная на границах
  8.         self.df_a = df_a
  9.         self.df_b = df_b
  10.         self.find_b_with_first_boundary_condition()
  11.     def find_b_with_first_boundary_condition(self):
  12.         a = np.zeros((len(self.x) - 1, len(self.x) - 1))
  13.         a[0][0] = 1 + self.lambd(1)
  14.         a[0][1] = self.nu(1)
  15.  
  16.         for i in range(1, len(self.x) - 2):
  17.             a[i][i - 1] = self.lambd(i)
  18.             a[i][i] = 1 + self.nu(i) + self.lambd(i + 1)
  19.             a[i][i + 1] = self.nu(i + 1)
  20.  
  21.         a[-1][-2] = self.lambd(len(self.x) - 2)
  22.         a[-1][-1] = 1 + self.nu(len(self.x) - 2)
  23.  
  24.         d = np.array([3 * (self.f[i] - self.f[i - 1]) / (self.x[i] - self.x[i - 1]) for i in range(1, len(self.x) - 1)])
  25.         d[0] = 3 * (self.f[1] - self.f[0]) / (self.x[1] - self.x[0]) - self.df_a
  26.         d = np.append(d, [3 * (self.f[len(self.x) - 1] - self.f[len(self.x) - 2]) / (self.x[len(self.x) - 1] - self.x[len(self.x) - 2]) - self.df_b])
  27.  
  28.         self.b = np.linalg.solve(a, d)
  29.         self.b = np.insert(self.b, 0, self.df_a)
  30.         self.b = np.append(self.b, self.df_b)
  31.     def interpolate_at_point(self, xx):
  32.         for h in range(len(self.x) - 1):
  33.             if((self.x[h] <= xx) & (self.x[h + 1] >= xx)):
  34.                 i = h
  35.                 break
  36.         t = (xx - (self.x[i])) / self.h(i)
  37.         return (1 - t**3) * self.f[i] + t**3 * self.f[i + 1] + t * (1 - t) * self.lambd(i) * self.h(i) * self.b[i] + t * (1 - t) * (self.nu(i) + t) * self.h(i) * self.b[i + 1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement