From 68eb32f6182acd7b71c1a20e4b03809a4cb5bce1 Mon Sep 17 00:00:00 2001 From: lhye200 Date: Tue, 10 Jun 2025 14:29:12 +0800 Subject: [PATCH] 222 --- 120-3.py | 1 + 120-4.py | 1 + 120-5.py | 1 + 159-1.py | 2 +- 159-2.py | 1 + 159-3.py | 1 + 159-5.py | 3 ++- 159-6.py | 3 ++- 159-7.py | 1 + 68-1.py | 1 - 69-5.py | 6 +++++- 89-1.py | 6 +++++- 89-2.py | 2 +- 89-3.py | 2 +- all.py | 0 15 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 all.py diff --git a/120-3.py b/120-3.py index 0de11a5..8258a8b 100644 --- a/120-3.py +++ b/120-3.py @@ -4,6 +4,7 @@ x1, x2 = 3, 6 def fx(x): return x/(4+x**2) +# 复合Newton-Cotes公式 复合牛顿-科特斯公式 # n等分参数,x1到x2的区间,type=1表示梯形法,type=2表示辛普森法 def CompositeNewtonCotes(n, type): if type == 1: diff --git a/120-4.py b/120-4.py index c5f9e2c..f14eb15 100644 --- a/120-4.py +++ b/120-4.py @@ -2,6 +2,7 @@ def fx(x): return 1/x +# 逐次分半梯形递推公式 def SplitTrapezoidal(a,b,err): t1 = (b-a)*(fx(a)+fx(b))/2 k = 1 diff --git a/120-5.py b/120-5.py index 03eafa2..b4753b1 100644 --- a/120-5.py +++ b/120-5.py @@ -5,6 +5,7 @@ def fx(x): x = 1e-10 # Avoid division by zero return math.sin(x)/x +# 龙贝格方法 积分 def Romberg(a, b, err): t00 = (b-a)*(fx(a)+fx(b))/2 t01 = t10 = t11 = t20 = t21 = t30 = t31 = 0 diff --git a/159-1.py b/159-1.py index 468eedc..be4a60f 100644 --- a/159-1.py +++ b/159-1.py @@ -1,7 +1,7 @@ def fx(x): return x**4-3*x+1 - +# 二分法求解方程的根 def SolveByDivTwo(x1,x2,err): while abs(x2-x1) >= err: x = (x1+x2)/2 diff --git a/159-2.py b/159-2.py index 17ff624..2adf555 100644 --- a/159-2.py +++ b/159-2.py @@ -4,6 +4,7 @@ import matplotlib.pyplot as plt def fx(x): return math.exp(x)-math.sin(x) +# 绘制函数图像 并标记可能的零点 def DrawGraph(a, b, stepper): x = [a + (b-a)*i*stepper for i in range(int(1/stepper+1))] y = [fx(i) for i in x] diff --git a/159-3.py b/159-3.py index ae065eb..8fb7c98 100644 --- a/159-3.py +++ b/159-3.py @@ -4,6 +4,7 @@ def fd1(x): def fd2(x): return 1/(x-1)**0.5 +# 迭代法求解方程 def Renew(x,fd,err): count=0 i = 0 diff --git a/159-5.py b/159-5.py index 8db084b..fed4517 100644 --- a/159-5.py +++ b/159-5.py @@ -14,7 +14,7 @@ def f2(x): def df2(x): return 1/(1+x**2) - 1 - +# 牛顿方法求解方程 def NewtonSolve(fx, dfx, x0, err, N0): count = 0 x1 = x0 + 1 + err @@ -28,6 +28,7 @@ def NewtonSolve(fx, dfx, x0, err, N0): x0 = x1 return x1, 1 +# 查找根区间 def FindRootZone(fx,start,stop,step): x = start while x < stop: diff --git a/159-6.py b/159-6.py index 24e42f8..5f7c997 100644 --- a/159-6.py +++ b/159-6.py @@ -7,7 +7,7 @@ def f(x): def df(x): return 2*x - +# 牛顿方法求解方程 def NewtonSolve(fx, dfx, x0, err, N0): count = 0 x1 = x0 + 1 + err @@ -21,6 +21,7 @@ def NewtonSolve(fx, dfx, x0, err, N0): x0 = x1 return x1, 1 +# 查找根区间 def FindRootZone(fx,start,stop,step): x = start while x < stop: diff --git a/159-7.py b/159-7.py index 219fe37..0555e94 100644 --- a/159-7.py +++ b/159-7.py @@ -1,3 +1,4 @@ +# 查找根区间 def FindRootZone(fx,start,stop,step): x = start while x < stop: diff --git a/68-1.py b/68-1.py index 7935934..a451c33 100644 --- a/68-1.py +++ b/68-1.py @@ -1,4 +1,3 @@ -# import math diff --git a/69-5.py b/69-5.py index e2589f1..36e56bf 100644 --- a/69-5.py +++ b/69-5.py @@ -1,6 +1,7 @@ x = [0,1,2,3] y = [0,0,0,0] +# 追赶法 def ZGsolve(A,b): n = len(b) beta = [0]*n @@ -21,19 +22,21 @@ def ZGsolve(A,b): return b - +# 获取相邻点的差分 def GetDList(list_r): result = [] for i in range(1,len(list_r)): result.append(list_r[i] - list_r[i-1]) return result +# 获取相邻点的差商 def GetDQList(list_x, list_y): result = [] for i in range(1,len(list_y)): result.append((list_y[i] - list_y[i-1]) / (list_x[i] - list_x[i-1])) return result +# 三次样条插值 def CubicSplineInterpolation(list_x,list_y,boundary_type,a1,a2): list_h = GetDList(list_x) list_dqxy = GetDQList(list_x, list_y) @@ -68,6 +71,7 @@ def CubicSplineInterpolation(list_x,list_y,boundary_type,a1,a2): return M,list_h +# 打印矩阵 def PrintResult(M,list_h,list_x,list_y): for i in range(len(list_h)): k1 = (M[i+1]-M[i])/6/list_h[i] diff --git a/89-1.py b/89-1.py index 21c228f..bdd71c4 100644 --- a/89-1.py +++ b/89-1.py @@ -43,7 +43,7 @@ def SovleRowMain(A,b): return b - +# 最小二乘法拟合 def LeastSquares(list_x,list_y,n): m = len(list_x) x_n = [] @@ -63,6 +63,7 @@ def LeastSquares(list_x,list_y,n): A.append(tmp) return SovleRowMain(A,b) +# 计算多项式在给定x值上的值 def CalculateY(list_x, coeff): re = [] for i in range(len(list_x)): @@ -71,6 +72,7 @@ def CalculateY(list_x, coeff): re[i] += coeff[j]*list_x[i]**j return re +# 计算均方根误差 def MeanSquareErr(list_y,list_y_approx): m = len(list_y) err = 0 @@ -78,6 +80,7 @@ def MeanSquareErr(list_y,list_y_approx): err += (list_y[i] - list_y_approx[i])**2 return err**0.5 +# 计算最大误差 def MaxErr(list_y,list_y_approx): m = len(list_y) err = 0 @@ -86,6 +89,7 @@ def MaxErr(list_y,list_y_approx): err = abs(list_y[i] - list_y_approx[i]) return err +# 打印拟合方程 def PrintEquation(coeff): n = len(coeff) str_ = str(coeff[0]) + "+" diff --git a/89-2.py b/89-2.py index bbedc1a..f9de217 100644 --- a/89-2.py +++ b/89-2.py @@ -45,7 +45,7 @@ def SovleRowMain(A,b): return b - +# 最小二乘法拟合 def LeastSquares(list_x,list_y,n): m = len(list_x) x_n = [] diff --git a/89-3.py b/89-3.py index 1f5ceb5..564fa89 100644 --- a/89-3.py +++ b/89-3.py @@ -43,7 +43,7 @@ def SovleRowMain(A,b): return b - +# 最小二乘法拟合 def LeastSquares(list_x,list_y,n): m = len(list_x) x_n = [] diff --git a/all.py b/all.py new file mode 100644 index 0000000..e69de29