]> Creatis software - CreaPhase.git/blob - octave_packages/optim-1.2.0/__poly_2_extrema.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / optim-1.2.0 / __poly_2_extrema.m
1 ## Copyright (C) 2002 Etienne Grossmann <etienne@egdn.net>
2 ## Copyright (C) 2009 Levente Torok <TorokLev@gmail.com>
3 ##
4 ## This program is free software; you can redistribute it and/or modify it under
5 ## the terms of the GNU General Public License as published by the Free Software
6 ## Foundation; either version 3 of the License, or (at your option) any later
7 ## version.
8 ##
9 ## This program is distributed in the hope that it will be useful, but WITHOUT
10 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 ## details.
13 ##
14 ## You should have received a copy of the GNU General Public License along with
15 ## this program; if not, see <http://www.gnu.org/licenses/>.
16
17 ##  ex = poly_2_ex (l, f)       - Extremum of a 1-var deg-2 polynomial
18 ##
19 ## l  : 3 : variable values
20 ## f  : 3 : f(i) = value of polynomial at l(i)
21 ## 
22 ## ex : 1 : Value at which f reaches an extremum
23 ## 
24 ## Assuming that f(i) = a*l(i)^2 + b* l(i) + c = P(l(i)) for some a, b, c,
25 ## ex is the extremum of the polynome P.
26
27 function ex = __poly_2_extrema (l, f)
28
29 ### This somewhat helps if solution is very close to one of the points.
30 [f,i] = sort (f);
31 l = l(i); 
32
33
34 m = (l(2) - l(1))/(l(3) - l(1));
35 d = (2*(f(1)*(m-1)+f(2)-f(3)*m));
36 if abs (d) < eps,
37   printf ("poly_2_ex : divisor is small (solution at infinity)\n");
38   printf ("%8.3e %8.3e %8.3e, %8.3e %8.3e\n",\
39           f(1), diff (f), diff (sort (l)));
40
41   ex = (2*(l(1)>l(2))-1)*inf;
42   ## keyboard
43 else
44   ex  =  ((l(3) - l(1))*((f(1)*(m^2-1) + f(2) - f(3)*m^2))) / d ;
45
46 ## Not an improvement
47 #  n = ((l(2)+l(3))*(l(2)-l(3)) + 2*(l(3)-l(2))*l(1)) / (l(3)-l(1))^2 ;
48 #  ex =  ((l(3) - l(1))*((f(1)*n + f(2) - f(3)*m^2))) / \
49 #      (2*(f(1)*(m-1)+f(2)-f(3)*m));
50 #  if ex != ex0,
51 #    ex -  ex0
52 #  end
53   ex = l(1) + ex;
54 end