非线性方程算法

二分法

应用二分法求解方程x^3-x-1=0在区间[1,1.5]内的数值解,要[1,1.5]求绝对误差小于10^{-8}

import numpy as np
import matplotlib.pyplot as plt

def f(x):
return x3 - x - 1
def HALF(a,b,epsilon):
k = 1 + int((np.log(b-a) - np.log(2
epsilon)) / (np.log(2)))
ans = (a+b)/2
height = []
length = []
for i in range(1, k+1):
if f(ans) == 0 :
break
elif f(a)
f(ans) < 0 :
b = ans
else:
a = ans
height.append(ans)
length.append(i)
ans = (a+b)/2
k += 1
plt.plot(length, height)
print(ans)

HALF(1, 1.5, 0.00000001)


二分法.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。