00001 /* 00002 * Search for a zero of a function in a given interval, by means of a 00003 * secant method. The input parameters x1 and x2 must be such that 00004 * f(x1)*f(x2) < 0 . The obtained root is then necessarily in the 00005 * interval [x1,x2]. 00006 * 00007 */ 00008 00009 /* 00010 * Copyright (c) 2002 Jerome Novak 00011 * 00012 * This file is part of LORENE. 00013 * 00014 * LORENE is free software; you can redistribute it and/or modify 00015 * it under the terms of the GNU General Public License as published by 00016 * the Free Software Foundation; either version 2 of the License, or 00017 * (at your option) any later version. 00018 * 00019 * LORENE is distributed in the hope that it will be useful, 00020 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00021 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00022 * GNU General Public License for more details. 00023 * 00024 * You should have received a copy of the GNU General Public License 00025 * along with LORENE; if not, write to the Free Software 00026 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00027 * 00028 */ 00029 00030 00031 char zerosec_borne_C[] = "$Header: /cvsroot/Lorene/C++/Source/Non_class_members/Utilities/zerosec_borne.C,v 1.5 2002/10/16 14:37:12 j_novak Exp $" ; 00032 00033 /* 00034 * $Id: zerosec_borne.C,v 1.5 2002/10/16 14:37:12 j_novak Exp $ 00035 * $Log: zerosec_borne.C,v $ 00036 * Revision 1.5 2002/10/16 14:37:12 j_novak 00037 * Reorganization of #include instructions of standard C++, in order to 00038 * use experimental version 3 of gcc. 00039 * 00040 * Revision 1.4 2002/05/02 15:16:22 j_novak 00041 * Added functions for more general bi-fluid EOS 00042 * 00043 * Revision 1.3 2002/04/18 09:17:17 j_novak 00044 * *** empty log message *** 00045 * 00046 * 00047 * $Header: /cvsroot/Lorene/C++/Source/Non_class_members/Utilities/zerosec_borne.C,v 1.5 2002/10/16 14:37:12 j_novak Exp $ 00048 * 00049 */ 00050 00051 // Headers C 00052 #include <stdlib.h> 00053 #include <math.h> 00054 #include <assert.h> 00055 00056 // Headers Lorene 00057 #include "headcpp.h" 00058 #include "param.h" 00059 //**************************************************************************** 00060 00061 double zerosec_b(double (*f)(double, const Param&), const Param& parf, 00062 double x1, double x2, double precis, int nitermax, int& niter) { 00063 00064 double f0_moins, f0, x0, x0_moins, dx, df , fnew, xnew; 00065 00066 // Teste si un zero unique existe dans l'intervalle [x_1,x_2] 00067 00068 f0_moins = f(x1, parf) ; 00069 f0 = f(x2, parf) ; 00070 if ( f0*f0_moins > 0.) { 00071 cout << 00072 "WARNING: zerosec: there may not exist a zero of the function" 00073 << endl ; 00074 cout << " between x1 = " << x1 << " ( f(x1)=" << f0_moins << " )" << endl ; 00075 cout << " and x2 = " << x2 << " ( f(x2)=" << f0 << " )" << endl ; 00076 } 00077 00078 // Choisit la borne avec la plus grande valeur de f(x) (borne positive) 00079 // comme la valeur la de x0 00080 00081 if ( f0_moins < f0) { // On a bien choisi f0_moins et f0 00082 x0_moins = x1 ; 00083 x0 = x2 ; 00084 } 00085 else { // il faut interchanger f0_moins et f0 00086 x0_moins = x2 ; 00087 x0 = x1 ; 00088 double swap = f0_moins ; 00089 f0_moins = f0 ; 00090 f0 = swap ; 00091 } 00092 00093 // Debut des iterations de la methode de la secante 00094 00095 niter = 0 ; 00096 do { 00097 df = f0 - f0_moins ; 00098 assert(df != double(0)) ; 00099 xnew = (x0_moins*f0 - x0*f0_moins)/df ; ; 00100 fnew = f(xnew, parf) ; 00101 if (fnew < 0.) { 00102 dx = x0_moins - xnew ; 00103 x0_moins = xnew ; 00104 f0_moins = fnew ; 00105 } 00106 else { 00107 dx = x0 - xnew ; 00108 x0 = xnew ; 00109 f0 = fnew ; 00110 } 00111 niter++ ; 00112 if (niter > nitermax) { 00113 //cout << "zerosec: Maximum number of iterations has been reached ! " 00114 // << endl ; 00115 //cout << x0_moins << ", " << xnew << ", " << x0 << endl ; 00116 //cout << f0_moins << ", " << fnew << ", " << f0 << endl ; 00117 00118 return xnew ; 00119 } 00120 } 00121 while ( ( fabs(dx) > precis ) && ( fabs(fnew) > 1.e-15 ) ) ; 00122 00123 return xnew ; 00124 } 00125 00126 00127
1.4.6