Extremum point golden section

We start to understand what is the golden section algorithm:

    Also known as the Golden Section 0.618 algorithms belong to the interval contraction method, first find the initial search interval contains minimum point, then golden point by comparing the value of the function of narrowing the search space (of course, to ensure that the minimum point in the range of search elements ), when the length of the domain of the reduced length when in place of the minimum point can be approximated by the average of the current interval inclusive.

Note: The scope function is a single valley (there is only one maximum value (converted to the required minimum value problem) or minimum points)

Popular speak is to say give a given search interval, such as [ab], and the other X . 1 = A + 0.382 (B - A), X 2  = A + 0.618 (B - A), then X . 1 and X 2 are substituted into the function f (x) Comparative F (X . 1 ) and F (X 2 ) the size, if F (X . 1 )> F (X 2 ), then let a = X . 1 , or B = X 2 , and then the new within the search interval [ab &], rediscovered X . 1 and X 2 process is repeated until the b - average a <ξ (this is the minimum precision analysis), and then take a, b approximation instead of F (X) min .

Code is implemented as follows:

 1 import numpy as np
 2 import matplotlib.pyplot as plt
 3 import time as tm
 4 
 5 x = np.random.rand(100)
 6 x.sort()
 7 plt.figure()
 8 plt.xlabel("t")
 9 plt.ylabel("y")
10 plt.title("0.618")
11 plt.plot(x,x*x - x + 1)
12 plt.show()
13 
14 def fun(x):
15     y = x*x - x + 1
16     return y
17 def digu(left,right,fun,dis):
18     a = left
19     b = right
20     while True:
21         c = a + 0.382*(b - a)
22         d = a + 0.618*(b - a)
23         if(abs(c-d) < dis):
24             return (c+d)/2
25         else:
26             if((fun(c) - fun(d)) > 0):
27                 a = c
28             else:
29                 b = d
30 start = tm.perf_counter()
31 print(digu(-2000,1000.,fun,0.002))
32 end = tm.perf_counter()
33 print(end-start)

operation result:

0.4980379375572119
0.0003180240000002499

Guess you like

Origin www.cnblogs.com/The-Shining/p/11567128.html