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.
Related
I'm making a c++ code which prints commands for gnuplot, in order to plot different things faster. The code plots the data already as the data fit as well, but now I'm adding some labels, and I want to print the fit equation, I mean something with this form
f(x) = (a +/- Δa)*x + (b +/- Δb)
I have the following line for printing it
set label 1 at screen 0.22, screen 0.75 sprintf('f(x) = %3.4f*x + %3.4f', a, b)
But, as you can see, there is only a and b values with no errors, I was thinking something like put there in the sprintf function any error related variables (FIT_something) and then have something like
set label 1 at screen 0.22, screen 0.75 sprintf('f(x) = (%3.4f +/- %3.4f)*x + (%3.4f + %3.4f)', a, deltaa, b, deltab)
But I can't find those, my answers are: does those exists? and if the answer is no, is there any way to print the variable errors further just writing it explicitly on the line?
Thanks for your help
Please read the statistical overview section of the gnuplot documentation (help statistical_overview). Keeping in mind the caveats described there, see also the documentation for set fit errorvariables, which I extract below:
If the `errorvariables` option is turned on, the error of each fitted
parameter computed by `fit` will be copied to a user-defined variable
whose name is formed by appending "_err" to the name of the parameter
itself. This is useful mainly to put the parameter and its error onto
a plot of the data and the fitted function, for reference, as in:
set fit errorvariables
fit f(x) 'datafile' using 1:2 via a, b
print "error of a is:", a_err
set label 1 sprintf("a=%6.2f +/- %6.2f", a, a_err)
plot 'datafile' using 1:2, f(x)
If the `errorscaling` option is specified, which is the default, the
calculated parameter errors are scaled with the reduced chi square. This is
equivalent to providing data errors equal to the calculated standard
deviation of the fit (FIT_STDFIT) resulting in a reduced chi square of one.
I plotted the logarithm of the pdf of the normal distribution.
Now i want to plot linear approximations of this function for different x-values (x=3). The problem is, that the returned values are not the expected ones.
set xrange[-4:4]
f(x) = 1/sqrt(2*pi*2**2) * exp(-x**2/(2*2**2))
g(x) = log(f(x))
h(x) = -0.5 * log(2*pi*2**2) - x**2/(2*2**2)
print(f(3))
print(g(3))
print(h(3))
plot h(x) with lines, \
h(3) with lines, \
# expected output
-2.737085713764618 with lines
I expect h(3) = -2.737085713764618, but get h(3) = -2.61208571376462 from gnuplot. I assume i am making a stupid mistake here. It would be great if someone could explain to me what is happening here.
Gnuplot uses integer division if the arguments are integers. To force floating point division you must provide real arguments.
f(x) = 1.0/sqrt(2*pi*2**2) * exp(-x**2/(2.0*2**2))
g(x) = log(f(x))
h(x) = -0.5 * log(2*pi*2**2) - x**2/(2.0*2**2)
print h(3.0)
-2.73708571376462
My data has two columns: date (in Month/Year format) and corresponding value. I plotted this data on x-log(y) scale using gnuplot. It looks very close to a straight line. I am interested to draw a straight line using curve fitting. I tried with few fit functions but did not get success.
I tried the following fit functions:
f(x) = a * x + b (f(x) is not linear as scale is x-log(y))
f(x) = a*10**x + b (overflow error)
Any help in this regard would be appreciated.
The overflow error should be due to at least one large value of x. If you can rescale the x data so that there is no overflow when calculating 10**x, the fit might work. As a test, try something like:
x_scaled = x / 1000.0
f(x_scaled) = a*10**x_scaled + b
Inspecting the maximum value of x will give you an idea of the scaling value, shown as 1000.0 in my example.
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.
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