Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import *
- import numpy as np
- def lin_intp(x, fx, num):
- '''
- :param x: x values (np array!)
- :param fx: f(x) exact values
- :param num: num to find
- :return: [y, r] where y - f(num), r - mistake
- '''
- def find_nearest(array, value):
- idx = (np.abs(array - value)).argmin()
- return idx
- nearest = x[find_nearest(x, num)]
- q = (num - nearest) / (x[1] - x[0])
- y0 = fx[find_nearest(x, num)]
- dlty = fx[find_nearest(x, num) + 1] - fx[find_nearest(x, num)]
- y = y0 + q * dlty
- delta = [dlty, fx[find_nearest(x, num) + 2] - fx[find_nearest(x, num) + 1]]
- delta = round(round(delta[1], 4) - round(delta[0], 4), 4)
- r = abs((delta * y0) / 2 * q * (q - 1))
- return [y, r]
- 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])
- fx = [round(exp(i), 4) for i in x]
- num = 0.541
- answer = lin_intp(x, fx, num)
- print(f'Ответ: {answer[0]}')
- print(f'Ошибка: {answer[1]}')
- def sq_intp(x, fx, num):
- '''
- :param x: x values (np array!)
- :param fx: f(x) exact values
- :param num: num to find
- :return: [y, r] where y - f(num), r - mistake
- '''
- def find_nearest(array, value):
- idx = (np.abs(array - value)).argmin()
- return idx
- nearest = x[find_nearest(x, num)]
- q = (num - nearest) / (x[1] - x[0])
- y0 = fx[find_nearest(x, num)]
- dlty1 = fx[find_nearest(x, num) + 1] - fx[find_nearest(x, num)]
- dlty2 = abs(dlty1 - (fx[find_nearest(x, num)] - fx[find_nearest(x, num) - 1]))
- dlty3 = dlty2 - (abs(fx[find_nearest(x, num) + 2] - fx[find_nearest(x, num) + 1]) - dlty1)
- y = y0 + q * dlty1 + abs(q*(q-1)/2) * dlty2
- r = dlty3/6 * abs(q*(q-1)*(q-2))
- return [y, r]
- 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])
- 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]
- num = 1.538
- answer = sq_intp(x, fx, num)
- print(f'Ответ: {answer[0]}')
- print(f'Ошибка: {answer[1]}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement