当前位置:   article > 正文

数值分析定点迭代python3实现加绘图_定点数迭代算法

定点数迭代算法

开放法:定点迭代实现
将函数方程式化位两个等式,模拟两个函数的交点,给定点,(函数的导数小于0),逐次逼近。

# from matplotlib import pyplot as plot
import numpy
import matplotlib.pyplot as plt
from matplotlib import animation  #动画包

print("题目:用定点迭代法求x^3-x-1=0在x=1.5附近的一个根")
i = 0
def result(x):
    y = (x+1)**(1/3)
    return y

def Y(x):
    global i #global关键字全局变量修改必须声明
    i = i+1
   # updata(x)
    plt.plot([x, x], [0, result(x)])
    plt.plot([x,result(x)],[result(x),result(x)])# 1.5,result(1.5)   1.5,1.5
    if((x-result(x))<0.000001):
        print('逐次逼近第', i, '后求得:')
        print('函数在x=1.5附近的一个根为',round(x,5))# 输出字符方式
    else:
        Y(result(x))

Y(1.5)

x = 0
# plt.plot([0,0.2,0.4,0.6,0.8,1.0,1.2,1.4,1.6,1.8,2.0],[0,0.2,0.4,0.6,0.8,1.0,1.2,1.4,1.6,1.8,2.0])
plt.title("fixed point iteration method")
x = numpy.linspace(1.3,1.6)
y = numpy.linspace(1.3,1.6)
plt.xlim(1.3,1.6)# 固定坐标
plt.ylim(1.3,1.6)
plt.plot(x,x)
plt.plot(x,(x+1)**(1/3),"r-")
plt.grid(True)# 设置网格线
x = 1.5
# plt.plot([x,x],[0,result(x)])
##plt.vlines(0,1,result(1.5),linestyles = "dashed")
# fig1 = plt.figure()# 建立子图
# def updata(data):
   # plt.plot([x, x], [0, result(x)])
    #plt.plot([x, result(x)], [result(x), result(x)])  # 1.5,result(1.5)   1.5,1.5
    #fig_points.set_data(data[:, 0:num])
   # return line
#fig_points, = plt.plot([], [])#'ro'是画点 不加是连线
#anim = animation.FuncAnimation(fig1, updata)#结束条件
plt.show()

#plt.vlines(0,1,result(1.5),linestyles = "dashed")


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

这里写图片描述

结果:
题目:用定点迭代法求x^3-x-1=0在x=1.5附近的一个根
逐次逼近第 9 后求得:
函数在x=1.5附近的一个根为 1.32472
  • 1
  • 2
  • 3
  • 4
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/937314
推荐阅读
相关标签
  

闽ICP备14008679号