Thread: nonlinear curve fitting

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    45

    nonlinear curve fitting

    hi,

    i have a rather long non-linear equation with 8 variables that i need to optimize. is there some package/library of ready-made curve fitting c codes that i can use?

    thanks!

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    90
    Have you any idea of the type of curve you need. There are so many.

    I suggest you load your data into Excel. That offers a wide range of curve fitting tools. Use this to experiment, and you'll soon lock on to the class of curve fitting you need.

    Have you thought about multiple regression - just a guess as you haven't provided much detail.
    Never re-write code unless the user benefits

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Gnuplot has some very nice curve fitting tools.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    20
    If your objective is to implement a nonlinear model already tested and parameterized, your task is not difficult. However, if you have a set of data, and want to find an equation, I recommend you to give a quick look at the GNU Scientific Library.

    Overall, I believe that the most straightforward way to solve your problem is to fit your curve using a standard package (R, GRaphPad Prism), get the equation, and code it in C.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    45
    hi, thanks for the replies. i use the curve-fitting solver tool in Excel quite frequently for my other straightforward curve-fitting problems. however, right now, what i need to do is to apply curve-fitting to an image, meaning that i have to iterate this curve-fitting on a pixel by pixel basis (there are about 15million pixels in my image), so Excel is definitely not the way to go.

    and yes, i already have a model that i want to optimize, so all i need to do is to vary the model parameters. the model is quite complicated, with exponential, trigonometric, as well as quadratic terms.

    what i am hoping to do is to use a readily-available source code for this kind of curve-fitting, and pluck it into my c program. however, i have yet to find any that is easy to understand and implement. there are some links here Levenberg–Marquardt algorithm - Wikipedia, the free encyclopedia but i have yet to try them out.

    any other suggestions?

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    20

  7. #7
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    90

    levmar c source

    levmar is the way to go.

    Attached is a ZIP of free C source. It also has TXT files with lots of references, info on how to use, links, etc.

    Hope this helps.

    For some reason ZIPs are not allowed as attachments, so it's renamed as LEVMAR.TXT. Please detach and rename back to ZIP before use.
    Never re-write code unless the user benefits

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    45
    thanks for the suggestions. i have downloaded the levmar algorithm from the levmar website, and then following the instructions in the FAQ and accompanying readme, i then went on to get lapack. i got lapack from the synaptic package manager (i am running ubuntu 8.04 hardy). as i was not sure which lapack package to get from the list, i installed the 2 that i thought were most likely: liblapack3gf, and, liblapack-dev.

    next, i cd-ed into the levmar folder in a terminal, then typed 'make'. this was what i got:

    Code:
    
    gcc   -O3 -funroll-loops -Wall    -c -o lm.o lm.c
    gcc   -O3 -funroll-loops -Wall    -c -o Axb.o Axb.c
    gcc   -O3 -funroll-loops -Wall    -c -o misc.o misc.c
    gcc   -O3 -funroll-loops -Wall    -c -o lmlec.o lmlec.c
    gcc   -O3 -funroll-loops -Wall    -c -o lmbc.o lmbc.c
    gcc   -O3 -funroll-loops -Wall    -c -o lmblec.o lmblec.c
    gcc   -O3 -funroll-loops -Wall    -c -o lmbleic.o lmbleic.c
    ar crv liblevmar.a lm.o Axb.o misc.o lmlec.o lmbc.o lmblec.o lmbleic.o
    a - lm.o
    a - Axb.o
    a - misc.o
    a - lmlec.o
    a - lmbc.o
    a - lmblec.o
    a - lmbleic.o
    ranlib liblevmar.a
    gcc   -O3 -funroll-loops -Wall    -c -o lmdemo.o lmdemo.c
    gcc -L/usr/local/lib  -L. lmdemo.o -o lmdemo -llevmar -llapack -lblas -lf2c  -lm
    /usr/bin/ld: cannot find -lf2c
    collect2: ld returned 1 exit status
    make: *** [lmdemo] Error 1
    may i know how to solve this problem?

    also, will the algorithm be usable after this, or am i missing some more steps? i would like to try out the expfit.c demo that comes with levmar.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Library for linking to fortran code. Since I don't see any fortran source files in the log you posted, try removing the -lf2c from the Makefile. Alternatively there may be a libf2c package on your system that provides a libf2c.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    45
    i think i will just use some routines from numerical recipes for my problem. levmar is a bit difficult for me at the moment. thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Curve Fitting problem (nonlinear regression)
    By regression in forum C++ Programming
    Replies: 14
    Last Post: 03-22-2010, 11:28 AM
  2. using basic library to write curve to bmp
    By CIO in forum C Programming
    Replies: 3
    Last Post: 02-21-2009, 02:25 PM
  3. The GNU Scientific Library - nonlinear fitting
    By lechat in forum C++ Programming
    Replies: 0
    Last Post: 12-03-2007, 02:58 PM
  4. Curve fitting
    By mark_dubo in forum C Programming
    Replies: 0
    Last Post: 05-15-2007, 05:29 AM
  5. effort, and the bell-shaped curve...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 08-18-2003, 12:13 AM