python解高次方程的解法_Python退火算法在高次方程的应用

@font-face{ font-family:"Times New Roman"; } @font-face{ font-family:"宋体"; } @font-face{ font-family:"Calibri"; } p.MsoNormal{ mso-style-name:正文; mso-style-parent:""; margin:0pt; margin-bottom:.0001pt; mso-pagination:none; text-align:justify; text-justify:inter-ideograph; font-family:Calibri; mso-fareast-font-family:宋体; mso-bidi-font-family:'Times New Roman'; font-size:10.5000pt; mso-font-kerning:1.0000pt; } p.p{ mso-style-name:"普通\(网站\)"; margin-top:5.0000pt; margin-right:0.0000pt; margin-bottom:5.0000pt; margin-left:0.0000pt; mso-margin-top-alt:auto; mso-margin-bottom-alt:auto; mso-pagination:none; text-align:left; font-family:Calibri; mso-fareast-font-family:宋体; mso-bidi-font-family:'Times New Roman'; font-size:12.0000pt; } span.msoIns{ mso-style-type:export-only; mso-style-name:""; text-decoration:underline; text-underline:single; color:blue; } span.msoDel{ mso-style-type:export-only; mso-style-name:""; text-decoration:line-through; color:red; } @page{mso-page-border-surround-header:no; mso-page-border-surround-footer:no;}@page Section0{ } div.Section0{page:Section0;}

退火算法就是钢铁在淬炼过程中失温而成稳定态时的过程,热力学上温度(内能)越高原子态越不稳定。这篇文章主要介绍了Python退火算法在高次方程的应用,需要的朋友可以参考下

文章目录

一,简介

二,计算方程

三,总结

一,简介

退火算法不言而喻,就是钢铁在淬炼过程中失温而成稳定态时的过程,热力学上温度(内能)越高原子态越不稳定,而温度有一个向低温区辐射降温的物理过程,当物质内能不再降低时候该物质原子态逐渐成为稳定有序态,这对我们从随机复杂问题中找出最优解有一定借鉴意义,将这个过程化为算法,具体参见其他资料。

二,计算方程

我们所要计算的方程是f(x) = (x - 2) * (x + 3) * (x + 8) * (x - 9),是一个一元四次方程,我们称为高次方程,当然这个函数的开口是向上的,那么在一个无限长的区间内我们可能找不出最大值点,因此我们尝试在较短区间内解最小值点,我们成为最优解。

解法一:毫无疑问,数学方法多次求导基本可以解出,但是这个过程较复杂,还容易算错,我就不赘述了,读者有时间自己可以尝试解一下。

解法二:这个解法就是暴力解决了,我们这里只求解区间[-10,10]上的最优解,直接随机200个点,再除以10(这样可以得到非整数横坐标),再依此计算其纵坐标f(x),min{f(x)}一下,用list的index方法找出最小值对应位置就行了,然后画出图形大致瞄一瞄。

直接贴代码:

import random

import matplotlib.pyplot as plt

list_x = []

# for i in range(1):

#   #print(random.randint(0,100))

#   for i in range(0,100):

#     print("sss",i)

#   list_x.append(random.randint(0,100))

for i in range(-100,100):

list_x.append(i/10)

print("横坐标为:",list_x)

print(len(list_x))

list_y = []

for x in list_x:

# print(x)

#y = x*x*x - 60*x*x -4*x +6

y = (x - 2) * (x + 3) * (x + 8) * (x - 9)

list_y.append(y)

print("纵坐标为:",list_y)

经验证,这里算出来的结果6.5和最优解1549都是对的

print("最小值为:",min(list_y))

num = min(list_y)

print("最优解:",list_y.index(num)/10)

plt.legend(Swift Codehttps://www.gendan5.com/swift...

print("第",list_y.index(num)/10-10,"个位置取得最小值")

plt.plot(list_x, list_y, label='NM')

plt.plot(x2, y2, label='Second Line')

plt.xlabel('X') #横坐标标题

plt.ylabel('Y') #纵坐标标题

plt.title('Interesting Graph\nCheck it out',loc="right")  #图像标题

plt.title('Interesting Graph\nCheck it out')

plt.legend()  #显示Fisrt Line和Second Line(label)的设置

plt.savefig('C:/Users/zhengyong/Desktop/1.png')

plt.show()

得到如下结果:

那么我们得出最优解的坐标是(6.5,-1549.6875)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值