-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinalProject2c.m
More file actions
53 lines (38 loc) · 1.09 KB
/
FinalProject2c.m
File metadata and controls
53 lines (38 loc) · 1.09 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
tic;
R = (sqrt(5)-1)/2; %Initializing given values
xl = 0;
xu = 2;
i = 0;
d = R*(xu-xl); %Calculating initial values
x1 = xl + d;
x2 = xu - d;
xhold = 0;
f1 = TempAna(x1); %Calculate initial function values
f2 = TempAna(x2);
fhold = 100; %Initialize a storage value for the previous minimum
for n=1:1000 %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;
xhold = x1;
xl = x2;
x2 = x1;
f2 = f1;
d = R*(xu-xl);
x1 = xl+d;
f1 = TempAna(x1);
else %If the second function is larger than the first, we close the boundaries around that value
fhold = f2;
xhold = x2;
xu = x1;
x1 = x2;
f1 = f2;
d = R*(xu-xl);
x2 = xu-d;
f2 = TempAna(x2);
end
end
minimumVal = fhold
minimumLoc = xhold
iterations = i
toc;