Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import numpy as np
- import matplotlib.pyplot
- # Pn(x) = C0 + C1 * x + C2 * x^2 + ... + Cn * x^n
- class Polynomial:
- def __init__(self, coeff):
- self.coeff = coeff
- def evaluate(self, x):
- return sum([self.coeff[idx] * (x ** idx) for idx in range(len(self.coeff))])
- def GeometricMean(self):
- return math.sqrt(sum(coef ** 2 for coef in self.coeff))
- class LeastSquaresApprox:
- EPS = 0.001
- MtSize = 4
- def __init__(self, x, y):
- if len(x) != len(y):
- raise ArithmeticError("x and y have different sizes")
- self.x = x
- self.y = y
- # Aλ = b
- self.__rhs_vector = None
- self.__lhs_matrix = None
- self.__solution = None
- def solution(self) -> Polynomial:
- if self.__solution is not None:
- return self.__solution
- import numpy as np
- self.__lhs_matrix = [
- [sum([self.x[idx] ** (i + j) for idx in range(len(self.x))]) for j in range(LeastSquaresApprox.MtSize)]
- for i in range(LeastSquaresApprox.MtSize)]
- self.__rhs_vector = [sum(self.y[idx]*(self.x[idx]**i) for idx in range(len(self.y))) for i in
- range(LeastSquaresApprox.MtSize)]
- temp_solution = np.linalg.solve(np.matrix(self.__lhs_matrix), np.matrix(self.__rhs_vector).transpose())
- self.__solution = Polynomial([val.min() for val in temp_solution])
- return self.__solution
- def MSE(self):
- solution = self.solution()
- return math.sqrt(sum([(solution.evaluate(self.x[idx]) - self.y[idx]) ** 2 for idx in range(len(self.x))]))
- def AbsoluteError(self):
- return self.MSE() / math.sqrt(len(self.x))
- def RelativeError(self):
- return self.AbsoluteError() / Polynomial(self.y).GeometricMean()
- def Results(self):
- solution = self.solution()
- subtles = [(self.x[idx] + self.x[idx + 1]) / 2 for idx in range(len(self.x) - 1)]
- approximated_results = [solution.evaluate(point) for point in subtles]
- print(f"x = \n{self.x}\ny = \n{self.y}")
- print(f"A = \n{self.__lhs_matrix}\nb = \n{self.__rhs_vector}")
- print(f"λ = \n{solution.coeff}\nAbsoluteError = {self.AbsoluteError()}\nRelativeError = {self.RelativeError()}")
- print(f"new_x = \n{subtles}\napproximated_results = \n{approximated_results}")
- matplotlib.pyplot.plot(subtles, approximated_results)
- def kek(self, x, y):
- solution = self.solution()
- subtles = [(x[0] + x[-1]) / 2, (x[0] * x[-1]) ** (1 / 2), 2 / (1 / x[0] + 1 / x[-1])]
- z = [solution.evaluate(point) for point in subtles]
- ya = (y[0] + y[-1]) / 2
- yg = (y[0] * y[-1]) ** (1 / 2)
- yh = 2 / (1 / y[0] + 1 / y[-1])
- tmp = [z[0]-ya, z[1] - yg, z[0] - yg, z[1] - ya, z[2]-ya, z[0]-yh, z[2]-yh, z[2]-yg, z[1]-yh]
- tmp = [math.fabs(v) for v in tmp]
- print(f"[xa,xg,xh] = {subtles}")
- print(f"[z(xa),z(xg),z(xh)] = {z}")
- print(f"[di] = {tmp}")
- print(f"min.i[di] = {tmp.index(min(tmp)) + 1}")
- A, B, D1, D2 = 0, 0, 0, 0
- for i in range(len(x)):
- A += math.log(x[i])**2
- B += math.log(x[i])
- D1 += math.log(x[i])*math.log(y[i])
- D2 += math.log(y[i])
- system = np.matrix([[A, B], [B, (len(x)+1)]])
- coef = np.matrix([D1, D2]).transpose()
- solution = np.linalg.solve(system, coef)
- a, b = math.exp(solution[1][0]), solution[0][0].min()
- print(f"z(x) = {a}x^{b}")
- approximated_results = [a*(v**b) for v in x]
- matplotlib.pyplot.plot(x, approximated_results)
- mse = math.sqrt(sum([(approximated_results[idx] - self.y[idx]) ** 2 for idx in range(len(self.x))]))
- print(f"СКУ = {mse}")
- x = [1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]
- #y = [math.exp(v) for v in x]
- y = [1.15, 1.39, 1.85, 1.95, 2.16, 2.79, 2.88, 2.38, 3.61]
- matplotlib.pyplot.plot(x, y)
- Solver = LeastSquaresApprox(x, y)
- Solver.Results()
- Solver.kek(x, y)
- matplotlib.pyplot.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement