Wednesday 8 September 2010

Curve fitting with gnuplot

Let's say you want to fit a curve to some messy experimental data. You can do this in gnuplot using the following:


# Define the shape of the fitting function. Here, I have used the
# Gauss error function as an example
f(x) = a * erf((x-x0)/b) + c

# Give some initial estimates for the fitting parameters
a = 1600
b = 1
c = 1600
x0 = 45

# This command fits your fitting function to the
# x and y values contained in columns 1 and 2
# of the data file "row-data.dat". The fitting
# parameters are listed after the "via" keyword.
fit f(x) "row-data.dat" using 1:2 via a,b,c,x0

# Plot your scattered data and the fitted curve
# on the same axes for comparison.
plot f(x), "row-data.dat"

# Dump a table of the results to "fit-plot.dat"
set table "fit-plot.dat"
plot f(x), "row-data.dat"
unset table


So, there you have it. This gnuplot script will fit a nice neat curve to your messy data. A log of the fitting process will be saved as "fit.log" and the plotting data will be saved as
"fit-plot.dat".

Quite often, you'll need to play with the initial estimates in order to get a good fit but
usually this works first time.

4 comments:

  1. Hi,

    I am new to using gnuplot although it was highly recommended. I am trying to fit data in the form:
    Nu = a1 * Re**a2 * Pr**a3;
    were my data is arranged in three colums being these (Re, Pr, Nu)

    I tried it with only two variables and it works just how it did for you but only have error when trying to make it work with two variables: f1 (x,y)
    Have you done something like this before? Any help would be appreciated.

    Best

    ReplyDelete
  2. Hi Francisco,

    Sorry about the slow reply... I didn't have email notifications set up.

    I have never tried f(x,y) fitting, but the manual suggests that you need to specify error estimates for the input data. If you don't have estimated error values, you can use a constant value, for example:

    fit f(x,y) "row-data.dat" using 1:2:3:(1) via a1,a2,a3

    Let me know if that's any use

    ReplyDelete
  3. Hi Alex,
    I was wondering if it was possible to import spectra to gnuplot and fit them?
    I would normally use mathematica to do that but the files I have this time have weird extension and presumably an info header (and I can't even open them to see what's inside)
    Also I should probably mention I have never used gnuplot before.

    Thanks in advance.

    ReplyDelete
  4. Hi Kate,

    The short answer is yes, you can fit any function (including spectral lineshapes) in Gnuplot. As for the specific data files you're talking about, it's hard to say without seeing it!

    My first suggestion would be to open it in a plain text editor and take a look! I've posted some advice elsewhere on this blog [1] about how to use bash scripts to convert horrible-looking data from lab equipment into a plain text table.

    [1] http://valavanis-research.blogspot.co.uk/2012/06/converting-lab-equipment-data-into.html

    ReplyDelete