def Dic(a,b):
fa=fun(a)
fb=fun(b)
while a<=b:
mid=(a+b)/2
fm=fun(mid)
if abs(fm)<10e-9:
print("方程的根为",mid)
break
if fa*fm<0:
a=a
b=mid
fb=fun(b)
print("解在[",a,",",b,"]之间")
elif fb*fm<0:
a=mid
b=b
fa=fun(a)
print("解在[",a,",",b,"]之间")
return mid
def fun(x):
return pow(x,3)-x-1 #pow(x,3)表示x的3次幂
a=1
b=1.5
Dic(a,b)
例题来自:李庆扬《数值分析》第五版