0
votes

How can I fit the data with f(x) = A*(sin(b*x)/(b*x))**2?

The data.dat file content is:

    -3.7 0.020505941
    -3.6 0.015109903
    -3.5 0.010044806
    -3.4 0.005648897
    -3.3 0.002285005
    -3.2 0.000332768
    -3.1 0.000179912
    -3 0.002212762
    -2.9 0.006806212
    -2.8 0.014313401
    -2.7 0.025055358
    -2.6 0.039310897
    -2.5 0.057307025
    -2.4 0.079210158
    -2.3 0.105118386
    -2.2 0.135055049
    -2.1 0.168963812
    -2 0.206705453
    -1.9 0.24805647
    -1.8 0.292709632
    -1.7 0.340276504
    -1.6 0.390291948
    -1.5 0.442220555
    -1.4 0.495464883
    -1.3 0.549375371
    -1.2 0.603261707
    -1.1 0.65640542
    -1 0.708073418
    -0.9 0.757532157
    -0.8 0.804062127
    -0.7 0.846972303
    -0.6 0.88561423
    -0.5 0.919395388
    -0.4 0.947791533
    -0.3 0.970357695
    -0.2 0.986737575
    -0.1 0.996671108
    0 1
    0.1 0.996671108
    0.2 0.986737575
    0.3 0.970357695
    0.4 0.947791533
    0.5 0.919395388
    0.6 0.88561423
    0.7 0.846972303
    0.8 0.804062127
    0.9 0.757532157
    1 0.708073418
    1.1 0.65640542
    1.2 0.603261707
    1.3 0.549375371
    1.4 0.495464883
    1.5 0.442220555
    1.6 0.390291948
    1.7 0.340276504
    1.8 0.292709632
    1.9 0.24805647
    2 0.206705453
    2.1 0.168963812
    2.2 0.135055049
    2.3 0.105118386
    2.4 0.079210158
    2.5 0.057307025
    2.6 0.039310897
    2.7 0.025055358
    2.8 0.014313401
    2.9 0.006806212
    3 0.002212762
    3.1 0.000179912
    3.2 0.000332768
    3.3 0.002285005
    3.4 0.005648897
    3.5 0.010044806
    3.6 0.015109903
    3.7 0.020505941
    3.8 0.025925906

My code for fitting below:

f(x) = A*(sin(b*x)/(b*x))**2;
A = 1;
b = 1;
fit f(x) "data.dat" u 1:2 via A,b;
plot [x=-3:3] f(x);

I got an error Undefined value during function evaluation.

1
Well, f(0) is undefined, right? - Zachary Vance
Yes, you completely right. - Sergio
@Zachary Vance But plot do not pay attention to problem with zero, but fit dies. Why? - Sergio

1 Answers

4
votes

I guess that unlike plot, fit doesn't ignore points for which the function being evaluated produces an undefined value. In your particular case, you might reformulate the problem and fit f(x)*x*x to y(x)*x*x in order to remove the "singularity" at zero. For example:

set terminal pngcairo
set output 'fig.png'

f(x) = A*(sin(b*x)/(b*x))**2;
g(x) = A*(sin(b*x)/(b))**2;

fit g(x) 'data.dat' u 1:($2*$1*$1) via A, b;

plot \
    g(x)/(x*x) t 'fit', \
    'data.dat' w p t 'points'

This produces: enter image description here