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自定义主从分布式架构实例分析
Sep 19 Python
Python中装饰器学习总结
Feb 10 Python
Python实现去除列表中重复元素的方法小结【4种方法】
Apr 27 Python
python 获取当天每个准点时间戳的实例
May 22 Python
详解Python解决抓取内容乱码问题(decode和encode解码)
Mar 29 Python
Django框架表单操作实例分析
Nov 04 Python
自定义Django Form中choicefield下拉菜单选取数据库内容实例
Mar 13 Python
Python Django view 两种return的实现方式
Mar 16 Python
python logging.info在终端没输出的解决
May 12 Python
Python 整行读取文本方法并去掉readlines换行\n操作
Sep 03 Python
Python学习之time模块的基本使用
Jan 17 Python
PyQt5实现多张图片显示并滚动
Jun 11 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
落伍首发 php+mysql 采用ajax技术的 省 市 地 3级联动无刷新菜单 源码
2006/12/16 PHP
PHP utf-8编码问题,utf8编码,数据库乱码,页面显示输出乱码
2013/04/08 PHP
PHP在线生成二维码(google api)的实现代码详解
2013/06/04 PHP
从PHP $_SERVER相关参数判断是否支持Rewrite模块
2013/09/26 PHP
PHP的变量类型和作用域详解
2014/03/12 PHP
PHP反射机制用法实例
2014/08/28 PHP
PHP 反射(Reflection)使用实例
2015/05/12 PHP
php时间计算相关问题小结
2016/05/09 PHP
php json转换相关知识(小结)
2018/12/21 PHP
js常见表单应用技巧
2008/01/09 Javascript
csdn 博客中实现运行代码功能实现
2009/08/29 Javascript
jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮
2013/10/11 Javascript
浏览器的JavaScript引擎的识别方法
2013/10/20 Javascript
JS获取键盘上任意按键的值(实例代码)
2013/11/12 Javascript
js实现飞入星星特效代码
2014/10/17 Javascript
jQuery多个版本和其他js库冲突的解决方法
2016/08/11 Javascript
详解vue-cli项目中怎么使用mock数据
2018/05/29 Javascript
详解小程序原生使用ES7 async/await语法
2018/08/06 Javascript
JS集合set类的实现与使用方法示例
2019/02/01 Javascript
JavaScript Math对象和调试程序的方法分析
2019/05/13 Javascript
Vue项目结合Vue-layer实现弹框式编辑功能(实例代码)
2020/03/11 Javascript
Python中列表的一些基本操作知识汇总
2015/05/20 Python
详解Python中的from..import绝对导入语句
2016/06/21 Python
ubuntu环境下python虚拟环境的安装过程
2018/01/07 Python
解决django FileFIELD的编码问题
2020/03/30 Python
Python Selenium模块安装使用教程详解
2020/07/09 Python
一款利用html5和css3实现的3D滚动特效的教程
2015/01/04 HTML / CSS
大专生自我鉴定范文
2013/10/01 职场文书
优秀员工个人的自我评价
2013/11/29 职场文书
社会调查研究计划书
2014/05/01 职场文书
本科毕业生自荐信
2014/06/02 职场文书
环保标语口号
2014/06/13 职场文书
委托书的写法
2014/08/30 职场文书
教师党员个人自我剖析材料
2014/09/29 职场文书
2016年春季运动会加油稿
2015/07/22 职场文书
创业计划书之网吧
2019/10/10 职场文书