trigNewton.cpp
Go to the documentation of this file.
1 /* $Id: trigNewton.cpp 490 2011-01-14 16:07:12Z pbelotti $
2  *
3  * Name: trigNewton.cpp
4  * Author: Pietro Belotti
5  * Purpose: numerically find tangents to (co)sines
6  *
7  * (C) Carnegie-Mellon University, 2006.
8  * This file is licensed under the Eclipse Public License (EPL)
9  */
10 
11 #include <math.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include "CouenneTypes.hpp"
15 
16 namespace Couenne {
17 
18 #define MAX_ITER 1000
19 #define COU_TRIG_TOLERANCE 1e-12
20 
22 
23  // Find a zero to the function
24  //
25  // F(x) = cos x - (sin x - sin a) / (x - a)
26  //
27  // whose derivative is
28  //
29  // F'(x) = - sin x - cos x / (x-a) + (sin x - sin a) / (x - a)^2
30 
31  if (l>u) {
32  register CouNumber swap = l;
33  l = u;
34  u = swap;
35  }
36 
37  register CouNumber xk = 0.5 * (u+l);
38 
39  CouNumber sina = sin (a),
40  sinxk = sin (xk),
41  cosxk = cos (xk),
42  dy = sinxk - sina,
43  dx = xk - a,
44  dydx = dy/dx,
45  F = cosxk - dydx;
46 
47  // Newton loop. Tolerance is set above
48  for (register int k = MAX_ITER; (fabs (F) > COU_TRIG_TOLERANCE) && k--;) {
49 
50  CouNumber Fp = sinxk + (cosxk - dydx) / dx;
51 
52  xk += F/Fp;
53 
54  if (xk < l) xk = l;
55  else if (xk > u) xk = u;
56 
57  sinxk = sin (xk);
58  cosxk = cos (xk);
59  dy = sinxk - sina;
60  dx = xk - a;
61  dydx = dy/dx;
62  F = cosxk - dydx;
63  }
64 
65  return xk;
66 }
67 
68 }
69 
70 /*
71 int main (int argc, char **argv) {
72 
73  CouNumber a = atof (argv [1]),
74  l = atof (argv [2]),
75  u = atof (argv [3]), r;
76 
77  for (register int i=100000; i--;)
78  r = trigNewton (a, l, u);
79 
80  printf ("b0 = %.14f: slope %.15f, derivative %.15f\n",
81  r, (sin (r) - sin (a)) / (r-a), cos (r));
82 
83  return 0;
84 }
85 */
void fint fint fint real * a
#define COU_TRIG_TOLERANCE
Definition: trigNewton.cpp:19
void fint fint * k
double CouNumber
main number type in Couenne
CouNumber trigNewton(CouNumber a, CouNumber l, CouNumber u)
common convexification method used by both cos and sin
Definition: trigNewton.cpp:21
CouExpr & cos(CouExpr &e)
CouExpr & sin(CouExpr &e)
#define MAX_ITER
Definition: trigNewton.cpp:18