-
Notifications
You must be signed in to change notification settings - Fork 3
/
practical2.jl
140 lines (100 loc) · 2.51 KB
/
practical2.jl
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#Practical 2 :- Implement Fibonacci and Golden section search.
f(x) = x^4 - 14*x^3 + 60x^2 - 70*x
function fbs(f, a, b, N, ϵ=0.01)
function F(n)
# We choose F₀=1, F₁=1, F₂=2, F₃=3, ...
ϕ = (1 + √5) / 2
Fₙ = (ϕ^(n + 1) - (1 - ϕ)^(n + 1)) / √5
return Fₙ
end
ρ = 1 - F(N) / F(N + 1)
d = (1 - ρ) * b + ρ * a
fd = f(d)
k = 1
while k <= N
c = ρ * b + (1 - ρ) * a
fc = f(c)
if (fc < fd)
d, b, fd = c, d, fc
else
a, b = b, c
end
k = k + 1
if k == N
ρ = 0.50 - ϵ
else
ρ = 1 - F(N - k + 1) / F(N - k + 2)
end
end
return a < b ? (a, b) : (b, a)
end
# function call
fibo = fbs(f, 0, 2, 4, 0.05)
println(fibo)
# Golden search
function gss(f, a, b, N)
# The golden ratio
ϕ = (sqrt(5) - 1)/2.0
ρ = 1-ϕ
d = (1-ρ)*b + ρ*a
fd = f(d)
for i in 1:N
c = ρ*b + (1-ρ)*a
fc = f(c)
if(fc < fd)
d, b, fd = c, d, fc
else
a, b = b, c
end
end
return a < b ? (a, b) : (b, a)
end
a, b = gss(f, 0.0, 2.0, 10)
# ------------------------------------OR---------------------------------------------------------------
function fibonacci_search(f, a, b, n; ϵ=0.01)
s = (1 - √5) / (1 + √5)
ρ = 1 / (1.618 * (1 - s^(n + 1)) / (1 - s^n))
d = ρ * b + (1 - ρ) * a
yd = f(d)
for i in 1:n-1
print(a)
print("\n")
print(b)
print("\n")
if i == n - 1
c = ϵ * a + (1 - ϵ) * d
else
c = ρ * a + (1 - ρ) * b
end
yc = f(c)
if yc < yd
b, d, yd = d, c, yc
else
a, b = b, c
end
ρ = 1 / (1.618 * (1 - s^(n - i + 1)) / (1 - s^(n - i)))
end
return a < b ? (a, b) : (b, a)
end
function f(x)
return x * x - x + 1
end
# --------------------------------------golden search---------------------------------------------------------------------------------
function golden_section_search(f, a, b, n)
ρ = 1.618 - 1
d = ρ * b + (1 - ρ) * a
yd = f(d)
for i = 1:n-1
print(a, "\n")
print(b, "\n")
c = ρ * a + (1 - ρ) * b
yc = f(c)
if yc < yd
b, d, yd = d, c, yc
else
a, b = b, c
end
end
return a < b ? (a, b) : (b, a)
end
golden_section_search(f, 0.0, 2.0, 10)