問6
def question6(f, a, b, e = 0.00001):
if f(a) * f(b) >0:
return False
while abs(f(a) - f(b)) > e:
c = (a + b) / 2
if f(c) == 0:
return c
elif f(a) * f(c) >0:
a = c
elif f(a) * f(c)<0:
b = c
return (a + b)/2
import math
def f(x):
return math.exp(x)-3*x
question6(f,0,1)
0.6190605163574219
def g(x):
return math.tanh(x) + x + 2
question6(g,0,1)
False
question6(g,-10,1)
-1.1743409633636475