This commit is contained in:
lwj
2025-06-13 23:08:32 +08:00
parent e5729d884f
commit 3fc9330dd6
3 changed files with 28 additions and 17 deletions

20
89-1.py
View File

@@ -1,6 +1,3 @@
x = [2,4,6,8]
y = [2,11,28,40]
# 列主元高斯消元法
def SovleRowMain(A,b):
@@ -61,7 +58,11 @@ def LeastSquares(list_x,list_y,n):
for j in range(n+1):
tmp.append(x_n[i+j])
A.append(tmp)
return SovleRowMain(A,b)
print("A:", A)
print("b:", b)
result = SovleRowMain(A, b)
print("result:", result)
return result
# 计算多项式在给定x值上的值
def CalculateY(list_x, coeff):
@@ -99,22 +100,25 @@ def PrintEquation(coeff):
str_ = str_[0:len(str_)-1]
print(str_)
#把x和y换成题干的数值###################
x = [2,4,6,8]
y = [2,11,28,40]
if __name__ == "__main__":
print("一次拟合")
coeff = LeastSquares(x,y,1)
PrintEquation(coeff)
y_approx = CalculateY(x, coeff)
print("MeanSquareErr:")
print("均方根误差:")
print(MeanSquareErr(y,y_approx))
print("MaxErr:")
print("最大误差:")
print(MaxErr(y,y_approx))
print("二次拟合")
coeff = LeastSquares(x,y,2)
PrintEquation(coeff)
y_approx = CalculateY(x, coeff)
print("MeanSquareErr:")
print("均方根误差:")
print(MeanSquareErr(y,y_approx))
print("MaxErr:")
print("最大误差:")
print(MaxErr(y,y_approx))