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