数值分析定点迭代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")



这里写图片描述

结果:
题目:用定点迭代法求x^3-x-1=0在x=1.5附近的一个根
逐次逼近第 9 后求得:
函数在x=1.5附近的一个根为 1.32472
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值