Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Spline:
- def __init__(self, x, f): # точки и значения функции в них
- self.x = x
- self.f = f
- def set_first_boundary_condition(self, df_a, df_b): # первая производная на границах
- self.df_a = df_a
- self.df_b = df_b
- self.find_b_with_first_boundary_condition()
- def find_b_with_first_boundary_condition(self):
- a = np.zeros((len(self.x) - 1, len(self.x) - 1))
- a[0][0] = 1 + self.lambd(1)
- a[0][1] = self.nu(1)
- for i in range(1, len(self.x) - 2):
- a[i][i - 1] = self.lambd(i)
- a[i][i] = 1 + self.nu(i) + self.lambd(i + 1)
- a[i][i + 1] = self.nu(i + 1)
- a[-1][-2] = self.lambd(len(self.x) - 2)
- a[-1][-1] = 1 + self.nu(len(self.x) - 2)
- 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)])
- d[0] = 3 * (self.f[1] - self.f[0]) / (self.x[1] - self.x[0]) - self.df_a
- 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])
- self.b = np.linalg.solve(a, d)
- self.b = np.insert(self.b, 0, self.df_a)
- self.b = np.append(self.b, self.df_b)
- def interpolate_at_point(self, xx):
- for h in range(len(self.x) - 1):
- if((self.x[h] <= xx) & (self.x[h + 1] >= xx)):
- i = h
- break
- t = (xx - (self.x[i])) / self.h(i)
- 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