弦截法
应用弦截法求解方程在区间内的数值解,且其中有误差要求。
import numpy as np
import matplotlib.pyplot as plt
def f(x):
return x*3-x-1
k = 1
height = []
length = []
def cutline(a,b,eps):
global k
ans = b - (b-a)f(b)/(f(b)-f(a))
height.append(ans)
length.append(k)
if abs(b-a) >= eps:
a = b
b = ans
k += 1
cutline(a,b,eps)
else:
print(ans,k)
plt.plot(length,height)
cutline(1, 1.5, 0.00000001)