From 2bf2c0bb85ccf702a852b5c2ac1de4d58b05698f Mon Sep 17 00:00:00 2001 From: lhye200 Date: Tue, 29 Apr 2025 22:50:45 +0800 Subject: [PATCH] 20250429 --- 120-5.py | 10 +--------- 159-2.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/120-5.py b/120-5.py index c4e37fe..03eafa2 100644 --- a/120-5.py +++ b/120-5.py @@ -5,17 +5,9 @@ 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 = 0 - t10 = 0 - t11 = 0 - t20 = 0 - t21 = 0 - t30 = 0 - t31 = 0 + t01 = t10 = t11 = t20 = t21 = t30 = t31 = 0 k = 1 while True: tmp = 0 diff --git a/159-2.py b/159-2.py index e69de29..17ff624 100644 --- a/159-2.py +++ b/159-2.py @@ -0,0 +1,34 @@ +import math +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] + 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') + 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)") + plt.xlabel("x") + plt.ylabel("f(x)") + plt.show() + return x, y + + +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