-
Notifications
You must be signed in to change notification settings - Fork 1
/
cvxfitfeas.m
43 lines (43 loc) · 1.13 KB
/
cvxfitfeas.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
function [check,maxerr,yfit] = cvxfitfeas(U,y,err)
% Checks if a data set can be approximated with a convex function subject
% to a maximum residual constraint.
%
% Notation:
%
% U - matrix of input data points, with rows denoting the different points
% y - outputs corresponding to the inputs (row vector)
% err - maximum allowable residual
% check - returns 1 if it is possible to fit function, 0 otherwise
% maxerr - maximum residual of convex fit
% yfit - output values defining the fit
m = length(y);
n = length(U(1,:));
A1 = [-eye(m) zeros(m,m*n) -ones(m,1);
eye(m) zeros(m,m*n) -ones(m,1)];
b1 = [-y'; y'];
A2 = zeros(m*(m-1),m+m*n+1);
lc = 1;
for i = 1:m
for j = 1:m
if i ~= j
A2(lc,i) = 1;
A2(lc,j) = -1;
A2(lc,[1+m+(i-1)*n:m+i*n]) = U(j,:)-U(i,:);
lc = lc + 1;
end
end
end
b2 = zeros(m*(m-1),1);
A = [A1;A2];
b = [b1;b2];
[xopt,maxerr,exit] = linprog([zeros(1,m+m*n) 1],A,b,[],[],[],[],[],optimset('disp','iter'));
if exit <= 0
disp('Warning - Optimization did not terminate property.');
end
yfit = xopt(1:m);
if maxerr <= err
check = 1;
else
check = 0;
end
end