Advertisement
Norvager

Интер+Апрок

Nov 11th, 2021
676
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.77 KB | None | 0 0
  1. import math
  2. import numpy as np
  3.  
  4. def f(x):
  5.     y = np.log(15.3*x**2)
  6.     return y
  7.  
  8. def Lagrange_polynom(point):
  9.     L = 0
  10.     for i in range(left, right + 1, 1):
  11.         P = 1
  12.         for j in range(left, right + 1, 1):
  13.             if j != i:
  14.                 P = P*(point - x[j])/(x[i] - x[j])
  15.         L += P * y[i]
  16.     return L
  17.  
  18. def f_apr(coefficients, x):
  19.     y = coefficients[0]*x**2 + coefficients[1]*x + coefficients[2]
  20.     return y
  21.  
  22. def find_coefficients(x, y): #Search for coefficients a, b and c
  23.     print('=====================================================')
  24.     print('search for coefficients...')
  25.     matrix = [[0, 0, 0],
  26.               [0, 0, 0],
  27.               [0, 0, 0]]
  28.     B = [0]*3
  29.  
  30.     for i in range(0, 14, 1):
  31.         matrix[0][0] += x[i]**4
  32.         matrix[0][1] += x[i]**3
  33.         matrix[0][2] += x[i]**2
  34.         B[0] += y[i]*x[i]**2
  35.  
  36.         matrix[1][0] += x[i]**3
  37.         matrix[1][1] += x[i]**2
  38.         matrix[1][2] += x[i]
  39.         B[1] += y[i]*x[i]
  40.  
  41.         matrix[2][0] += x[i]**2
  42.         matrix[2][1] += x[i]
  43.         matrix[2][2] += 1
  44.         B[2] += y[i]
  45.    
  46.     print('\nMatrix: ')
  47.     for i in range(0, 3, 1):
  48.         print(f'a*{matrix[i][0]} + b*{matrix[i][1]} + с*{matrix[i][2]} = {B[i]}')
  49.  
  50.     #===============Еhe method of simple iterations============================
  51.     # Prepairing matrix
  52.     for i in range(len(matrix)):
  53.         n = matrix[i][i]
  54.         for j in range(len(matrix)):
  55.             if i == j:
  56.                 matrix[i][j] = 0
  57.                 continue
  58.             matrix[i][j] = matrix[i][j]/(-n)
  59.         B[i] = B[i] / n
  60.  
  61.     Res_prev = B # Starting point
  62.     Res = [0]*3
  63.     while True:
  64.  
  65.         # Solution
  66.         for i in range(len(matrix)):
  67.             Res[i] = matrix[i][0]*Res_prev[0] + matrix[i][1]*Res_prev[1] + matrix[i][2]*Res_prev[2] + B[i]
  68.  
  69.         # Checking the stop condition
  70.         eps = 0.01
  71.         inaccuracy = 0
  72.         for i in range(len(matrix)):
  73.             inaccuracy += (Res[i] - Res_prev[i])**2
  74.         inaccuracy = math.sqrt(inaccuracy)
  75.         if inaccuracy < eps:
  76.             break
  77.  
  78.         Res_prev = Res
  79.  
  80.     print('\nsearch completed...')
  81.     print(f'coefficients: a = {Res[0]}, b = {Res[1]}, c = {Res[2]}')
  82.     coefficients = Res
  83.     print('\n==============================================================')
  84.     return coefficients
  85.  
  86.  
  87. x = [] # Array of argument values
  88. y = [] # Array of function values
  89. print('14 points:')
  90. c = 0 # Counter for output
  91. for i in range(1, 15, 1):
  92.     xi = 0.2*i
  93.     x.append(xi)
  94.     y.append(f(xi))
  95.     print(f'x{c} = {x[c]}')
  96.     print(f'y{c} = {y[c]}\n')
  97.     c += 1
  98.  
  99. coefficients = find_coefficients(x, y)
  100.  
  101. left = 1
  102. right = 3
  103. point = 0.5
  104. node = 0.6
  105. z1 = x[13] + 1 # x14 + 0.1
  106. z2 = x[13] + 5 # x14 + 0.5
  107. print(f'Approximation/Prediction points: {z1} and {z2}') # Prediction points
  108.  
  109. print('\n=================================node====================')
  110. print(f'Function({node}) = {f(node)}')
  111. print(f'Lagrange_polynom({node}) = {Lagrange_polynom(node)}')
  112. print(f'Approximation({node}) = {f_apr(coefficients, node)}')
  113.  
  114. print('\n=================================point===================')
  115. print(f'Function({point}) = {f(point)}')
  116. print(f'Lagrange_polynom({point}) = {Lagrange_polynom(point)}')
  117. print(f'Approximation({point}) = {f_apr(coefficients, point)}')
  118.  
  119. print('\n=================================x_apr1==================')
  120. left = 0
  121. right = 12
  122. print(f'Function({z1}) = {f(z1)}')
  123. print(f'Lagrange_polynom({z1}) = {Lagrange_polynom(z1)}')
  124. print(f'Approximation({z1}) = {f_apr(coefficients, z1)}')
  125.  
  126. print('\n=================================x_apr2==================')
  127. print(f'Function({z2}) = {f(z2)}')
  128. print(f'Lagrange_polynom({z2}) = {Lagrange_polynom(z2)}')
  129. print(f'Approximation({z2}) = {f_apr(coefficients, z2)}')
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement