defbisect(a,b,tol): if f(a)*f(b)>0: print("输入的a和b不满足要求") else: fa = f(a) fb = f(b) while (b-a)/2>tol: c = (b+a)/2 fc = f(c) if fc == 0: break else: if fc*fa<0: b = c fb = fc else: a = c fa = fc xc = (a+b)/2 return xc print(bisect(0,1,0.0005))
不动点迭代
当对任意数\(x\)循环求解\(\cos\),最终结果会趋向于一个值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import numpy as np import matplotlib.pyplot as plt
defcycle_solver_cos(x,times): X = np.zeros(times) I = np.zeros(times) for i inrange(times): x = np.cos(x) X[i] = x I[i] = i plt.plot(I,X,'o-') plt.axhline(y=0.7390851332,c='r',ls='--') plt.show() cycle_solver_cos(30,28)