python 梯度法求解函数极值的实例
作者:咚咚怪 发布时间:2023-08-11 07:33:05
标签:python,梯度,函数,极值
如下所示:
#coding utf-8
a=0.001 #定义收敛步长
xd=1 #定义寻找步长
x=0 #定义一个种子x0
i=0 #循环迭代次数
y=0
dic={}
import math
def f(x):
y=math.sin(x) #定义函数f(X)=sinx
return y
def fd(x):
y=math.cos(x) #函数f(x)导数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)
来源:https://blog.csdn.net/weixin_42733218/article/details/81119880
0
投稿
猜你喜欢
- 首先是只有一个change事件changelevel()//选择值若想改变select同时改变row里的值多个事件用;分割开来此时发现cha
- Static files管理static files指一些用到的像css,javascript,images之类的文件。在开发阶段:1.在s
- 今天要做个搜索,用到了时间比较函数!以前多是直接比较时间,查询语句有长效率又不高~~~后来用了DateDiff函数,在调试的时候发现了一些问
- 一:模版的继承1.什么是模板继承?你需要事先在你想要使用的主页面上划定区域做好标记,之后在子页面继承的时候你就可以使用在主页面划定的区域,也
- 看代码吧~package mainimport ("fmt""io""net/http&q
- Vigenere 加密算法该密码由意大利密码学家 Giovan Battista Bellaso 于 1553 年发明,但几个世纪以来一直归
- 简介这是实验室2018年底招新时的考核题目,使用Python编写一个能够完成基本对战的五子棋游戏。面向新手。程序主要包括两个部分,图形创建与
- 现在大家学习python掌握内容了解太多太多,但是最重要的不是掌握了解算法的使用,而是了解算法原理远比使用算法命令更重要,现在大家了解算法应
- 主要需要pd.ExcelWriter([文件路径])方法参考官方文档:>>> writer = pd.ExcelWrite
- 本文实例为大家分享了JavaScript实现动态生成表格的具体代码,供大家参考,具体内容如下功能描述在输入框中输入行和列,点击按钮,生成拥有
- 在javascript中,null>=0 为真,null==0却为假,null的值详解1.前言今天看见朋友们在讨论一个问题,说 null 到
- 字符替换是一个非常重要的功能,javascript 中有一个 String.replace( ),但是此方法有
- 获取评论贴的请求头与表单数据下一篇在这里这里,我们随便选取一个网站,获取该贴评论后的请求头,表单数据以及评论贴链接。(因为涉及敏感信息,自己
- 本文主要介绍 SQLServerExpress2008不用第三方工具调试T-SQL语句,经过本文的介绍,用SQLSERVER2008 Man
- dictPython内置了字典:dict的支持,dict全称dictionary,在其他语言种也称为map,使用键-值(key-value)
- 该脚本是为了结合之前的编写的脚本,来实现数据的比对模块,实现数据的自动化!由于数据格式是定死的,该代码只做参考,有什么问题可以私信我!CSV
- 本文实例讲述了Python基于Tkinter实现的记事本。分享给大家供大家参考。具体如下:from Tkinter import *root
- 如下所示:# 计算0到100之间的之间所有的数字累计求和结果# 定义最终结果的变量result = 0# 定义一个整数的变量来记录循环次数i
- 今天使用import pandasusers = pandas.read_csv("H:\python\data analysis
- 前言在任何编程语言中,代码需要根据不同的条件在给定的输入中做不同的决定和执行相应的动作。例如,在一个游戏中,如果玩家生命点为0,游戏结束。在