Like Tree1Likes

this self contained program

This is a discussion on this self contained program within the C Programming forums, part of the General Programming Boards category; Hi I have found an implementation of an algorithm called the Levenberg Marquardt algorithm that states that it is self ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    192

    this self contained program

    Hi I have found an implementation of an algorithm called the Levenberg Marquardt algorithm that states that it is self contained.
    I am trying to understand an implementation of it and most of the web and HUGE but this one seems nice and small however I cannot get it to compile (using codeblocks). I have attached what I think is required, and below is the page I downloaded it from.
    Could someone please have a look and see if they can get it to compile.
    http://joachimwuttke.de/lmfit/
    (download link under resources)

    Thanks
    Alex
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,915
    What operating system are you using? The normal ./configure/make/make install commands seemed to work for me, on Linux.

    Jim

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    192
    Oh! Im on windows. Can I run a similar command from here.
    out of interest what does it give you? .o files or something? I need to be able to compile this into an embedded program.
    I am looking for a standalone C implementation of this algorithm but there seem to be few out there - there are ones built into MUCH MUCH more complex libraries, but I dont really have the C expertise to extract them
    Alex

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Haven't used codeblocks before but I'm guessing that you'd have to start by adding all the source files to a new project before attempting to build the application. Have you done that? If not then do so and try to compile/link and report the exact error messages you get if any.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,220
    Quote Originally Posted by a.mlw.walker View Post
    Oh! Im on windows. Can I run a similar command from here.
    out of interest what does it give you? .o files or something?
    Yes. On Windows you can install MinGW, which gives you the MSYS environment, allowing you to run scripts like configure.

    The result is a bunch of .o files which are combined into an archive file (.a). In GCC, you normally name the file something like libfoo.a and then you can compile a program using -lfoo to link with that library. Compilers for embedded systems will have a similar facility, but this varies from compiler to compiler.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,915
    After a quick look everything looks fairly standard. It looks like you should be able to compile it with a Windows compiler without any problems. You should be able to add the source files to your project and compile it normally. Just be sure to put the source and include files into your project source directory.

    Jim

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    192
    Any chance someone can post the "lib"' s because Im not sure to compile the rest of the files, except the ones I attached.
    Yeah I made a new project in codeblocks, added the files in my zip (attached above) and it wouldnt compile. I can see that there must be more code somewhere because there aren't any algorithms that do what levenberg marquardt does in the files I can see (the .c ones I attached).

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    2,764
    Post the compiler errors! Please use code tags for posting the errors.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    2,764
    Your file lmmin.c DOES NOT have the correct contents. It is the same as lmcurve.h

    Compiles for me using MinGW GCC C under Code::Blocks when I used the correct code in the file lmmin.c.

    Tim S.
    Last edited by stahta01; 01-31-2013 at 04:40 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    192
    Bang on, I dont know how I managed that. Thanks
    Alex

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    192
    Hey guys,
    sorry to bring this up again, I am trying to change the function that the example is fitting for my equation:

    Code:
    double myfunction(double Actual_Output[9], double *p){
    double Fitted_Curve[5] = {0};
    int i;
    double invtrig = 0, x = 0;
    
    
      for (i = 0; i <sizeof(Actual_Output)-1; i++)
      {
    	invtrig = arcsinh(sinh(p[2])*exp(p[0]*(i+1)*2*M_PI));
    	Fitted_Curve[i] = (1/(p[0]*p[1]))*(p[2]-invtrig);
        x += Actual_Output[i+2]-Fitted_Curve[i];
    
        }
    return x;
    }
    /* model function: a quadratic function p0 + p1 * (t-p2)^2 */
    
    double f( double t, const double *p )
    {
        return p[0] + p[1]*(t-p[2])*(t-p[2]);
    }
    the example function is f, mine is myfunction...
    I thought I would have to pass the function my data somehow, but they seem to only pass a double and a pointer to a double array.
    Can anyone help me to implement my algorithm instead of theirs?
    Thanks
    Alex

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    2,915
    Please show where you are using your function. If you are trying to use your function like they are using their function then your function's signature must match their function's signature. You can change the interior of the function but it must return the same type of variable and the parameters must be exactly the same.

    Jim

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    2,915
    You may want to study this link: The Function Pointer Tutorial. It might help explain how that function is actually being used.

    Jim

  14. #14
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by a.mlw.walker View Post
    I thought I would have to pass the function my data somehow, but they seem to only pass a double and a pointer to a double array.
    Can anyone help me to implement my algorithm instead of theirs?
    As I understand it after reading the example program (curve1.c) the function "f" should describe the type of function which you think will best fit your data points. For example if you think there is a linear relationship "f" could look like:
    Code:
    double f(double t, const double *p)
    {
        return p[0] * t + p[1]
    }
    If you think a logarithmic relationship works best it could look like:
    Code:
    double f(double t, const double *p)
    {
        return log(t) + p[0]     // needs <math.h>
    }
    and so on. So it's up to you what function you need.

    You have to pass your data points to lmcurve_fit() as two arrays (4th and 5th parameter). Read again the example program and experiment with it (change the parameters) to see how it works.

    Code:
    double myfunction(double Actual_Output[9], double *p){
    ...
    for (i = 0; i <sizeof(Actual_Output)-1; i++)
    This is definitely wrong. Your first parameter is an array, hence the function only gets a pointer to the first element. Then sizeof returns the size of this pointer which is system dependent (on my system it's 4 bytes). You can't determine the size of the array (i.e. the number of its element) inside the function. You would have to pass it as an additional parameter.

    Bye, Andreas
    stahta01 likes this.

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    192
    Hi guys,
    so after some studying, I think that for my problem I actually want surface1.c as the basis to manipulate for my algorithm, not sure though, just think that.
    I have adjusted the program so that it now compiles and runs with my function however I dont think I have quite understood my way around all the function pointers and the struct thats in there too.
    The data I have that I am trying to fit is:
    Code:
       double tx[5] = {1, 2, 3, 4, 5};
        double tz[5] = {1.2, 2.693, 4.325, 6.131, 8.125};
    On a graph tx would be on the x axis, and tz on the y. Im not sure what the 'tx' array in the example is.
    So I am trying to fit the above data to the algorithm:
    Code:
    double f(double tz, const double *p){
    double Fitted_Curve[5] = {0};
    int i;
    double invtrig = 0, x = 0;
    
    
      for (i = 0; i <sizeof(tz)-1; i++)
      {
    	invtrig = arcsinh(sinh(p[2])*exp(p[0]*(i+1)*2*3.145));
    	Fitted_Curve[i] = (1/(p[0]*p[1]))*(p[2]-invtrig);
        x += pow((tz[i]-Fitted_Curve[i]), 2);
    
        }
    return x;
    }
    However it wont compile with the above, because I am asking for tz[i]. The following algorithm it will compile and run with, however it produces incorrect results, as its not "fitting" the correct curve (I dont think)
    Code:
    double f(double tz, const double *p){
    double Fitted_Curve = 0;//[5] = {0};
    int i;
    double invtrig = 0, x = 0;
    
    
     // for (i = 0; i <sizeof(tz)-1; i++)
     // {
    	invtrig = arcsinh(sinh(p[2])*exp(p[0]*(i+1)*2*3.145));
    	Fitted_Curve = (1/(p[0]*p[1]))*(p[2]-invtrig);
        x = pow((tz-Fitted_Curve), 2);
    
      //  }
    return x;
    }
    I dont really need tx because its just 1,2,3,4,5 which I planned on using i+1 for.

    Please can someone have a look and see if they can simplify the algorithm, or at least help me to simplify it so that I can "fit this equation"?
    Thanks
    Attached Files Attached Files

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting the text contained in an external application
    By geek@02 in forum C# Programming
    Replies: 3
    Last Post: 05-11-2012, 06:20 PM
  2. how to see if one string is contained in the other
    By bigal1496 in forum C Programming
    Replies: 1
    Last Post: 08-02-2009, 06:23 PM
  3. making self contained executables?
    By m37h0d in forum C++ Programming
    Replies: 18
    Last Post: 03-30-2008, 07:27 PM
  4. Using Pointers in Contained Classes
    By arb215 in forum C++ Programming
    Replies: 6
    Last Post: 09-24-2004, 11:31 AM
  5. Self contained help
    By loggjam in forum C Programming
    Replies: 4
    Last Post: 08-05-2002, 12:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21