Gnuplot fits nested function
What is the correct way in gnuplot to fit a function f(x)
that has the following shape?
f(x) = A*exp(x - B*f(x))
I tried to set it like any other function using:
fit f(x) "data.txt" via A,B
and the output is just a sentence: " stack overflow
"
I don't even know how to search for this thread, so any help would be greatly appreciated.
What are these functions called? Nested? Recursive? Implicit?
thank
source to share
This is not only for fitting but also for plotting. You will need to write f (x) explicitly, otherwise gnuplot will loop it until it reaches the recursion limit. One way to do this is 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 match, not f (x). If you've never declared f (x), then gnuplot has no expression to work with. In any case, if you want to define a function recursively, you will at least need to set the 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 above expression, you are setting the recursion limit with a variable limit
. In any case, it must remain a finite number.
source to share
This is a recursive function. You need a recursion stop condition, for example the 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 should check what value should be returned when recursion is stopped (I used here 0
)
source to share
thank you for your responses
While discussing with a friend about this issue, I found a workaround.
First, such functions are called "transcendental functions", which means that the function f (x) is not explicitly decidable, but the variable x can be solved as a function f (x), and it will have the following form
x = B * f (x) + log (f (x) / A)
Hence, it is possible to define a new function (not transcendental)
g (x) = B * x + log (x / A)
From here you can put the function g (x) in the x vs y graph. Using gnuplot one can make the fitting like
fit g (x) "data.txt" using ($ 2): ($ 1) via A, B
Hope this helps someone else
source to share