CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2009
    Posts
    34

    How to code a math formula and passing it to a function

    hi!

    I want to use a C++ function which receives a math function and executes minimization on it ( http://www.codecogs.com/d-ox/maths/o...ion/nelder.php) but I dont understand how to pass my formula as the first parameter.

    There is even an example but I dont understand it...

    Code:
    double f(double *x) {
        double r = sqrt(x[0] * x[0] + x[1] * x[1]);
        return ABS(r) < 1E-12 ? 1 : sin(r) / r;
    }

    My formula is: http://personal.telefonica.terra.es/...dio1/text3.gif

    This is what I've made:

    Code:
    for (i=1;i<=numberOfAnchors;i++)
    {
       errorsquare+= pow( abs( x[i] - xestim + y[i] - yestim ) - distance[i]  ) , 2 );
    }
    errorsquare=errorsquare/numberOfAnchors;

    Thanks.
    Last edited by Shadowrun; April 24th, 2009 at 06:40 PM.

  2. #2
    Join Date
    Mar 2009
    Posts
    19

    Re: How to code a math formula and passing it to a function

    I'm not quite sure what you're asking. If it's about C++ syntax for referencing functions, this isn't really the forum for that, but whatever.

    So, from what I've gathered, you need to make your squared error calculation a function and pass it into Maths::Optimization::nelder().

    nelder() takes, as its first parameter, a function taking a pointer to a double (an array of doubles) and returning a double.

    So, just transform for calculation into a function (let's call it "Foo"), where your unknowns are the parameters (it looks like x fills that roll, while y is known, based on your example), and pass it in by name as the first parameter of nelder().
    Code:
    double Foo(double * x)
    {
         /*return your squared error based on x*/;
    }
    
    int main()
    {
         Maths::Optimization::nelder(Foo, /*other params*/);
         return 0;
    }

  3. #3
    Join Date
    Feb 2009
    Posts
    34

    Re: How to code a math formula and passing it to a function

    You say that there is only 1 unknown (x), but there are 2 ('xestim' and 'yestim'). Research paper shows "P" (point) as the unknown, so I guess it should be (x,y) 2 unknowns....

    The C++ function that I'm trying to use says: "This method performs the minimization of a function with several variables..."

    So my question is precisely how I have to code my formula so I can pass it to the C++ function, i.e.
    Code:
    /*return your squared error based on 'xestim' and 'yestim' */
    Last edited by Shadowrun; April 25th, 2009 at 09:04 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured