-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheckConstraints.m
60 lines (50 loc) · 1.92 KB
/
checkConstraints.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
% valid (boolean)
% true - All constraints satisfied
% false - One or more constraints violated
%
% invalidConstraint (scalar)
% If valid == false, invalidConstraint is the index in
% piecewiseConstraints of the first violated constraint. Otherwise it's
% an empty array.
%
% y (scalar)
% If valid == false, y is the function value of the violated constraint,
% evaluated at the input argument point, x.
%
% x (cell aray of 2x1 matrices)
% Points in a 2-dimensional plane
%
% piecewiseConstraints (array of constraints for each month)
% Example: piecewiseConstraints = {[0 1 1 2 2 3;
% 0 0 1 1 2 2]};
function [valid, invalidConstraint, y] = checkConstraints(x, piecewiseConstraints)
valid = true;
invalidConstraint = -1;
y = NaN;
for monthIndex = 1:length(piecewiseConstraints)
% last constraint x-value that x is greater than
xL = find( x{monthIndex}(1) > ...
piecewiseConstraints{monthIndex}(1,:),1,'Last');
% first constraint x-value that is greater than x
xR = xL + 1;
% (x,y) points on left and right input point x
x1 = piecewiseConstraints{monthIndex}(1,xL);
x2 = piecewiseConstraints{monthIndex}(1,xR);
y1 = piecewiseConstraints{monthIndex}(2,xL);
y2 = piecewiseConstraints{monthIndex}(2,xR);
% slope of the line of the constraint directly above or below x
m = (y2 - y1)/(x2 - x1);
% y-intercept of the line of the constraint directly above or below x
% y = mx + b <=> b = y - mx
b = y1 - m*x1;
% valid if the y-value of the input point, x, is below the constraint
valid = x{monthIndex}(2) - (m*x{monthIndex}(1) + b) <= 1e-4;
if ~valid
% return the month for which a constraint is violated
invalidConstraint = monthIndex;
% return the function value of the violated constraint
y = m*x{monthIndex}(1) + b;
return
end
end
return