Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import numpy as np
- def f(x):
- y = np.log(15.3*x**2)
- return y
- def Lagrange_polynom(point):
- L = 0
- for i in range(left, right + 1, 1):
- P = 1
- for j in range(left, right + 1, 1):
- if j != i:
- P = P*(point - x[j])/(x[i] - x[j])
- L += P * y[i]
- return L
- def f_apr(coefficients, x):
- y = coefficients[0]*x**2 + coefficients[1]*x + coefficients[2]
- return y
- def find_coefficients(x, y): #Search for coefficients a, b and c
- print('=====================================================')
- print('search for coefficients...')
- matrix = [[0, 0, 0],
- [0, 0, 0],
- [0, 0, 0]]
- B = [0]*3
- for i in range(0, 14, 1):
- matrix[0][0] += x[i]**4
- matrix[0][1] += x[i]**3
- matrix[0][2] += x[i]**2
- B[0] += y[i]*x[i]**2
- matrix[1][0] += x[i]**3
- matrix[1][1] += x[i]**2
- matrix[1][2] += x[i]
- B[1] += y[i]*x[i]
- matrix[2][0] += x[i]**2
- matrix[2][1] += x[i]
- matrix[2][2] += 1
- B[2] += y[i]
- print('\nMatrix: ')
- for i in range(0, 3, 1):
- print(f'a*{matrix[i][0]} + b*{matrix[i][1]} + с*{matrix[i][2]} = {B[i]}')
- #===============Еhe method of simple iterations============================
- # Prepairing matrix
- for i in range(len(matrix)):
- n = matrix[i][i]
- for j in range(len(matrix)):
- if i == j:
- matrix[i][j] = 0
- continue
- matrix[i][j] = matrix[i][j]/(-n)
- B[i] = B[i] / n
- Res_prev = B # Starting point
- Res = [0]*3
- while True:
- # Solution
- for i in range(len(matrix)):
- Res[i] = matrix[i][0]*Res_prev[0] + matrix[i][1]*Res_prev[1] + matrix[i][2]*Res_prev[2] + B[i]
- # Checking the stop condition
- eps = 0.01
- inaccuracy = 0
- for i in range(len(matrix)):
- inaccuracy += (Res[i] - Res_prev[i])**2
- inaccuracy = math.sqrt(inaccuracy)
- if inaccuracy < eps:
- break
- Res_prev = Res
- print('\nsearch completed...')
- print(f'coefficients: a = {Res[0]}, b = {Res[1]}, c = {Res[2]}')
- coefficients = Res
- print('\n==============================================================')
- return coefficients
- x = [] # Array of argument values
- y = [] # Array of function values
- print('14 points:')
- c = 0 # Counter for output
- for i in range(1, 15, 1):
- xi = 0.2*i
- x.append(xi)
- y.append(f(xi))
- print(f'x{c} = {x[c]}')
- print(f'y{c} = {y[c]}\n')
- c += 1
- coefficients = find_coefficients(x, y)
- left = 1
- right = 3
- point = 0.5
- node = 0.6
- z1 = x[13] + 1 # x14 + 0.1
- z2 = x[13] + 5 # x14 + 0.5
- print(f'Approximation/Prediction points: {z1} and {z2}') # Prediction points
- print('\n=================================node====================')
- print(f'Function({node}) = {f(node)}')
- print(f'Lagrange_polynom({node}) = {Lagrange_polynom(node)}')
- print(f'Approximation({node}) = {f_apr(coefficients, node)}')
- print('\n=================================point===================')
- print(f'Function({point}) = {f(point)}')
- print(f'Lagrange_polynom({point}) = {Lagrange_polynom(point)}')
- print(f'Approximation({point}) = {f_apr(coefficients, point)}')
- print('\n=================================x_apr1==================')
- left = 0
- right = 12
- print(f'Function({z1}) = {f(z1)}')
- print(f'Lagrange_polynom({z1}) = {Lagrange_polynom(z1)}')
- print(f'Approximation({z1}) = {f_apr(coefficients, z1)}')
- print('\n=================================x_apr2==================')
- print(f'Function({z2}) = {f(z2)}')
- print(f'Lagrange_polynom({z2}) = {Lagrange_polynom(z2)}')
- print(f'Approximation({z2}) = {f_apr(coefficients, z2)}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement