diff --git a/89-1.py b/89-1.py index bdd71c4..84a3ccd 100644 --- a/89-1.py +++ b/89-1.py @@ -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)) \ No newline at end of file diff --git a/89-2.py b/89-2.py index f9de217..844653a 100644 --- a/89-2.py +++ b/89-2.py @@ -1,9 +1,5 @@ import math -x = [1,2,4,8,16,32,64] -y = [4.22,4.02,3.85,3.59,3.44,3.02,2.59] - - # 列主元高斯消元法 def SovleRowMain(A,b): ks = 0.00000001 @@ -63,8 +59,15 @@ 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和y的值改为实际数据####################### +x = [1,2,4,8,16,32,64] +y = [4.22,4.02,3.85,3.59,3.44,3.02,2.59] if __name__ == "__main__": # 取对数 ln(W) = ln(C)+lamda*ln(t) ln_W = [math.log(i) for i in y] diff --git a/89-3.py b/89-3.py index 564fa89..fd0576f 100644 --- a/89-3.py +++ b/89-3.py @@ -1,7 +1,4 @@ -x = [19,25,31,38,44] -y = [19.0,32.3,49.0,73.3,97.8] - # 列主元高斯消元法 def SovleRowMain(A,b): ks = 0.00000001 @@ -61,8 +58,15 @@ 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和y换成题干的数值################### +x = [19,25,31,38,44] +y = [19.0,32.3,49.0,73.3,97.8] if __name__ == "__main__": x_square = [i**2 for i in x] coeff = LeastSquares(x_square, y, 1)