GPLIB++
levrosen.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <cmath>
3 #include "lm.h"
4 
5 using namespace std;
6 
7 void misfit(double *p, double *x, int m, int n, void *data)
8  {
9 
10  x[0] = 100 * pow(p[1] - pow(p[0], 2), 2) + pow(1 - p[0], 2);
11  x[1] = 100 * pow(p[1] - pow(p[0], 2), 2) + pow(1 - p[0], 2);
12  x[2] = 100 * pow(p[1] - pow(p[0], 2), 2) + pow(1 - p[0], 2);
13  }
14 
15 int main()
16  {
17  const int nparams = 2;
18  const int n = 3;
19  const int maxiter = 100;
20  double p[nparams], lb[nparams], ub[nparams]; // 6 is max(2, 3, 5)
21  double *x; // 16 is max(2, 3, 5, 6, 16)
22  x = new double[n];
23  double opts[LM_OPTS_SZ], info[LM_INFO_SZ];
24 
25  opts[0] = LM_INIT_MU;
26  opts[1] = 1E-15;
27  opts[2] = 1E-15;
28  opts[3] = 1E-20;
29  opts[4] = LM_DIFF_DELTA; // relevant only if the finite difference jacobian version is used
30 
31  for (int i = 0; i < nparams; ++i)
32  {
33  p[i] = -1;
34  lb[i] = -10000;
35  ub[i] = 10000;
36  }
37 
38  for (int i = 0; i < n; i++)
39  x[i] = 0.0;
40 
41  double ret = dlevmar_bc_dif(misfit, p, x, nparams, n, lb, ub, maxiter,
42  opts, info, NULL, NULL, NULL); // no jacobian
43  cout << "Levenberg-Marquardt returned " << ret << " in " << info[5]
44  << "iter, reason " << info[6] << endl;
45 
46  cout << "p: ";
47  for (int i = 0; i < nparams; ++i)
48  cout << p[i] << " ";
49  cout << endl;
50  cout << endl << " Minimization info:" << endl;
51  for (int i = 0; i < LM_INFO_SZ; ++i)
52  cout << info[i] << " ";
53  cout << endl;
54 
55  return 0;
56  }
void misfit(double *p, double *x, int m, int n, void *data)
Definition: levrosen.cpp:7
int main()
Definition: levrosen.cpp:15