1
votes

I am trying to fit a sinc function with gnuplot but it fails with the message:

'Undefined value during function evaluation'. 

First my data:

27      9.3
27.2    9.3
27.8    9.3
29      9.4
32      9.5
34      9.6
34.2    9.7
34.4    9.7
34.6    9.8
34.8    10.1
35      10.9
35.2    12.9
35.4    16.1
35.6    21.1
35.8    26.5
36      31.8
36.2    34.7
36.4    36.6
36.6    36.3
36.8    32.3
37      26.4
37.2    20.6
37.4    15.4
37.6    11.6
37.8    9.9
38      9.6
38.5    10
39      9.5
39.5    9.5
40      9.6

What I am trying to do in Gnuplot:

sinc(x)=sin(pi*x)/pi/x
f(x)=a*(sinc((b*(x-c))))**2+d
fit f(x) '4_temp.txt' via a,b,c,d

I set a,b,c,d close to the values that are needed (see picture) but it wont fit.

Somebody can help? Thanks in advance.

enter image description here

1
Did you provide starting values for a,b,c,d? - Ethan

1 Answers

0
votes

I can reproduce your error message. You are trying to fit a sin(x)/x function. For x=0 you will get 0/0, although, gnuplot has no problems to plot sin(x)/x, apparently, fitting has a problem with this. Only if you add a little offset, e.g. 1e-9, it seems to work and it will find some reasonable parameters. As @Ethan says, you need to choose some starting values which should not be too far away from the final values. You will get the fitted values:

Final set of parameters            Asymptotic Standard Error
=======================            ==========================
a               = 27.5271          +/- 0.2822       (1.025%)
b               = 0.608263         +/- 0.006576     (1.081%)
c               = 36.3954          +/- 0.00657      (0.01805%)
d               = 9.21346          +/- 0.127        (1.379%)

Code:

### fitting type of sin(x)/x function
reset session

$Data <<EOD
27     9.3
27.2    9.3
27.8    9.3
29      9.4
32      9.5
34      9.6
34.2    9.7
34.4    9.7
34.6    9.8
34.8    10.1
35      10.9
35.2    12.9
35.4    16.1
35.6    21.1
35.8    26.5
36      31.8
36.2    34.7
36.4    36.6
36.6    36.3
36.8    32.3
37      26.4
37.2    20.6
37.4    15.4
37.6    11.6
37.8    9.9
38      9.6
38.5    10
39      9.5
39.5    9.5
40      9.6
EOD

a=25
b=1
c=36
d=10

sinc(x)=sin(pi*x)/pi/(x)
f(x)=a*(sinc((b*(x-c+1e-9))))**2+d
set fit nolog
fit f(x) $Data via a,b,c,d

plot $Data u 1:2 w p pt 7, f(x) w l lc rgb "red"
### end of code

Result:

enter image description here