数值分析正割法python3实现并绘图

import numpy as np
import matplotlib.pyplot as plt

print("题目:用正割法求x^3-x-1=0在x=1.5的一个根")
print("---------------------------------------")
i = 0
def result(x):
    y = (x**3-x-1)/(3*x**2-1)
    return x-y

def Y(x):
    global i
    i = i+1
    plt.plot([x, x], [0, (x**3-x-1)])
    plt.plot([x, result(x)], [(x**3-x-1), 0])
    temp = round(x-result(x),5)
    if(temp== 0.0):
        print('正割法第',i,'次')
        print('解得:',round(x,5))
        x = result(x)
        y = (result(x)**3- result(x) - 1)
        plt.plot(x, y, ".")
        plt.plot(x,y,"g-")
        plt.annotate("(1.32472,1.32472)", xy=(result(x), (result(x)**3- result(x) - 1)),
                     xytext=(result(x) - 0.5, (result(x)**3- result(x) - 1) +2), color='k', fontsize=10)
    else:
        Y(result(x))

Y(2.7)

x = 0
plt.title("secant    method")
x = np.linspace(0,3)
plt.xlim(0,3)# 固定坐标
plt.ylim(-5,20)
plt.plot(x,x**3-x-1,"b-")
plt.grid(True)
plt.plot([0,3],[0,0],"--")
plt.show()

正割法是近似的牛顿切线法,把求导用斜率代替。
用切线不断逼近函数的单根

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值