-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment3.m
More file actions
60 lines (43 loc) · 1.28 KB
/
assignment3.m
File metadata and controls
60 lines (43 loc) · 1.28 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
tic;
R = (sqrt(5)-1)/2; %Initializing given values
xl = 0.5;
xu = 1.4;
em = 0.01;
et = 100;
i = 0;
tval = 11.31370850; %Value calculated in Q3
d = R*(xu-xl); %Calculating initial values
x1 = xl + d;
x2 = xu - d;
f1 = golden(x1); %Calculate initial function values
f2 = golden(x2);
fhold = 100; %Initialize a storage value for the previous minimum
while et>em %Keep iterating while the error is above the desired error
i=i+1; %Keep track of iterations
if(f1<f2) %If the first function is larger than the second, we close the boundaries around that value
fhold = f1;
xl = x2;
x2 = x1;
f2 = f1;
d = R*(xu-xl);
x1 = xl+d;
f1 = golden(x1);
else %If the second function is larger than the first, we close the boundaries around that value
fhold = f2;
xu = x1;
x1 = x2;
f1 = f2;
d = R*(xu-xl);
x2 = xu-d;
f2 = golden(x2);
end
if fhold ~= 0 %Avoiding divide by zero
et = abs((tval-fhold))/tval*100;
else
break;
end
end
finalError = et %display the final results
minimumVal = fhold
iterations = i
toc;