-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuebung8 - Copy.py
More file actions
74 lines (49 loc) · 1.49 KB
/
uebung8 - Copy.py
File metadata and controls
74 lines (49 loc) · 1.49 KB
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
import matplotlib.pyplot as mp
import numpy as np
import math
phiPercent = 2 / (3 + math.sqrt(5))
def FToAnalyze(x):
return (x+1/3)**2
def printSomething():
fig = mp.figure()
ax = fig.add_subplot(1,1,1)
x_values = list(range(-10,10))
ax.plot (x_values, [FToAnalyze(i) for i in x_values], x_values, [(-FToAnalyze(i)) for i in x_values])
ax.legend(['f(x)=(x+1/3)^2', 'f(x)=(x+1/3)^2'])
ax.set_xlabel('X Value')
ax.set_ylabel('f(x)')
mp.show()
def printSomethingUseful(func, interval, minima):
fig = mp.figure()
ax = fig.add_subplot(1, 1, 1)
x_values = np.linspace(interval[0], interval[1], 100)
ax.plot(x_values , [func(x) for x in x_values])
ax.plot(minima[0], minima[1], 'ro')
ax.annotate(str(minima[0]),(minima[0], minima[1]))
mp.show()
def goldenSectionFAP(func, interval):
count = 0
left = interval[0]
right = interval[1]
ml = 0.0
mr = 0.0
while (right - left) > 0.0001:
a = (right - left) * phiPercent
ml = func(left + a)
mr = func(right - a)
print("a " + str(a) + " ml " + str(ml) + " mr " + str(mr))
count += 1
if ml < mr:
right = right - a
else:
left = left + a
# print(str(right - left))
printSomethingUseful(func, interval, (right - (0.5*(right-left)), mr - (0.5*(mr-ml))))
return left, right
if __name__ == "__main__":
# Which function shall be used, which interval shall be checked?
funcToUse = FToAnalyze
position = (-30, 30)
# Do it!
goldenSectionFAP(funcToUse, position)
# -------------------------------------------------------------- #