Python3实现
#!/usr/bin/python3
print("接下来是表演二分法求方程根的时候了")
print("x^3-5*x-1=0")
print("========================华丽的分割线============================")
a=0.0
b=3.0
n=1
x=(a+b)*0.5
print("第",n,"次二分,x=",x)
while ((x-a)>0.000000001):
fx=x**3-5*x-1
if fx>0:
b=x
else:
a=x
x=(a+b)/2
n=n+1
print("第",n,"次二分,x=",x)
print("==============误差已经控制在百万分之1内==============")
print("一共经历了",n,"次二分,得到方程的根:x=",x)
fx=x**3-5*x-1
print("验算x^3-5*x-1=%f"%(fx))
|