如何用二分法递归求x的七次方+x-3=0的根写了一个源码但是不知道哪错了麻烦各位大佬教教我
a = input("请输入函数表达式")
x1 = input("请输入左端点范围")
x2 = input("请输入右端点范围")
x1 =float(x1)
x2 = float(x2)
t = 0
def f(a):
return a
LIMIT =10 ** -7
while x2 - x1 > LIMIT:
t = t + 1
mid = (x1 + x2 ) / 2
y = f(mid)
if y > 0:
x1 = mid
elif y < 0:
x2 = mid
print("计算次数",t,(x1 + x2) / 2)
a = input("请输入函数表达式")
x1 = input("请输入左端点范围")
x2 = input("请输入右端点范围")
x1 =float(x1)
x2 = float(x2)
t = 0
def f(a):
return a
LIMIT =10 ** -7
while x2 - x1 > LIMIT:
t = t + 1
mid = (x1 + x2 ) / 2
y = f(mid)
if y > 0:
x1 = mid
elif y < 0:
x2 = mid
print("计算次数",t,(x1 + x2) / 2)