1 # -*- coding: utf-8 -*- 2 #coding=utf-8 3 import numpy as np 4 from sympy import * 5 import math 6 import matplotlib.pyplot as plt 7 plt.close() 8 fig = plt.figure() 9 #网格可见 10 plt.grid(True) 11 plt.axis([0, 2.5, 0, 3]) 12 #开启交互 13 plt.ion() 14 # plt.title("迭代法") 15 #设置xy轴的名称 16 plt.xlabel("X") 17 plt.ylabel("Y") 18 19 20 x1 = np.linspace(0, 10, 100) 21 y1 = (2*x1)**0.5 22 x2 = np.linspace(0, 10, 100) 23 y2 = x2 24 plt.plot(x1, y1, label="$f$", color="green", linewidth=1) 25 plt.plot(x2, y2, label="$g$", color="blue", linewidth=1) 26 plt.show() 27 28 #定义变量x 29 x = Symbol(‘x‘) 30 31 #定义函数f 32 f = (2*x)**0.5 33 g = x 34 i=0.1 35 while(1): 36 f1 = f.subs(x, i) 37 f2 = i 38 print("值为:",f1) 39 plt.plot([i,(2*i) ** 0.5],[(2*i) ** 0.5,(2*i) ** 0.5]) 40 plt.plot([i, i], [i,(2*i) ** 0.5]) 41 42 if (abs(i - f1 )<0.000001): 43 break 44 i=f1 45 plt.pause(0.5) 46 show_res = ‘[x=‘ + str(i) + ‘]‘ 47 plt.text(1,1, show_res) 48 while True: 49 plt.pause(0.05)