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常规方法实现数组的全排列
Mar 17 Python
python实现的简单猜数字游戏
Apr 04 Python
Scrapy框架CrawlSpiders的介绍以及使用详解
Nov 29 Python
python 格式化输出百分号的方法
Jan 20 Python
Python 生成器,迭代,yield关键字,send()传参给yield语句操作示例
Oct 12 Python
Python Numpy 自然数填充数组的实现
Nov 28 Python
Python文件操作方法详解
Feb 09 Python
Python实现多线程下载脚本的示例代码
Apr 03 Python
Python调用OpenCV实现图像平滑代码实例
Jun 19 Python
python实现一个简单RPC框架的示例
Oct 28 Python
一文读懂python Scrapy爬虫框架
Feb 24 Python
pytorch中的 .view()函数的用法介绍
Mar 17 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 开发的 8 个技巧
2007/01/02 PHP
一个经典实用的PHP图像处理类分享
2014/11/18 PHP
试用php中oci8扩展
2015/06/18 PHP
百万级别知乎用户数据抓取与分析之PHP开发
2015/09/28 PHP
php获取微信共享收货地址的方法
2017/12/21 PHP
详解PHP实现支付宝小程序用户授权的工具类
2018/12/25 PHP
JS控件autocomplete 0.11演示及下载 1月5日已更新
2007/01/09 Javascript
做网页的一些技巧(续)
2007/02/01 Javascript
JavaScript DOM 学习第二章 编辑文本
2010/02/19 Javascript
跨浏览器开发经验总结(四) 怎么写入剪贴板
2010/05/13 Javascript
(跨浏览器基础事件/浏览器检测/判断浏览器)经验代码分享
2013/01/24 Javascript
JQuery为textarea添加maxlength属性并且兼容IE
2013/04/25 Javascript
jquery按回车提交数据的代码示例
2013/11/05 Javascript
Jquery使用val方法读写value值
2015/05/18 Javascript
自己动手写的javascript前端等待控件
2015/10/30 Javascript
Vue.js实现简单ToDoList 前期准备(一)
2016/12/01 Javascript
js实现九宫格的随机颜色跳转
2017/02/19 Javascript
浅谈struts1 &amp; jquery form 文件异步上传
2017/05/25 jQuery
python3访问sina首页中文的处理方法
2014/02/24 Python
Python中给List添加元素的4种方法分享
2014/11/28 Python
Python多线程实现同步的四种方式
2017/05/02 Python
Python 错误和异常代码详解
2018/01/29 Python
利用numpy和pandas处理csv文件中的时间方法
2018/04/19 Python
python逆序打印各位数字的方法
2018/06/25 Python
Sanic框架蓝图用法实例分析
2018/07/17 Python
浅谈python的dataframe与series的创建方法
2018/11/12 Python
浅谈tensorflow中Dataset图片的批量读取及维度的操作详解
2020/01/20 Python
Python能做什么
2020/06/02 Python
英国著名国际平价时尚男装品牌:Topman
2016/08/27 全球购物
您熟悉ORM(Object-Relation Mapping)吗?请谈谈您所理解的ORM
2016/02/08 面试题
机电系毕业生求职信
2014/07/11 职场文书
工作检讨书范文
2015/01/23 职场文书
个人总结与自我评价2015
2015/03/11 职场文书
2016年“5.12”护士节致辞
2015/07/31 职场文书
员工规章制度范本
2015/08/07 职场文书
Node与Python 双向通信的实现代码
2021/07/16 Javascript