-
Notifications
You must be signed in to change notification settings - Fork 1
/
quadratic.m
81 lines (71 loc) · 1.9 KB
/
quadratic.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
function [Problem] = quadratic(A, b, interval)
% The definition of the quadratic problem.
%
% The problem of interest is defined as
%
% min f(x) = 1/2 * x^T * A * x + b^T * x.
% where
% x in R^d
%
% Inputs:
% A a positive definite matrix of size dxd
% b a column vector of size d
%
% Output:
% Problem problem instance.
d = length(b);
m = size(A, 1);
n = size(A, 2);
if m ~= n
error('A is not square');
end
if m <= 1
error('A is too small');
end
Problem.name = 'quadratic';
Problem.m = m;
Problem.n = n;
Problem.dim = d;
Problem.samples = d;
Problem.A = A;
Problem.b = b;
Problem.interval = interval;
Problem.cost = @cost;
function f = cost(x)
f = 0.5 * x'*A*x - b'*x;
end
Problem.grad = @grad;
function d = grad(x)
d = A*x - b;
end
Problem.grad2 = @grad2;
function e = grad2()
e = A;
end
Problem.plot_surface = @plot_surface;
function [] = plot_surface
if m == 2
[XX,YY] = meshgrid(Problem.interval);
X = XX(:); Y = YY(:); Z = diag(Problem.cost([X Y]'));
ZZ = reshape(Z, size(XX));
contour(XX,YY,ZZ);
hold on;
end
end
Problem.plot_legend = @plot_legend;
function [] = plot_legend(varargin)
hold on;
p1 = plot(nan, varargin{5});
p2 = plot(nan, varargin{6});
p3 = plot(nan, varargin{7});
p4 = plot(nan, varargin{8});
legend([p1 p2 p3 p4], {varargin{1},varargin{2},varargin{3},varargin{4}}, 'location', 'best')
end
Problem.plot_line = @plot_line;
function [] = plot_line(x1, x2, c, s)
if m == 2
PXY = [x1, x2];
line('XData', PXY(1 , :), 'YData', PXY(2 , :), 'LineStyle', s, 'LineWidth', 2, 'Marker', 'o', 'Color', c);
end
end
end