This commit is contained in:
lwj
2025-05-15 12:22:42 +08:00
parent af50496fdc
commit fda76d99f7
3 changed files with 32 additions and 46 deletions

View File

@@ -2,7 +2,7 @@ def fx(x):
return x**4-3*x+1 return x**4-3*x+1
def SolveByDivTwo(x1,x2,err): def erfen(x1,x2,err):
while abs(x2-x1) >= err: while abs(x2-x1) >= err:
x = (x1+x2)/2 x = (x1+x2)/2
if fx(x) * fx(x1) < 0: if fx(x) * fx(x1) < 0:
@@ -15,6 +15,5 @@ def SolveByDivTwo(x1,x2,err):
if __name__ == "__main__": if __name__ == "__main__":
x1 = 0.3 x1 = 0.3
x2 = 0.4 x2 = 0.4
err = 0.5e-5 root = erfen(x1, x2, 0.5e-5)
root = SolveByDivTwo(x1, x2, err) print(f"解为: {root}")
print(f"Root: {root:.5f}")

View File

@@ -4,21 +4,14 @@ import matplotlib.pyplot as plt
def fx(x): def fx(x):
return math.exp(x)-math.sin(x) return math.exp(x)-math.sin(x)
def DrawGraph(a, b, stepper): def huatu(a, b, buchang):
x = [a + (b-a)*i*stepper for i in range(int(1/stepper+1))] x = [a + (b-a)*i*buchang for i in range(int(1/buchang+1))]
y = [fx(i) for i in x] y = [fx(i) for i in x]
plt.xlim(a-abs(b-a)*0.1, b+abs(b-a)*0.1)
plt.plot(x, y) plt.plot(x, y)
plt.axhline(0, color='black', lw=0.5, ls='-') plt.axhline(0, color='black', lw=1)
plt.axvline(a, color='red', lw=0.5, ls='--')
plt.axvline(b, color='red', lw=0.5, ls='--')
plt.text(a, y[0], f'({a:.5f},{y[0]:.5f})', fontsize=8, ha='left')
plt.text(b, y[-1], f'({b:.5f},{y[-1]:.5f})', fontsize=8, ha='right')
for i in range(len(x)-1): for i in range(len(x)-1):
if y[i] * y[i+1] < 0: if y[i] * y[i+1] < 0:
print(f"可能存在零点: ({x[i]:.5f},{y[i]:.5f})({x[i+1]:.5f},{y[i+1]:.5f})之间") print(f"({x[i]},{y[i]}),({x[i+1]},{y[i+1]})")
plt.plot((x[i]+x[i+1])/2, (y[i]+y[i+1])/2, 'ro', markersize=3)
plt.title("Graph of f(x)")
plt.xlabel("x") plt.xlabel("x")
plt.ylabel("f(x)") plt.ylabel("f(x)")
plt.show() plt.show()
@@ -28,7 +21,4 @@ def DrawGraph(a, b, stepper):
if __name__ == "__main__": if __name__ == "__main__":
a = -2*math.pi a = -2*math.pi
b = math.pi b = math.pi
step = 0.00001 huatu(a, b, 0.00001)
print(f"边界点: {a}, {b}")
print(f"步长: {step}")
x, y = DrawGraph(a, b, step)

View File

@@ -1,43 +1,40 @@
def fd1(x): def f1(x):
return 1+1/x**2 return 1+1/x**2
def fd2(x): def f2(x):
return 1/(x-1)**0.5 return 1/((x-1)**0.5)
def Renew(x,fd,err): def diedai(x,fd,err):
count=0 count=0
i = 0 i = 0
try:
while True: while True:
xk = fd(x) xk = fd(x)
if abs(i) < (xk-x): if complex(xk).imag != 0:
break
if (i) < abs(xk-x):
count += 1 count += 1
else: else:
count = 0 count = 0
if count > 10: if count > 10:
print("不收敛")
break break
if abs(xk-x) < err: if abs(xk-x) < err:
return xk return xk
i = xk-x i = abs(xk-x)
x = xk x = xk
except Exception as e:
print(f"发生错误: {e}")
return None
return None return None
if __name__ == "__main__": if __name__ == "__main__":
x0 = 1.5 x0 = 1.5
result = Renew(x0, fd1, 1e-5) result = diedai(x0, f1, 1e-5)
if result is not None: if result is not None:
print(f"1式收敛 解为: {result:.5f}") print(f"(1)收敛 解为: {result:.5f}")
else: else:
print("1式不收敛") print("(1)不收敛")
result = Renew(x0, fd2, 1e-5) result = diedai(x0, f2, 1e-5)
if result is not None: if result is not None:
print(f"2式收敛 解为: {result:.5f}") print(f"(2)收敛 解为: {result:.5f}")
else: else:
print("2式不收敛") print("(2)不收敛")