This commit is contained in:
2025-06-10 14:29:12 +08:00
parent 0d7b89e21d
commit 68eb32f618
15 changed files with 23 additions and 8 deletions

View File

@@ -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:

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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]

View File

@@ -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

View File

@@ -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:

View File

@@ -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:

View File

@@ -1,3 +1,4 @@
# 查找根区间
def FindRootZone(fx,start,stop,step):
x = start
while x < stop:

View File

@@ -1,4 +1,3 @@
#
import math

View File

@@ -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]

View File

@@ -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]) + "+"

View File

@@ -45,7 +45,7 @@ def SovleRowMain(A,b):
return b
# 最小二乘法拟合
def LeastSquares(list_x,list_y,n):
m = len(list_x)
x_n = []

View File

@@ -43,7 +43,7 @@ def SovleRowMain(A,b):
return b
# 最小二乘法拟合
def LeastSquares(list_x,list_y,n):
m = len(list_x)
x_n = []

0
all.py Normal file
View File