python中黄金分割法实现方法


Posted in Python onMay 06, 2015

本文实例讲述了python中黄金分割法实现方法。分享给大家供大家参考。具体实现方法如下:

''' a,b = bracket(f,xStart,h)
  Finds the brackets (a,b) of a minimum point of the
  user-supplied scalar function f(x).
  The search starts downhill from xStart with a step
  length h.
  x,fMin = search(f,a,b,tol=1.0e-6)
  Golden section method for determining x that minimizes
  the user-supplied scalar function f(x).
  The minimum must be bracketed in (a,b).
'''    
from math import log, ceil
def bracket(f,x1,h):
  c = 1.618033989 
  f1 = f(x1)
  x2 = x1 + h; f2 = f(x2)
 # Determine downhill direction and change sign of h if needed
  if f2 > f1:
    h = -h
    x2 = x1 + h; f2 = f(x2)
   # Check if minimum between x1 - h and x1 + h
    if f2 > f1: return x2,x1 - h 
 # Search loop
  for i in range (100):  
    h = c*h
    x3 = x2 + h; f3 = f(x3)
    if f3 > f2: return x1,x3
    x1 = x2; x2 = x3
    f1 = f2; f2 = f3
  print "Bracket did not find a mimimum"    
def search(f,a,b,tol=1.0e-9):
  nIter = int(ceil(-2.078087*log(tol/abs(b-a)))) # Eq. (10.4)
  R = 0.618033989
  C = 1.0 - R
 # First telescoping
  x1 = R*a + C*b; x2 = C*a + R*b
  f1 = f(x1); f2 = f(x2)
 # Main loop
  for i in range(nIter):
    if f1 > f2:
      a = x1
      x1 = x2; f1 = f2
      x2 = C*a + R*b; f2 = f(x2)
    else:
      b = x2
      x2 = x1; f2 = f1
      x1 = R*a + C*b; f1 = f(x1)
  if f1 < f2: return x1,f1
  else: return x2,f2

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
测试、预发布后用python检测网页是否有日常链接
Jun 03 Python
简单的python后台管理程序
Apr 13 Python
Python中的pygal安装和绘制直方图代码分享
Dec 08 Python
Python通过属性手段实现只允许调用一次的示例讲解
Apr 21 Python
Python3.7实现中控考勤机自动连接
Aug 28 Python
python模拟登陆,用session维持回话的实例
Dec 27 Python
Python中判断子串存在的性能比较及分析总结
Jun 23 Python
如何在sublime编辑器中安装python
May 20 Python
python如何导入依赖包
Jul 13 Python
利用Python中的Xpath实现一个在线汇率转换器
Sep 09 Python
Pycharm Available Package无法显示/安装包的问题Error Loading Package List解决
Sep 18 Python
Pandas-DataFrame知识点汇总
Mar 16 Python
使用rpclib进行Python网络编程时的注释问题
May 06 #Python
pymongo给mongodb创建索引的简单实现方法
May 06 #Python
Python中用PIL库批量给图片加上序号的教程
May 06 #Python
python写入中英文字符串到文件的方法
May 06 #Python
python使用xlrd模块读写Excel文件的方法
May 06 #Python
在Python中使用全局日志时需要注意的问题
May 06 #Python
python通过post提交数据的方法
May 06 #Python
You might like
Ajax PHP分页演示
2007/01/02 PHP
Windows下利用Gvim写PHP产生中文乱码问题解决方法
2011/04/20 PHP
php查询mysql数据库并将结果保存到数组的方法
2015/03/18 PHP
php实现按天数、星期、月份查询的搜索框
2016/05/02 PHP
php添加数据到xml文件的简单例子
2016/09/08 PHP
PHP判断数组是否为空的常用方法(五种方法)
2017/02/08 PHP
JavaScript中的集合及效率
2010/01/08 Javascript
JS、CSS加载中的小问题探讨
2013/11/26 Javascript
jQuery简单实现网页选项卡特效
2014/11/24 Javascript
2种jQuery 实现刮刮卡效果
2015/02/01 Javascript
JavaScript中getUTCSeconds()方法的使用详解
2015/06/11 Javascript
JavaScript编写连连看小游戏
2015/07/07 Javascript
JS实现仿苹果底部任务栏菜单效果代码
2015/08/28 Javascript
JS学习之表格的排序简单实例
2016/05/16 Javascript
jQuery图片轮播插件——前端开发必看
2016/05/31 Javascript
flexible.js实现移动端rem适配方案
2020/04/07 Javascript
[00:12]2018DOTA2亚洲邀请赛SOLO赛 MidOne是否中单第一人?
2018/04/05 DOTA
在主机商的共享服务器上部署Django站点的方法
2015/07/22 Python
python函数中return后的语句一定不会执行吗?
2017/07/06 Python
Python文件和流(实例讲解)
2017/09/12 Python
python定时关机小脚本
2018/06/20 Python
Python测试网络连通性示例【基于ping】
2018/08/03 Python
python列表使用实现名字管理系统
2019/01/30 Python
python 协程中的迭代器,生成器原理及应用实例详解
2019/10/28 Python
Python程序暂停的正常处理方法
2019/11/07 Python
Java文件与类动手动脑实例详解
2019/11/10 Python
Tensorflow 自定义loss的情况下初始化部分变量方式
2020/01/06 Python
Python3 读取Word文件方式
2020/02/13 Python
美国最大的网上冲印店:Shutterfly
2017/01/01 全球购物
戴尔马来西亚官网:Dell Malaysia
2020/05/02 全球购物
科级干部考察材料
2014/02/15 职场文书
机关出纳岗位职责
2014/04/03 职场文书
《孙权劝学》教学反思
2014/04/23 职场文书
统计员岗位职责范本
2015/04/14 职场文书
遗嘱范文
2015/08/07 职场文书
《分数乘法》教学反思
2016/02/24 职场文书