-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSuccessiveQuadraticEstimationMethod.py
80 lines (65 loc) · 1.61 KB
/
SuccessiveQuadraticEstimationMethod.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Successive Quadratic Estimation Method
import matplotlib.pyplot as plt
import numpy as np
a=0.1 #Lower bound
b=14 #Upper bound
dx=1e-5 # Step size
fcp=1e-8 # Convergence parameter for function evaluation
# Equation to be minimized
def f(x):
# return 2*x**2+16/x
return x**2+54/x
x=np.linspace(a,b,1000)
#Initialization
x1=a
x2=x1+dx
f1=f(x1)
f2=f(x2)
if f1>f2:
x3=x1+2*dx
elif f1<=f2:
x3=x1-dx
f3=f(x3)
# Detemine minimum function value for the initial step
f_min=min({f1,f2,f3})
# Determine x-value corresponding to minimum funtion value
if f_min==f1:
x_min=x1
elif f_min==f2:
x_min=x2
elif f_min==f3:
x_min=x3
# Determine x_bar
a1=(f2-f1)/(x2-x1)
a2=(1/(x3-x2))*(((f3-f1)/(x3-x1))-((f2-f1)/(x2-x1)))
x_bar=0.5*((x1+x2)-(a1/a2))
f_bar=f(x_bar)
while abs(f_min-f_bar)>fcp:
x2=(x_min+x_bar)/2
x1=x2-dx
x3=x1+2*dx
f1=f(x1)
f2=f(x2)
f3=f(x3)
f_min=min({f1,f2,f3})
if f_min==f1:
x_min=x1
elif f_min==f2:
x_min=x2
elif f_min==f3:
x_min=x3
a1=(f2-f1)/(x2-x1)
a2=(1/(x3-x2))*(((f3-f1)/(x3-x1))-((f2-f1)/(x2-x1)))
x_bar=0.5*((x1+x2)-(a1/a2))
f_bar=f(x_bar)
print(f'The approximate minimum function value is {f_min}, at {x_min}')
# Plot the function
plt.plot(x,f(x))
plt.xlabel("x",fontweight='bold')
plt.ylabel("f(x)",fontweight='bold')
plt.grid(which='major',axis='both',linestyle='dashed')
plt.title('Exhaustive Search Method',fontweight='bold')
plt.scatter(x_min,f_min, color='red', label='Approximate Minimum Point')
plt.legend()
plt.savefig('Successive Quadratic Estimation Method.png',dpi=300)
plt.show()