Gnuplot fit of a nested function - gnuplot

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

Related

GNUPLOT - How to plot sum function that depends on limit?

I want to plot the following function in gnuplot:
f(x) = \sum_{i=1}^{x-1} 10^i
I tried using f(x)= (sum [i=1:x-1] 10 ** i), but it says "range specifiers of sum must have integer values".
Any idea how can I plot this sum function where the independent variable is in the limits? Thanks!
Make an integer out of your independent variable. Check help int. Maybe needs some changes depending on what exactly you want to have. Try:
f(x) = (sum [i=1:int(x-1)] 10**i)

Is there in prod() operator in gnuplot?

I am beginner with gnuplot and I am trying to plot the following function with gnuplot:
f(x) = sum[i=0:x](Pi[j=0:i](x+j-3))
where by Pi I mean the product operator:
Pi[j=0:i](x+j-3) = (x+0-3)*(x+1-3)...(x+i-3)
How can I write the gnuplot script for the Pi part?
if I didn't made mistakes you could use a recursive function:
prod(x,n,m) = (n<0) ? 1 : (x+n+m) * prod(x,n-1,m)
f(x) = sum[i=0:int(x)](prod(x,i,-3))
plot [0:3] f(x)
You can follow the recipe in this answer to use external functions, in this case Python. Create the external file function.py:
import sys
x=float(sys.argv[1])
i=int(sys.argv[2])
p = 1
for j in range(0,i+1):
p *= x + j - 3
print p
Now in gnuplot you can define the following product and sum functions:
prod(x,i) = real(system(sprintf("python function.py %g %i", x, i)))
f(x) = sum[i=0:int(x)](prod(x,i))
plot[0:3] f(x)
Note that x needs to be integer to be used to define the limits of the summation. Also note that calling external functions is quite slow.

Limit the intercept for fit with gnuplot

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.

Gnuplot: imposing bounds on fitting parameters

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.

Gnuplot: Fitting asymptotic curve to data

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

Resources