The method of gold segmentation in Python

Source: Internet
Author: User
Tags scalar in python

This article mainly introduces the implementation of the Golden section in Python, involving techniques related to the calculation of Python's mathematics, the need for friends can refer to the

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the ; > "' 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 and a step length h. x,fmin = Search (f,a,b,tol=1.0e-6) Golden section method for D Etermining x that minimizes the user-supplied scalar function f (x). The minimum must is 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)) # Eq. (10.4) R = 0.618033989 C = 1.0-r # I 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

I hope this article will help you with your Python programming.

Note < > : More Wonderful tutorials please focus on Triple programming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.