LORENE
zero_premier.C
1 /*
2  * Locates the first zero of a function in a given interval.
3  *
4  */
5 
6 /*
7  * Copyright (c) 1999-2001 Eric Gourgoulhon
8  *
9  * This file is part of LORENE.
10  *
11  * LORENE is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * LORENE is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with LORENE; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24  *
25  */
26 
27 
28 
29 
30 /*
31  * $Id: zero_premier.C,v 1.4 2016/12/05 16:18:11 j_novak Exp $
32  * $Log: zero_premier.C,v $
33  * Revision 1.4 2016/12/05 16:18:11 j_novak
34  * Suppression of some global variables (file names, loch, ...) to prevent redefinitions
35  *
36  * Revision 1.3 2014/10/13 08:53:32 j_novak
37  * Lorene classes and functions now belong to the namespace Lorene.
38  *
39  * Revision 1.2 2002/10/16 14:37:12 j_novak
40  * Reorganization of #include instructions of standard C++, in order to
41  * use experimental version 3 of gcc.
42  *
43  * Revision 1.1.1.1 2001/11/20 15:19:29 e_gourgoulhon
44  * LORENE
45  *
46  * Revision 1.2 2000/01/04 10:57:51 eric
47  * Le test f1*f2 < 0. est remplace par f1*f2 <= double(0).
48  *
49  * Revision 1.1 1999/12/24 13:00:10 eric
50  * Initial revision
51  *
52  *
53  * $Header: /cvsroot/Lorene/C++/Source/Non_class_members/Utilities/zero_premier.C,v 1.4 2016/12/05 16:18:11 j_novak Exp $
54  *
55  */
56 
57 // Headers Lorene
58 #include "headcpp.h"
59 #include "param.h"
60 //****************************************************************************
61 
62 namespace Lorene {
63 
64 bool zero_premier(double (*f)(double, const Param&), const Param& par,
65  double a, double b, int n, double& a0, double& b0) {
66 
67  double dx = (b-a)/double(n) ;
68 
69  a0 = a ;
70  b0 = a0 + dx ;
71 
72  double f1 = f(a0, par) ;
73  bool trouve = false ;
74 
75  for (int i=0; i<n; i++) {
76  double f2 = f(b0, par) ;
77  if (f1*f2 <= double(0)) { // on a encadre le zero
78  trouve = true ;
79  break ;
80  }
81 
82  // On passe au sous-intervalle suivant :
83  a0 = b0 ;
84  f1 = f2 ;
85  b0 += dx ;
86  }
87 
88  return trouve ;
89 
90 }
91 }
Lorene prototypes.
Definition: app_hor.h:67
Parameter storage.
Definition: param.h:125
bool zero_premier(double(*f)(double, const Param &), const Param &par, double a, double b, int n, double &a0, double &b0)
Locates the sub-interval containing the first zero of a function in a given interval.
Definition: zero_premier.C:64