Добавил:
chrysler_a57_mltbnk
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:семестр 1 / lab14
.py import os
clear = lambda: os.system('cls')
clear()
import numpy as np
import matplotlib.pyplot as plt
#Key points
x = [7.0 , 12.0 , 17.0 , 22.0 , 27.0 , 32.0]
y = [7.0 , 17.0 , -3.0 , -11.0 , 19.0 , 11.5]
#Compilation of a system of equations
#Abscissa matrix
X = np.zeros(36)
X.shape = (6,6)
#Ordinate matrix
Y = np.zeros(6)
Y.shape = (6,1)
#Calculate the x values at each coefficient for each point
for i in range(0,6):
X[i][5] = x[i]**0
X[i][4] = x[i]**1
X[i][3] = x[i]**2
X[i][2] = x[i]**3
X[i][1] = x[i]**4
X[i][0] = x[i]**5
#Answers matrix
for i in range(0,6):
Y[i] = y[i]
#Matrix of coefficients
A = np.linalg.solve(X, Y)
A.shape = (6,1)
#Output
#Polynomial in general form
print(f'Polynomial in general form:\ny(x) = a5*x^5 + a4*x^4 + a3*x^3 + a2*x^2 + a1*x^1 + a0*x^0\n')
#System of linear algebraic equations
print(f'System of linear algebraic equations\n{chr(0x250F)}')
for i in range(0,6):
print(f'{chr(0x2503)} y{i}(x{i}) = a5*{int(X[i][0])} + a4*{int(X[i][1])} + a3*{int(X[i][2])} + a2*{int(X[i][3])} + a1*{int(X[i][4])} + a0*{int(X[i][5])}')
print(chr(0x2517),"\n")
#Resulting polynomial
print(f'Resulting polynomial:\n{A[0]}*x^5 + {A[1]}*x^4 + {A[2]}*x^3 + {A[3]}*x^2 + {A[4]}*x^1 + {A[5]}*x^0\n')
#Calculating values for plotting
tmp = []
I = []
for i in range(0,33):
tmp.append(A[5] + A[4]*i + A[3]*i**2 + A[2]*i**3 + A[1]*i**4 + A[0]*i**5)
I.append(i)
#Polynomial plotting
plt.plot(x, y)
plt.plot(I, tmp)
plt.grid()
plt.show()
clear = lambda: os.system('cls')
clear()
import numpy as np
import matplotlib.pyplot as plt
#Key points
x = [7.0 , 12.0 , 17.0 , 22.0 , 27.0 , 32.0]
y = [7.0 , 17.0 , -3.0 , -11.0 , 19.0 , 11.5]
#Compilation of a system of equations
#Abscissa matrix
X = np.zeros(36)
X.shape = (6,6)
#Ordinate matrix
Y = np.zeros(6)
Y.shape = (6,1)
#Calculate the x values at each coefficient for each point
for i in range(0,6):
X[i][5] = x[i]**0
X[i][4] = x[i]**1
X[i][3] = x[i]**2
X[i][2] = x[i]**3
X[i][1] = x[i]**4
X[i][0] = x[i]**5
#Answers matrix
for i in range(0,6):
Y[i] = y[i]
#Matrix of coefficients
A = np.linalg.solve(X, Y)
A.shape = (6,1)
#Output
#Polynomial in general form
print(f'Polynomial in general form:\ny(x) = a5*x^5 + a4*x^4 + a3*x^3 + a2*x^2 + a1*x^1 + a0*x^0\n')
#System of linear algebraic equations
print(f'System of linear algebraic equations\n{chr(0x250F)}')
for i in range(0,6):
print(f'{chr(0x2503)} y{i}(x{i}) = a5*{int(X[i][0])} + a4*{int(X[i][1])} + a3*{int(X[i][2])} + a2*{int(X[i][3])} + a1*{int(X[i][4])} + a0*{int(X[i][5])}')
print(chr(0x2517),"\n")
#Resulting polynomial
print(f'Resulting polynomial:\n{A[0]}*x^5 + {A[1]}*x^4 + {A[2]}*x^3 + {A[3]}*x^2 + {A[4]}*x^1 + {A[5]}*x^0\n')
#Calculating values for plotting
tmp = []
I = []
for i in range(0,33):
tmp.append(A[5] + A[4]*i + A[3]*i**2 + A[2]*i**3 + A[1]*i**4 + A[0]*i**5)
I.append(i)
#Polynomial plotting
plt.plot(x, y)
plt.plot(I, tmp)
plt.grid()
plt.show()
Соседние файлы в папке семестр 1
