diff --git a/159-1.py b/159-1.py index 468eedc..e602ffa 100644 --- a/159-1.py +++ b/159-1.py @@ -2,7 +2,7 @@ def fx(x): return x**4-3*x+1 -def SolveByDivTwo(x1,x2,err): +def erfen(x1,x2,err): while abs(x2-x1) >= err: x = (x1+x2)/2 if fx(x) * fx(x1) < 0: @@ -15,6 +15,5 @@ def SolveByDivTwo(x1,x2,err): if __name__ == "__main__": x1 = 0.3 x2 = 0.4 - err = 0.5e-5 - root = SolveByDivTwo(x1, x2, err) - print(f"Root: {root:.5f}") \ No newline at end of file + root = erfen(x1, x2, 0.5e-5) + print(f"解为: {root}") \ No newline at end of file diff --git a/159-2.py b/159-2.py index 17ff624..3a48ba8 100644 --- a/159-2.py +++ b/159-2.py @@ -4,21 +4,14 @@ 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))] +def huatu(a, b, buchang): + x = [a + (b-a)*i*buchang for i in range(int(1/buchang+1))] 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.axhline(0, color='black', lw=0.5, ls='-') - 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') + plt.axhline(0, color='black', lw=1) for i in range(len(x)-1): if y[i] * y[i+1] < 0: - print(f"可能存在零点: ({x[i]:.5f},{y[i]:.5f})和({x[i+1]:.5f},{y[i+1]:.5f})之间") - plt.plot((x[i]+x[i+1])/2, (y[i]+y[i+1])/2, 'ro', markersize=3) - plt.title("Graph of f(x)") + print(f"({x[i]},{y[i]}),({x[i+1]},{y[i+1]})") plt.xlabel("x") plt.ylabel("f(x)") plt.show() @@ -28,7 +21,4 @@ def DrawGraph(a, b, stepper): if __name__ == "__main__": a = -2*math.pi b = math.pi - step = 0.00001 - print(f"边界点: {a}, {b}") - print(f"步长: {step}") - x, y = DrawGraph(a, b, step) \ No newline at end of file + huatu(a, b, 0.00001) \ No newline at end of file diff --git a/159-3.py b/159-3.py index ae065eb..2ae4025 100644 --- a/159-3.py +++ b/159-3.py @@ -1,43 +1,40 @@ -def fd1(x): +def f1(x): return 1+1/x**2 -def fd2(x): - return 1/(x-1)**0.5 +def f2(x): + return 1/((x-1)**0.5) -def Renew(x,fd,err): +def diedai(x,fd,err): count=0 i = 0 - try: - while True: - xk = fd(x) - if abs(i) < (xk-x): - count += 1 - else: - count = 0 - if count > 10: - print("不收敛") - break - if abs(xk-x) < err: - return xk - i = xk-x - x = xk - except Exception as e: - print(f"发生错误: {e}") - return None + while True: + xk = fd(x) + if complex(xk).imag != 0: + break + if (i) < abs(xk-x): + count += 1 + else: + count = 0 + if count > 10: + break + if abs(xk-x) < err: + return xk + i = abs(xk-x) + x = xk return None if __name__ == "__main__": x0 = 1.5 - result = Renew(x0, fd1, 1e-5) + result = diedai(x0, f1, 1e-5) if result is not None: - print(f"1式收敛 解为: {result:.5f}") + print(f"(1)收敛 解为: {result:.5f}") else: - print("1式不收敛") + print("(1)不收敛") - result = Renew(x0, fd2, 1e-5) + result = diedai(x0, f2, 1e-5) if result is not None: - print(f"2式收敛 解为: {result:.5f}") + print(f"(2)收敛 解为: {result:.5f}") else: - print("2式不收敛") + print("(2)不收敛")