Hi,
I'm trying a small toy example to gain confidence with
Non Linear Optimization with Levenberg-Marquardt in ALGLib.
This is my simple code where the objective function computes the sum of the distance between a point and each vertex of a fixed triangle.
Code:
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "optimization.h"
using namespace alglib;
const double XA = 1, YA = 5;
const double XB = 7, YB = 2;
const double XC = 7, YC = 8;
double pointDist(double x1, double y1, double x2, double y2)
{
return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}
void func(const real_1d_array &x, real_1d_array &fi, void *ptr)
{
fi[0] = pointDist(x[0], x[1], XA, YA);
fi[1] = pointDist(x[0], x[1], XB, YB);
fi[2] = pointDist(x[0], x[1], XC, YC);
}
int main(int argc, char *argv[])
{
real_1d_array x = "[0,0]";
double epsg = 0.0000000001;
double epsf = 0;
double epsx = 0;
ae_int_t maxits = 0;
minlmstate state;
minlmreport rep;
minlmcreatev(2, x, 0.0001, state);
minlmsetcond(state, epsg, epsf, epsx, maxits);
minlmoptimize(state, func);
minlmresults(state, x, rep);
printf("%d\n", int(rep.terminationtype));
printf("%s\n", x.tostring(2).c_str());
return 0;
}
Minimizing this function should give the coordinates of the triangle center of mass, right?
With the provided input points, the result should be:
[5,5]But when I compile and run it gives me:
[4.00,3.50]Do you know what am I missing or doing wrong here?
Many thanks!