|
a |
|
b/combinedDeepLearningActiveContour/minFunc/polyinterp.m |
|
|
1 |
function [minPos,fmin] = polyinterp(points,doPlot,xminBound,xmaxBound) |
|
|
2 |
% function [minPos] = polyinterp(points,doPlot,xminBound,xmaxBound) |
|
|
3 |
% |
|
|
4 |
% Minimum of interpolating polynomial based on function and derivative |
|
|
5 |
% values |
|
|
6 |
% |
|
|
7 |
% In can also be used for extrapolation if {xmin,xmax} are outside |
|
|
8 |
% the domain of the points. |
|
|
9 |
% |
|
|
10 |
% Input: |
|
|
11 |
% points(pointNum,[x f g]) |
|
|
12 |
% doPlot: set to 1 to plot, default: 0 |
|
|
13 |
% xmin: min value that brackets minimum (default: min of points) |
|
|
14 |
% xmax: max value that brackets maximum (default: max of points) |
|
|
15 |
% |
|
|
16 |
% set f or g to sqrt(-1) if they are not known |
|
|
17 |
% the order of the polynomial is the number of known f and g values minus 1 |
|
|
18 |
|
|
|
19 |
if nargin < 2 |
|
|
20 |
doPlot = 0; |
|
|
21 |
end |
|
|
22 |
|
|
|
23 |
nPoints = size(points,1); |
|
|
24 |
order = sum(sum((imag(points(:,2:3))==0)))-1; |
|
|
25 |
|
|
|
26 |
% Code for most common case: |
|
|
27 |
% - cubic interpolation of 2 points |
|
|
28 |
% w/ function and derivative values for both |
|
|
29 |
% - no xminBound/xmaxBound |
|
|
30 |
|
|
|
31 |
if nPoints == 2 && order ==3 && nargin <= 2 && doPlot == 0 |
|
|
32 |
% Solution in this case (where x2 is the farthest point): |
|
|
33 |
% d1 = g1 + g2 - 3*(f1-f2)/(x1-x2); |
|
|
34 |
% d2 = sqrt(d1^2 - g1*g2); |
|
|
35 |
% minPos = x2 - (x2 - x1)*((g2 + d2 - d1)/(g2 - g1 + 2*d2)); |
|
|
36 |
% t_new = min(max(minPos,x1),x2); |
|
|
37 |
[minVal minPos] = min(points(:,1)); |
|
|
38 |
notMinPos = -minPos+3; |
|
|
39 |
d1 = points(minPos,3) + points(notMinPos,3) - 3*(points(minPos,2)-points(notMinPos,2))/(points(minPos,1)-points(notMinPos,1)); |
|
|
40 |
d2 = sqrt(d1^2 - points(minPos,3)*points(notMinPos,3)); |
|
|
41 |
if isreal(d2) |
|
|
42 |
t = points(notMinPos,1) - (points(notMinPos,1) - points(minPos,1))*((points(notMinPos,3) + d2 - d1)/(points(notMinPos,3) - points(minPos,3) + 2*d2)); |
|
|
43 |
minPos = min(max(t,points(minPos,1)),points(notMinPos,1)); |
|
|
44 |
else |
|
|
45 |
minPos = mean(points(:,1)); |
|
|
46 |
end |
|
|
47 |
return; |
|
|
48 |
end |
|
|
49 |
|
|
|
50 |
xmin = min(points(:,1)); |
|
|
51 |
xmax = max(points(:,1)); |
|
|
52 |
|
|
|
53 |
% Compute Bounds of Interpolation Area |
|
|
54 |
if nargin < 3 |
|
|
55 |
xminBound = xmin; |
|
|
56 |
end |
|
|
57 |
if nargin < 4 |
|
|
58 |
xmaxBound = xmax; |
|
|
59 |
end |
|
|
60 |
|
|
|
61 |
% Constraints Based on available Function Values |
|
|
62 |
A = zeros(0,order+1); |
|
|
63 |
b = zeros(0,1); |
|
|
64 |
for i = 1:nPoints |
|
|
65 |
if imag(points(i,2))==0 |
|
|
66 |
constraint = zeros(1,order+1); |
|
|
67 |
for j = order:-1:0 |
|
|
68 |
constraint(order-j+1) = points(i,1)^j; |
|
|
69 |
end |
|
|
70 |
A = [A;constraint]; |
|
|
71 |
b = [b;points(i,2)]; |
|
|
72 |
end |
|
|
73 |
end |
|
|
74 |
|
|
|
75 |
% Constraints based on available Derivatives |
|
|
76 |
for i = 1:nPoints |
|
|
77 |
if isreal(points(i,3)) |
|
|
78 |
constraint = zeros(1,order+1); |
|
|
79 |
for j = 1:order |
|
|
80 |
constraint(j) = (order-j+1)*points(i,1)^(order-j); |
|
|
81 |
end |
|
|
82 |
A = [A;constraint]; |
|
|
83 |
b = [b;points(i,3)]; |
|
|
84 |
end |
|
|
85 |
end |
|
|
86 |
|
|
|
87 |
% Find interpolating polynomial |
|
|
88 |
params = A\b; |
|
|
89 |
|
|
|
90 |
% Compute Critical Points |
|
|
91 |
dParams = zeros(order,1); |
|
|
92 |
for i = 1:length(params)-1 |
|
|
93 |
dParams(i) = params(i)*(order-i+1); |
|
|
94 |
end |
|
|
95 |
|
|
|
96 |
if any(isinf(dParams)) |
|
|
97 |
cp = [xminBound;xmaxBound;points(:,1)].'; |
|
|
98 |
else |
|
|
99 |
cp = [xminBound;xmaxBound;points(:,1);roots(dParams)].'; |
|
|
100 |
end |
|
|
101 |
|
|
|
102 |
% Test Critical Points |
|
|
103 |
fmin = inf; |
|
|
104 |
minPos = (xminBound+xmaxBound)/2; % Default to Bisection if no critical points valid |
|
|
105 |
for xCP = cp |
|
|
106 |
if imag(xCP)==0 && xCP >= xminBound && xCP <= xmaxBound |
|
|
107 |
fCP = polyval(params,xCP); |
|
|
108 |
if imag(fCP)==0 && fCP < fmin |
|
|
109 |
minPos = real(xCP); |
|
|
110 |
fmin = real(fCP); |
|
|
111 |
end |
|
|
112 |
end |
|
|
113 |
end |
|
|
114 |
% Plot Situation |
|
|
115 |
if doPlot |
|
|
116 |
figure(1); clf; hold on; |
|
|
117 |
|
|
|
118 |
% Plot Points |
|
|
119 |
plot(points(:,1),points(:,2),'b*'); |
|
|
120 |
|
|
|
121 |
% Plot Derivatives |
|
|
122 |
for i = 1:nPoints |
|
|
123 |
if isreal(points(i,3)) |
|
|
124 |
m = points(i,3); |
|
|
125 |
b = points(i,2) - m*points(i,1); |
|
|
126 |
plot([points(i,1)-.05 points(i,1)+.05],... |
|
|
127 |
[(points(i,1)-.05)*m+b (points(i,1)+.05)*m+b],'c.-'); |
|
|
128 |
end |
|
|
129 |
end |
|
|
130 |
|
|
|
131 |
% Plot Function |
|
|
132 |
x = min(xmin,xminBound)-.1:(max(xmax,xmaxBound)+.1-min(xmin,xminBound)-.1)/100:max(xmax,xmaxBound)+.1; |
|
|
133 |
size(x) |
|
|
134 |
for i = 1:length(x) |
|
|
135 |
f(i) = polyval(params,x(i)); |
|
|
136 |
end |
|
|
137 |
plot(x,f,'y'); |
|
|
138 |
axis([x(1)-.1 x(end)+.1 min(f)-.1 max(f)+.1]); |
|
|
139 |
|
|
|
140 |
% Plot Minimum |
|
|
141 |
plot(minPos,fmin,'g+'); |
|
|
142 |
if doPlot == 1 |
|
|
143 |
pause(1); |
|
|
144 |
end |
|
|
145 |
end |