111
This commit is contained in:
20
89-1.py
20
89-1.py
@@ -1,6 +1,3 @@
|
|||||||
x = [2,4,6,8]
|
|
||||||
y = [2,11,28,40]
|
|
||||||
|
|
||||||
|
|
||||||
# 列主元高斯消元法
|
# 列主元高斯消元法
|
||||||
def SovleRowMain(A,b):
|
def SovleRowMain(A,b):
|
||||||
@@ -61,7 +58,11 @@ def LeastSquares(list_x,list_y,n):
|
|||||||
for j in range(n+1):
|
for j in range(n+1):
|
||||||
tmp.append(x_n[i+j])
|
tmp.append(x_n[i+j])
|
||||||
A.append(tmp)
|
A.append(tmp)
|
||||||
return SovleRowMain(A,b)
|
print("A:", A)
|
||||||
|
print("b:", b)
|
||||||
|
result = SovleRowMain(A, b)
|
||||||
|
print("result:", result)
|
||||||
|
return result
|
||||||
|
|
||||||
# 计算多项式在给定x值上的值
|
# 计算多项式在给定x值上的值
|
||||||
def CalculateY(list_x, coeff):
|
def CalculateY(list_x, coeff):
|
||||||
@@ -99,22 +100,25 @@ def PrintEquation(coeff):
|
|||||||
str_ = str_[0:len(str_)-1]
|
str_ = str_[0:len(str_)-1]
|
||||||
print(str_)
|
print(str_)
|
||||||
|
|
||||||
|
#把x和y换成题干的数值###################
|
||||||
|
x = [2,4,6,8]
|
||||||
|
y = [2,11,28,40]
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print("一次拟合")
|
print("一次拟合")
|
||||||
coeff = LeastSquares(x,y,1)
|
coeff = LeastSquares(x,y,1)
|
||||||
PrintEquation(coeff)
|
PrintEquation(coeff)
|
||||||
y_approx = CalculateY(x, coeff)
|
y_approx = CalculateY(x, coeff)
|
||||||
print("MeanSquareErr:")
|
print("均方根误差:")
|
||||||
print(MeanSquareErr(y,y_approx))
|
print(MeanSquareErr(y,y_approx))
|
||||||
print("MaxErr:")
|
print("最大误差:")
|
||||||
print(MaxErr(y,y_approx))
|
print(MaxErr(y,y_approx))
|
||||||
|
|
||||||
print("二次拟合")
|
print("二次拟合")
|
||||||
coeff = LeastSquares(x,y,2)
|
coeff = LeastSquares(x,y,2)
|
||||||
PrintEquation(coeff)
|
PrintEquation(coeff)
|
||||||
y_approx = CalculateY(x, coeff)
|
y_approx = CalculateY(x, coeff)
|
||||||
print("MeanSquareErr:")
|
print("均方根误差:")
|
||||||
print(MeanSquareErr(y,y_approx))
|
print(MeanSquareErr(y,y_approx))
|
||||||
print("MaxErr:")
|
print("最大误差:")
|
||||||
print(MaxErr(y,y_approx))
|
print(MaxErr(y,y_approx))
|
||||||
|
|
||||||
13
89-2.py
13
89-2.py
@@ -1,9 +1,5 @@
|
|||||||
import math
|
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):
|
def SovleRowMain(A,b):
|
||||||
ks = 0.00000001
|
ks = 0.00000001
|
||||||
@@ -63,8 +59,15 @@ def LeastSquares(list_x,list_y,n):
|
|||||||
for j in range(n+1):
|
for j in range(n+1):
|
||||||
tmp.append(x_n[i+j])
|
tmp.append(x_n[i+j])
|
||||||
A.append(tmp)
|
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__":
|
if __name__ == "__main__":
|
||||||
# 取对数 ln(W) = ln(C)+lamda*ln(t)
|
# 取对数 ln(W) = ln(C)+lamda*ln(t)
|
||||||
ln_W = [math.log(i) for i in y]
|
ln_W = [math.log(i) for i in y]
|
||||||
|
|||||||
12
89-3.py
12
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):
|
def SovleRowMain(A,b):
|
||||||
ks = 0.00000001
|
ks = 0.00000001
|
||||||
@@ -61,8 +58,15 @@ def LeastSquares(list_x,list_y,n):
|
|||||||
for j in range(n+1):
|
for j in range(n+1):
|
||||||
tmp.append(x_n[i+j])
|
tmp.append(x_n[i+j])
|
||||||
A.append(tmp)
|
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__":
|
if __name__ == "__main__":
|
||||||
x_square = [i**2 for i in x]
|
x_square = [i**2 for i in x]
|
||||||
coeff = LeastSquares(x_square, y, 1)
|
coeff = LeastSquares(x_square, y, 1)
|
||||||
|
|||||||
Reference in New Issue
Block a user