+ 2
Why it doesn't work? (the solution of the transcendental equations by various methods)
import math def f(x): fun = 4 * math.cos(x) + 0.3 * x return fun #a, b, h = [float(i) for i in input().split()] a = float (input()) b = float (input()) h = float (input()) e = 0.0000001 while a < b: z = a + h if f(a) * f(z) < 0: z_0 = z a_0 = a while z_0 - a_0 > e: x_0 = (a_0 + z_0) / 2 if f(a_0) * f(z_0): z_0 = x_0 else: a_0 = x_0 print(x_0)
1 Odpowiedź
+ 3
"while a<b:"
a always remains less than b. So we get an infinite loop. Initiate z with a, and set the condition as "while z < b:". Increment z by h inside the loop.
Also, I think the second if condition should be
if f(a_0) * f(x_0) < 0:
That should fix things. Let me know. 😊