Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Undefined value during function evaluation

1,451 views
Skip to first unread message

schmidt...@gmail.com

unread,
Jun 27, 2017, 9:54:25 AM6/27/17
to
Gnuplot always ansers with this problem:
Current data point
=========================
# = 31 out of 55
x = 100000
z = 225

Current set of parameters
=========================
a = 100000
b = 0.00015
d = 30

"Einzelspalt.plt", line 10: Undefined value during function evaluation



My input is:

a=100000
b=0.15e-3
c=633e-9
d=30
e=880
f(x)=((a)**2)*((b)**2)*((c/(pi*b*sin(atan((x-d)/e))))**2)*((sin((pi*b*sin(atan((x-d)/e)))/c))**2)
fit f(x) 'Einzelspalt.txt' using ($1):($2) via a,b,d
plot f(x), 'Einzelspalt.txt' using ($1):($2)

If I comment the fit out, gnuplot plots the Funktion f(x) with the values for a,b,c,d,e that I have definied before. And it also plots the data of "Einzelspalt.txt". I tested it with a polynomial as f(x), what makes no physikal sense, and it worked. Gnoplot fitted that polynomial to my data and gave me the values.
Because it prints the function in case that I don´t want to fit it, I think gnuplot also "understands" f(x).
So I can not imagine what the problem is. Could anyone help me, please?

Karl Ratzsch

unread,
Jun 27, 2017, 12:01:30 PM6/27/17
to
(b^2 cancels out, if I did the math right, leaving b only in the last
sinus. So your function simplifies to

f(x) = a**2 * c**2 * (1/(pi*sin(atan((x-d)/e))))**2 *
(sin((pi*b*sin(atan((x-d)/e)))/c))**2

And gnuplot expects angles in radians, not degrees. I guess from your
starting value of "d" your datafile contains abscissa values in degrees,
right?

fit f(x) dataf us ($1/180*pi):2 via a,b,d,e
plot f(x), dataf us ($1/180*pi):2

)

Are the supplied parameters in the right region, i.e. is the function
already close to the data _before fitting_?

The function you give is complicated enough to never converge on your
data if the starting parameters are off. At some point most probably a
division by zero happened due to diverging parameter values.

Try predetermining the parameters using simpler functions e.g. only on a
part of the whole dataset, or by using the "stats" command:

stats "Einzelspalt.txt" us ($1/180*pi):2
d=STATS_pos_max_y
# run the fit without d
# then re-fit all variables



Karl




schmidt...@gmail.com

unread,
Jun 27, 2017, 3:40:51 PM6/27/17
to
Thank you Karl,
I will cancle the b, you are right.
But the angles are no Problem, because there are no angles in my data. I use the arcustangens to get the angle and then insert it into the sinus. So arctan((x-d)/e) is the angle and gnuplot will charge it in radiands. (x-d) and e have the unit mm, and their unit does not matter, because they cancle. But maybe there is a problem with the "big" sinus...
(sin((pi*b*sin(atan((x-d)/e)))/c))**2
Could be, you are right.
I will change to degree and try again, thank you.

And yes, the function is already close to the data before fitting.

I have to use this funktion for my data, there is no easier one for this problem.

Anna

Hans-Bernhard Bröker

unread,
Jun 27, 2017, 3:45:05 PM6/27/17
to
Am 27.06.2017 um 15:54 schrieb schmidt...@gmail.com:

> a=100000
> b=0.15e-3
> c=633e-9
> d=30
> e=880

> f(x)=((a)**2)*((b)**2)*((c/(pi*b*sin(atan((x-d)/e))))**2)*((sin((pi*b*sin(atan((x-d)/e)))/c))**2)

That's a somewhat suboptimal way of setting up your function. It would
make a lot more sense to do it more piecemeal:

# Let's refer to things by their usual name (some do without the pis...)
sinc(x)=sin(pi*x)/(pi*x)

# use atan2 instead of atan:
f1(x,d,e)=sin(atan2(x-d,e))
# Note: this doesn't really need to use trig functions.
# alternative:
#f1(x,d,e) = (x-d) / sqrt((x-d)**2 + e**2)

# Using these, and after spotting that f(x) consists of terms that are
# all_ squared, so the square can be moved to the outer edge, one gets:

f(x,a,b,c,d,e) = (a*b * sinc(b/c*f1(x,d,e)))**2

One thing this makes much easier to see is that there's a redundancy in
your parameters:

f(x,a*b,1,c/b,d,e)=f(x,a,b,c,d,e) for all k,x,a,...

Another thing more obvious is how this function's value can become
undefined: for x==d, f1(x,d,e) is zero, so the argument to sinc()
becomes zero, and you hit the function's undefined spot:


gnuplot> print f(d,a,b,c,d,e)
^
undefined value

To avoid that, you can use sinc()'s continuous extension instead:

sinc(x) = (x == 0) ? 1.0 : sin(pi*x)/(pi*x)

schmidt...@gmail.com

unread,
Jun 28, 2017, 10:16:43 AM6/28/17
to
Oh thank you! Now I´ve got the misstake.
You are right. The Problem was that gnuplot gets zero in the common denominator.
The Problem was, that I gave the start value d=30. In my data there was an x=30 and because of that gnuplot gets zero for x-d in the first run and fails.

I solved the problem with commenting that value out so that I got a better value for d, d=30.0398. Then I put the problematical x=30 back into my list of data and gave the new start value d=30.0398 to gnuplot. Then it fitted the Funktion.

Thank you both.

Fleur
0 new messages