2
votes

I'm trying to fit my data to this curve:

f(x) = b + n*exp(-x/u)
fit f(x) "data1" using 2:3 via b,n,u

I get this fit which is not close to theory:

image of bad automatic fit

Then I use initial parameters from theory and without recomputing the fit I get a much nicer result.

If I recompute the fit, it ignores my initial parameters after the first iteration and goes back to the first result.

I try a couple of things. I reduce the FIT_LIMIT by 20 orders of magnitude but it doesn't change anything. I also bias the data with errors so that I put much more weight on the first few data points and then I get a somewhat better fit but it's still bad.

This is the output:

image of output of values

Questions:

  1. It's not clear to me whether the issue is mathematical or whether GNUplot is just doing things wrong by stopping at some local minimum or hitting some limit or some such thing. I've forgotten all about how chi-squared fit works.

  2. Is there some way to get a better automatic fit, ideally before resolving to include errors in my data?

3

3 Answers

3
votes

Two ideas:

  1. You seem to have a considerable number of data points around (0,0) which look like artifacts and probably affect your fit.

  2. Try fitting in the logarithmic domain, i.e.:

    f(x) = b + n*exp(-x/u)
    fit log(f(x)) "data1" using 2:(log($3)) via b,n,u
    

    This trick usually works if the large values dominate the fit – as their deviations from the fitted curve weigh less in the logarithmic domain. This does not seem to be the case for you yet, but may be after you address point 1.

2
votes

On what basis are you presuming that the fit is bad? You need more than your visual, subjective impression.

Here's your assumed function:

y = b + n*exp(-x/u)

Try fitting this function:

z = y-b = n*exp(-x/u)

Taking the natural log of both sides:

ln(z) = ln(n*exp(-x/u)) = ln(n) - x/u

This is a simple linear regression with dependent variable x, independent variable ln(z), intercept ln(n), and slope -1/u.

The problem is that your data appears to be asymptotic at x = 0. that would suggest a function of the form 1/x to me. Maybe the poor fit has to do with the choice of function.

You said "theory". What phenomenon does the data represent?

1
votes

Come one. Gnuplot is capable of very nice nonlinear fitting.

I would not recommend the linearization method: pen-and-paper method used before computers where only line throug graph of logarithmed points on millimeter paper was made by ruler and the coefficients were read from the paper. We don't have to do things old way because the old teachers know only this method. Let's exploit powers of computers.

Let's advise nonlinear regression with some starting guess of coefficients.

f(x) = b + n*exp(-x/u)
b = -5
n = 1.5
u = 15
fit log(f(x)) "data1" using 2:3 via b,n,u
plot "data1" using 2:3 with points, f(x) with line

adjust the constants to what you have in data and enjoy direct results even with the guess of confidence intervals and covariance matrix.