Search code examples
gnuplotinterpolation

Gnuplot cspline interpolation dataset at specified points


I have two files, say file 1

x        y
0.0    1.0
1.0    2.0
3.0    5.0

and file 2

x
0.5
1.815
2.5

I want to get smooth cspline interpolation values of file 1, but on the x-axis as specified by file 2. I know how we can set the table and sample size and get the output determined by sample size, but i want to have data points value at the points specified by second file. Is there a way to do it?


Solution

  • Suppose you have a file named data.dat containing this:

    #  x      y
     0.000  0.007
     1.111  0.013
     2.222  0.061
     3.333  0.164
     4.444  0.250
     5.556  0.273
     6.667  0.158
     7.778  0.061
     8.889  0.015
    10.000  0.018
    

    You know that this data are described by Gaussian function. On gnuplot it can been written as:

    g(x) = a*exp(-(x - b)**2/2*c**2)
    

    Suppose yet you have another file named x-values.dat containing the values which you want interpolate.

    # x
    0.923
    1.497
    2.357
    3.900
    4.396
    5.696
    6.658
    7.146
    8.851
    9.947
    

    You can use fit command to find a, b, and c values which fits data. At last you use the x-values.dat as input to Gaussian function.

    # The Gaussian function
    g(x) = a*exp(-(x - b)**2/2*c**2)
    
    # Initial values 
    a = 0.5     # Height of the curve's peak
    b = 6.0     # Position of the center of the peak
    c = 1.0     # Controls the width of the "bell"
    
    set fit prescale    # Helps to fit if there are parameters that 
                        # differ in size by many orders of magnitude
    
    # Performs the fit
    fit g(x) 'data.dat' u 1:2 via a, b, c
    
    # The graph itself:
    # 1: Data points
    # 2: Gaussian function after fit
    # 3: Gaussian function using values from 'x_values.dat' as input
    plot \
        'data.dat' u 1:2 w p pt 5 lc 'black' t 'Data',\
        g(x) w l lc 'red' t 'Gaussian',\
        'x_values.dat' u 1:(g($1)) w p pt 7 lc 'blue' t 'Interpolation'
    

    interpolated data