Wrong fit parameters, Undefined value during function evaluation - gnuplot

I'm trying to do some simple fits using Gnuplot and encountering an error which somehow I'm not able to explain. Seems that I'm doing something terribly wrong with the definition of my function.
I first started with a fit of some functions. The functional form of these is not important as the fits work pretty well, but since they are involved later I'm reporting them as well.
evap(x)=(x>x_fc)?A*qnet(x)/wm2_to_mmh:(x<x_wp)?0:(A*qnet(x)/wm2_to_mmh)*(x-x_wp)/(x_fc-x_wp)
fit evap(x) './fluxes_da_1p0.txt' using 1:2 via A, x_fc, x_wp
sensible(x)=( (qnet(x)/wm2_to_mmh)-evap(x) ) / (1+B)+C
fit sensible(x) './fluxes_da_1p0.txt' using 1:3 via B,C
Then I defined
deltasensible(x)=abs(sensible(x)-sensible(0.454))
deltalatent(x)=abs(evap(x)-evap(0.454))
period_adv=18.0
g(x)=m*deltalatent(x)*period_adv*(deltasensible(x))**e
If I impose
m=3.8
e=0.5
and just plot the function g(x) superimposed on the data it seems to work pretty well.
However if I try to fit it using the parameters
fit g(x) './advection_da_1p0.txt' using 5:2 via m,e
I get the following error
iter chisq delta/lim lambda m e
0 4.2471983038e+00 0.00e+00 5.51e+00 3.800000e+00 5.000000e-01
1 3.7149813491e+00 -1.43e+04 5.51e-01 3.573384e+00 4.843345e-01
Current data point
=========================
# = 8 out of 8
x = 0.0178156
z = 0.0309726
Current set of parameters
=========================
m = 0.0178156
e = -0.0583802
"rain_estimate.gpl", line 52: Undefined value during function evaluation
For some reason Gnuplot is trying out really strange values for m,e, although I provided their initial values. If I remove the deltalatent(x) dependency everything is working fine. Am I missing something simple?

So I think I figured it out the problem by myself. Given my data a line was the best fit for Gnuplot so that it was trying to remove the exponential dependency on deltasensible by choosing a really small coefficient. Seems more an "error" with the data than with Gnuplot itself.

Related

Adding datapoints to a boxplot

R beginner here so sorry for the basic question... I have worked in class to draw some boxplots using the boxplot() function. As part of the assignment I've been asked to add data points to the boxplots. I can't seem to find an argument in the boxplot() function to do this...Is there another function I need to use? Thank you!
I recommend looking into ggplot2 package within R.
You can create multiple visualizations, including a boxplot with the data points shown. This is the ggplot code to do so:
library(ggplot2)
ggplot(df, aes(x=catvar, y=yvar)) +
geom_boxplot()+geom_dotplot(binaxis='y', stackdir='center', dotsize=1)

How to convert integer into string gnuplot

My code is the following, I would like a to assume iteratives values like '3' , '4' and so on. My code is like:
a=2
#perform some basic operation like:
b=a*2
#convert it to string
c=str(b)
p path1 u 1:($1<=0?$#c:1/0) w filledcurves y=0
The solution proposed on similar topic here so far did not work.
You should use this syntax
c=''.b
c will be equal to 'b'
gnuplot provides both sprintf (as in the C language routine) and a private implementation gprintf that offers formatting options beyond the normal ones provided by the C language. The full details with all supported format options are in the gnuplot documentation. A very simple use would be:
c = sprintf("%8.3f", b)
However, it makes no sense to convert the value to a string if your intent is to use it in a plot command that expects a number. There is no iteration in the pseudo-code you show so I can't guess exactly where you are headed with this but note that the operation #c to evaluate c as a macro expansion inside an iteration will always yield the content of c as it was prior to the iteration. So using the # operator inside an iteration is almost always wrong.
If I correctly understand your minimal incomplete non-working example, I can only guess what you probably wanted to do, i.e. limit the plot of a dataset in a certain column which is selected by some variable. Correct? If my guess is true, I would do it the following way, no need for string conversion and the use of macros. Check help column.
a=2
# perform some basic operation like:
b=a*2
plot path1 u 1:($1<=0 ? column(b) : NaN) w filledcurves y=0

python define style/vairable for line (but not for figure)

I very often plot two types of results - e.g. prediction vs measurement - into a figure for a document, where the look of the corresponding lines/scatters should be match each other in different plots, but at the beginning of the writing/plotting the final look is not decided. I would like to define a bunch of plot options for every such curve, to make it possible to replot them very efficiently.
For example a would like to define the styles like:
s_theory = [linestyle="--", color="grey", marker=None, label="simulation"]
s_measurement = [linestyle=":", color="black", marker="s", markersize="5",label="measurement"]
I would like to apply these magically on plt.plot():
plt.plot(xt,yt,**s_theory)
plt.plot(xm,ym,**s_measurement)
How can I do that? What is the magic word I did not found during my search for this task? I am pretty sure there is a very simple to do that.
Based on the comment of ImportanceOfBeingErnest:
style_sim = {"linestyle":"--", "color":"grey", "marker":"None", "label":"simulation"}
style_meas = {"linestyle":":", "color":"black", "marker":"s", "markersize":5, "label":"measurement"}
plt.plot(xt,yt,**style_sim)
plt.plot(xm,ym,**style_meas)
If you find it useful, please vote up the comment of ImportanceOfBeingErnest!

GNUplot fit with only x-errors in version 5?

I'm trying to fit my data using GNUplot. As it happens my data only has x-error bars. I heard version 5 is supposed to allow fitting using x errors only. How can one do this? I tried the following but as you can see I get errors that I cannot figure out:
gnuplot> fit f(x) "data1m" using 2:3:4 with xerrorbars via b,u,n
warning:
> Implied independent variable y not found in fit function.
> Assuming version 4 syntax with zerror in column 3 but no zerror keyword.
^
Need via and either parameter list or file
How can I resolve this?
It doesn't seem to work with x errors only, but you could use a very small constant y error.
Try fit f(x) "data1m" using 2:3:4:(1E-38) xyerrors via b,u,n

How to define a function without arguments in gnuplot?

I want to define a function which returns a random number:
gnuplot> rnd() = int(rand(0)*2**24)
^
invalid command
I need add a parameter x to stop the error message:
gnuplot> rnd(x) = int(rand(0)*2**24)
Is it possible to define a function without parameters?
I don't think that is possible (look at the syntax for functions in help user-defined). As far as I can tell -- using rand is the only time when this is useful -- In all other cases your "function" would just evaluate to a constant. I'm guessing the gnuplot devs just haven't thought of this (interesting) corner case, or they didn't think it useful enough to implement.
I hope that I'm wrong about this and somebody else comes along and proves it, but I don't think I am. I think the easiest workaround it to just pass a parameter as you've already mentioned. If you really don't like that, you could use a macro:
set macro
rnd="(int(rand(0)*2**24))"
print 5+#rnd

Resources