欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python 梯度法求解函數(shù)極值的實(shí)例

 更新時(shí)間:2019年07月10日 08:42:40   作者:咚咚怪  
今天小編就為大家分享一篇python 梯度法求解函數(shù)極值的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

如下所示:

#coding utf-8
a=0.001    #定義收斂步長(zhǎng)
xd=1    #定義尋找步長(zhǎng)
x=0     #定義一個(gè)種子x0
i=0     #循環(huán)迭代次數(shù)
y=0
dic={}
import math
def f(x):
 y=math.sin(x) #定義函數(shù)f(X)=sinx
 return y
def fd(x):
 y=math.cos(x) #函數(shù)f(x)導(dǎo)數(shù)fd(X)=cosx
 return y
while y>=0 and y<3.14*4:
 y=y+xd
 x=y
 while abs(fd(x))>0.001: #定義精度為0.001
  x=x+a*fd(x)
 if x>=0 and x<3.14*4:
  print(x,f(x))
  dic[y]=x
print(dic)
ls=[]
for i in dic.keys():
 cor=0
 if ls is None:
  ls.append(dic[i])
 else:
  for j in ls:
   if dic[i]-j<0.1:
    cor=1
    break
  if cor==0:
   ls.append(dic[i])
print(ls)

以上這篇python 梯度法求解函數(shù)極值的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論