I have a simple linear fitting function as:
f(x) = m*x + b
however, the fitting end up having a negative intercept (b<0) which does not have any meaning in my use case.
I need to restrict the intercept to be only positive numbers. The code that I found
fit [b=0:] f(x) "name_of_the_file" u 1:2 via m, b
only works for x variable restriction, but not for any other parameter.
How can I limit the intercept to positive numbers?
You can try to modify your fitting function and replace b by c**2:
f(x) = m*x + c**2
fit f(x) "name_of_the_file" u 1:2 via m, c
Then you have a nonnegative b = c**2.
Related
I have this set of data that want to fit with gnuplot using the function f(x) = exp(A+ B/(x-x0)) where A,B and x0 are my set of parameters to fit
# x f(x)
0.382 8.29023731095968
0.509 6.36124122026352
0.637 4.66938977764103
0.764 3.3194714217965
0.891 2.15140777817893
1.019 1.15428884806615
1.146 0.262232461832655
I have try it with
fit log(f(x)) 'data.dat' using 1:(log($2)) via A, B, x0
also have defined the function as f(x) = A+ B/(x-x0) and tried with
fit f(x) 'data.dat' using 1:(log($2)) via A, B, x0
and then plot exp(f(x))
The code works but the fitted parameters are not fine, because when I plot the curve and the points together not make sense. Is this fit too complicate for gnuplot?
Fitting can fail if you have an inappropriate function or if you have starting values which might make it difficult for the fitting procedure to converge.
In your case, I guess x0 is an important parameter. You should help the gnuplot fitting algorithm a little to have a chance to find reasonable values. Here, I guess x0=1.5 is a reasonable starting value. If this is not sufficient and if your model permits you might want to add additional variables or terms to get a better fit.
Code:
### fitting with appropriate starting values
reset session
$Data <<EOD
0.382 8.29023731095968
0.509 6.36124122026352
0.637 4.66938977764103
0.764 3.3194714217965
0.891 2.15140777817893
1.019 1.15428884806615
1.146 0.262232461832655
EOD
A = 1
B = 1
x0 = 1.5
f(x) = exp(A + B/(x-x0))
set fit nolog
fit f(x) $Data u 1:2 via A,B,x0
plot $Data u 1:2 w lp pt 7 ti "Data",\
f(x) w l lc rgb "red" ti "Fit"
### end of code
Result:
Final set of parameters Asymptotic Standard Error
======================= ==========================
A = 4.61445 +/- 0.3907 (8.466%)
B = 3.57094 +/- 0.8876 (24.86%)
x0 = 1.80616 +/- 0.1371 (7.593%)
Is there a way to contraint the values that fitting parameters can take with gnuplot?
f(x) = A/(x**2) + B/(x**4)
A = 1
B = 0.01
fit f(x) 'data.dat' u 1:2 via A,B
I know that B < 0 doesn't make any sense. Is there a way to impose B > 0?
Since gnuplot supports non-linear fitting you can use B**2 (or sqrt(B**2)) in your function to constrain your variable to be positive.
You could change your function to something like this:
minB = 0.001
f(x) = A*x**-2 + (B<minB:minB:B)*x**-4
But i'm not sure how the NLLS alogrithm reacts to this. Beware.
Or you might think about something like this:
f(x) = A*x**-2 + 10**B*x**-4
Probably this will react much smoother and be closer to an actual physical model of your data.
I have some code in a Gnuplot file that does curve fitting, currently to a line:
f(x) = m * x + b
fit f(x) "data.txt" using "Days":"Data" via m, b
This works great, but the data looks like it will fit a log curve better. So I tried the following:
f(x) = b + m * log(x)
fit f(x) "data.txt" using "Days":"Data" via m, b
This results in the following error:
line 46: unknown type in real()
What am I doing wrong?
I just had a similar issue. My data set was small (8 points) on a semi-log plot. When trying to fit, I also received `
unknown type in real()`
I changed my initial parameter estimates and everything works. Hopefully, that helps.
I am trying to fit an asymptotic curve to my data using gnuplot. It is a dataset showing reaction time results over a testing period. I have been able to plot the data and fit a straight line through it using the following code.
f(x) = a*x + c;
fit f(x) 'ReactionLearning.txt' using 1:2 via a,c
plot 'ReactionLearning.txt' using 1:2 with points lt 1 pt 3 notitle, \
f(x) with lines notitle
Which gives the following result:
http://imgur.com/PlQmalX.jpg
However, as this is supposed to show a learning effect, an asymptotic curve would make a lot more sense because the increase in performance caused by a learning effect will eventually stop, making the line even out.
From what I understand asymptotic cuves are created with the f(x) = 1/x. So I changed my code to be
f(x) = 1/(a*x)
fit f(x) 'ReactionLearning.txt' using 1:2 via a
plot 'ReactionLearning.txt' using 1:2 with points lt 1 pt 3 notitle, \
f(x) with lines notitle
However, I get this output: http://imgur.com/PimTa1T
Could someone explain what I am doing wrong here?
Thanks
There are many curves that show an asymptotic behavior, and 1/x is probably not the one that comes most often when describing physical or biological processes. Usually, these processes might show some sort of exponential decay. With the data that you show I don't think you can conclude anything about which model you should use, other than "it decays". If you already know what is the functional behavior you expect, that makes things different. That said, the general form of your 1/x curve should be f(x) = a/(x-x0) + c, which will probably give you some meaningful results when you fit to it:
f(x) = a/(x-x0) + c
fit f(x) "data" via a,c,x0
Since fitting might show instabilities for this kind of function if the initial values are bad, you should/might need to provide sensible initial values or reformulate the problem as a linear relation. You can do the latter by a change of variable y = 1/(x - x0) and do the fitting for different values of x0. Record the error in the fit (which is output by gnuplot) for each of them and see how the error gets minimized as a function of x0: it should be quadratic about the optimum value. Something like this:
f(x) = a*x + c
x0 = 1. # give some value for x0
fit f(x) "data" u (1./($1-x0)):2 via a,c # record fit errors for a and c
x0 = 3. # give some other value for x0
fit f(x) "data" u (1./($1-x0)):2 via a,c # record fit errors for a and c
What is the proper way in gnuplot to fit a function f(x) having the next form?
f(x) = A*exp(x - B*f(x))
I tried to fit it as any other function using:
fit f(x) "data.txt" via A,B
and the output is just a sentence saying: "stack overflow"
I don't even know how to look for this topic so any help would be much appreciate it.
How are this kind of functions called? Nested? Recursive? Implicit?
Thanks
This doen't only fail for fitting, also for plotting. You'll have to write down the explicit form of f(x), otherwise gnuplot will loop it until it reaches its recursion limit. One way to do it would be to use a different name:
f(x) = sin(x) # for example
g(x) = A*exp(x - B*f(x))
And now use g(x) to fit, rather than f(x). If you have never declared f(x), then gnuplot doesn't have an expression to work with. In any case, if you want to recursively define a function, you'll at least need to set a recursion limit. Maybe something like this:
f0(x) = x
f1(x) = A*exp(x - B*f0(x))
f2(x) = A*exp(x - B*f1(x))
f3(x) = A*exp(x - B*f2(x))
...
This can be automatically looped:
limit=10
f0(x) = x
do for [i=1:limit] {
j=i-1
eval "f".i."(x) = A*exp(x - B*f".j."(x))"
}
Using the expression above you set the recursion limit with the limit variable. In any case it shall remain a finite number.
That is a recursive function. You need a condition for the recursion to stop, like a maximum number of iterations:
maxiter = 10
f(x, n) = (n > maxiter ? 0 : A*exp(x - B*f(x, n+1)))
fit f(x, 0) "data.txt" via A,B
Of course you must check, which value should be returned when the recursion is stopped (here I used 0)
Thanks for your replies
Discussing with a friend about this problem I found a way around.
First, this kind of functions are call "transcendental functions", that means that the function f(x) is not explicitly solvable, but the variable x could be solved as a function of f(x) and it will have the next form
x = B*f(x) + log(f(x)/A)
Therefore it is possible to define a new function (that is not transcendental)
g(x) = B*x + log(x/A)
From here you can fit the function g(x) to the plot x vs y. Using gnuplot it is possible to do the fitting as
fit g(x) "data.txt" using ($2):($1) via A,B
Hope this will help someone else