levrosen.cpp
Go to the documentation of this file.00001 #include <iostream>
00002 #include <cmath>
00003 #include "lm.h"
00004
00005 using namespace std;
00006
00007 void misfit(double *p, double *x, int m, int n, void *data)
00008 {
00009
00010 x[0] = 100 * pow(p[1] - pow(p[0], 2), 2) + pow(1 - p[0], 2);
00011 x[1] = 100 * pow(p[1] - pow(p[0], 2), 2) + pow(1 - p[0], 2);
00012 x[2] = 100 * pow(p[1] - pow(p[0], 2), 2) + pow(1 - p[0], 2);
00013 }
00014
00015 int main()
00016 {
00017 const int nparams = 2;
00018 const int n = 3;
00019 const int maxiter = 100;
00020 double p[nparams], lb[nparams], ub[nparams];
00021 double *x;
00022 x = new double[n];
00023 double opts[LM_OPTS_SZ], info[LM_INFO_SZ];
00024
00025 opts[0] = LM_INIT_MU;
00026 opts[1] = 1E-15;
00027 opts[2] = 1E-15;
00028 opts[3] = 1E-20;
00029 opts[4] = LM_DIFF_DELTA;
00030
00031 for (int i = 0; i < nparams; ++i)
00032 {
00033 p[i] = -1;
00034 lb[i] = -10000;
00035 ub[i] = 10000;
00036 }
00037
00038 for (int i = 0; i < n; i++)
00039 x[i] = 0.0;
00040
00041 double ret = dlevmar_bc_dif(misfit, p, x, nparams, n, lb, ub, maxiter,
00042 opts, info, NULL, NULL, NULL);
00043 cout << "Levenberg-Marquardt returned " << ret << " in " << info[5]
00044 << "iter, reason " << info[6] << endl;
00045
00046 cout << "p: ";
00047 for (int i = 0; i < nparams; ++i)
00048 cout << p[i] << " ";
00049 cout << endl;
00050 cout << endl << " Minimization info:" << endl;
00051 for (int i = 0; i < LM_INFO_SZ; ++i)
00052 cout << info[i] << " ";
00053 cout << endl;
00054
00055 return 0;
00056 }