-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.m
executable file
·108 lines (73 loc) · 2.15 KB
/
test.m
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
function test
clc
options = setoptimoptions(...
%{
'algorithm', 'fminsearch',...
%}
'algorithm', 'fminlbfgs',...
'goalsexactachieve', 0,...
'hessupdate', 'bfgs',...
'Largescale', 'on',...
'storeN', 20,...
'display', 'iter',...
'TolCon', 1e-10,...
'TolX' , 1e-13,...
'TolFun' ,1e-13,...
'Gradobj', 'on',...
'GradConstr', 'on',...
'MaxIter', 1e2,...
'MaxFunEvals', 5e4,...
'popsize', 10);
[sol, fval, exitflag, output] = ...
minimize(@himmelblau, [10;-1], [-1 1],[6], [1 1],[sqrt(2)], ...
5*[-1; -1], 5*[1; 1], @nonlcon, options);
sol, fval
([1 1]*sol)/sqrt(2)
[c, ceq] = nonlcon(sol)
disp ' '
disp ' '
disp ' '
options = optimset(...
%{
'algorithm', 'fminsearch',...
%}
'algorithm', 'active-set',...
'display', 'off',...
'TolCon', 1e-10,...
'TolX' , 1e-10,...
'TolFun' ,1e-10,...
'Gradobj', 'on',...
'GradConstr', 'on',...
'MaxIter', 1e4,...
'MaxFunEvals', 5e4);
[sol2, fval2, ~, output] = ...
fmincon(@himmelblau, [10;-1], [-1 1],[6], [1 1],[sqrt(2)], ...
5*[-1; -1], 5*[1; 1], @nonlcon, options)
%
output
% ([1 1]*sol2)/sqrt(2)
[c, ceq] = nonlcon(sol2)
% [sol, fval, exitflag, output] = ...
% minimize(@banana, [1; 1], [1 1],[3], [1 1],[2], ...
% 5*[-1 -1], 5*[1 1], @nonlcon, options)
end
function [obj, grad_obj] = banana(x)
obj = (1-x(1)).^2 + 100*(x(2)-x(1).^2).^2;
grad_obj = [
-2*(1-x(1)) - 400*(x(2)-x(1).^2).*x(1)
200*(x(2)-x(1).^2)
]';
end
function [obj, grad_obj] = himmelblau(x)
obj = (x(1).^2 + x(2) - 11).^2 + (x(1) + x(2).^2 - 7).^2;
grad_obj = [
4*x(1)*(x(1).^2 + x(2) - 11) + 2*(x(1) + x(2).^2 - 7)
2*(x(1).^2 + x(2) - 11) + 4*x(2)*(x(1) + x(2).^2 - 7)
];
end
function [c, ceq, grad_c, grad_ceq] = nonlcon(x)
ceq = x(:).'*x(:) - 1;
c = [];
grad_c = [];
grad_ceq = 2*x;
end