forum.alglib.net
http://forum.alglib.net/

Needing help with Levenberg-Marquardt
http://forum.alglib.net/viewtopic.php?f=2&t=2096
Page 1 of 1

Author:  DominikGebhardt [ Tue Aug 05, 2014 6:10 am ]
Post subject:  Needing help with Levenberg-Marquardt

Hi,
I'm quite new to this and so I'm currently struggeling a little bit with using "minlmcreatevj".

I want to fit a circle to a number of points. P(x,y)

I want to minimize the function: SUM[ ((xi-a)^2 + (yi-b)^2 - r^2)^2 ]
The documentation says, that i have to split it in single function f[0]^2+f[1]^2+f[2]^2

I'm not sure if I did this right so here are my functions:

Code:
        public static void function1_fvec(double[] x, double[] fi, object obj)
        {
            // m_x = x-coordinate of center
            // m_y = y-coordinate of center
            // r      = radius of circle
            // x      = x-coordinate of point
            // y      = y-coordinate of point

            fi[0] = Math.Pow(x - m_x, 2);
            fi[1] = Math.Pow(y - m_y, 2);
            fi[2] = -Math.Pow(r, 2);
        }

        public static void function1_jac(double[] x, double[] fi, double[,] jac, object obj)
        {
            // m_x = x-coordinate of center
            // m_y = y-coordinate of center
            // r      = radius of circle
            // x      = x-coordinate of point
            // y      = y-coordinate of point

            fi[0] = Math.Pow(x - m_x, 2);
            fi[1] = Math.Pow(y - m_y, 2);
            fi[2] = -Math.Pow(r, 2);

            jac[0, 0] = -2 * (x - m_x);
            jac[0, 1] = 0;
            jac[0, 2] = 0;
            jac[1, 0] = 0;
            jac[1, 1] = -2 * (y - m_y);
            jac[1, 2] = 0;
            jac[2, 0] = 0;
            jac[2, 1] = 0;
            jac[2, 2] = -2 * r
        }


But I don't understand, where I can input my points. Otherwise he wouldn't know to what points he should fit the circle.
I'm using C#.

I'm a little bit confused and I hope, that you could help me with my problem how this library actually works.

Best regards

Dominik

Author:  Sergey.Bochkanov [ Tue Aug 05, 2014 7:01 am ]
Post subject:  Re: Needing help with Levenberg-Marquardt

Hello!

You got it wrong :) In your case, problem should be stated as:

Quote:
f0 = (x0-a)^2 + (y0-b)^2 - r^2
f1 = (x1-a)^2 + (y1-b)^2 - r^2
f2 = (x2-a)^2 + (y2-b)^2 - r^2
...


Thus, you have fixed number of parameters (three, a, b, r) - and variable number of functions, which is equal to the number of points.

Author:  DominikGebhardt [ Tue Aug 05, 2014 7:11 am ]
Post subject:  Re: Needing help with Levenberg-Marquardt

Ah ok.
Do I get this right, if I have 74 points I will have to fill f[0] till f[73]?
And in addition to that, for each function i will have 3 derivates so i will have something like this:

grad[0,0] = -2 * (x0-a)
grad[0,1] = -2 * (x1-b)
grad[0,2] = -2 * r
grad[1, 0].........and so on?

If this is right, is x[] one of my points?
So x[] should look like this:

double[] x = {{0, 0}, {1, 0.5}...}

Thank you so much for your fast reply! :)

Edit:
But if this would be correct, where should I define a,b,r?

My function would now look like this:

Code:
        public static void function1_fvec(double[] x, double[] fi, object obj)
        {

            for (int i = 0; i < 74; i++)
            {
                fi[i] = (x[i] - a) ^ 2 + (x[i + 1] - b) ^ 2 - r ^ 2;
            }
           
        }

I'm also not sure if I should minlm or lsfit. If I understood the documentation, than lsfit should only be a wrapper for minlm isn't it?

Author:  Sergey.Bochkanov [ Tue Aug 05, 2014 11:34 am ]
Post subject:  Re: Needing help with Levenberg-Marquardt

1. You are right, for 74 points you should have f[0]...f[73].
2. In your case (LM optimizer is used), x[] stores coefficients a, b, r - not (xi,yi)! Thus, you have x=double[3], not double[74]. You should store list of point coordinates elsewhere - in globally visible variables/fields or pass 2D array as additional parameter obj.
3. Both minlm and lsfit will work for you. Because you already started with minlm, you can just continue using it :)

Author:  NobregaRP [ Wed Nov 18, 2015 4:29 pm ]
Post subject:  Re: Needing help with Levenberg-Marquardt

Does anyone know how to estimate the error of each parameter using lmoptimizer?

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/