Search code examples
gnuplot

3D fitting with gnuplot


I have the following function

s(x,y,z) = e0 + t*(cos(x)*cos(y) + cos(y)*cos(z) + cos(z)*cos(x))

which maps three data points x,y,z onto a single energy (this is the dispersion of an fcc crystal in the tight-binding approximation). The problem is this: I have a datafile "test.dat" which is formatted like

x1 y1 z1 E1
...

a section of which is

0.000000 1.000000 0.500000 4.429
-0.016667 0.983333 0.500000 4.363
-0.033333 0.966667 0.500000 4.206
-0.050000 0.950000 0.500000 4.013
-0.066667 0.933333 0.500000 3.809
-0.083333 0.916667 0.500000 3.604
-0.100000 0.900000 0.500000 3.402
-0.116667 0.883333 0.500000 3.205
-0.133333 0.866667 0.500000 3.015
-0.150000 0.850000 0.500000 2.831
-0.166667 0.833333 0.500000 2.656
-0.183333 0.816667 0.500000 2.489
-0.200000 0.800000 0.500000 2.329
-0.216667 0.783333 0.500000 2.178
-0.233333 0.766667 0.500000 2.035
-0.250000 0.750000 0.500000 1.901
-0.266667 0.733333 0.500000 1.775
-0.283333 0.716667 0.500000 1.658
-0.300000 0.700000 0.500000 1.549
-0.316667 0.683333 0.500000 1.449
-0.333333 0.666667 0.500000 1.357
-0.350000 0.650000 0.500000 1.275
-0.366667 0.633333 0.500000 1.201
-0.383333 0.616667 0.500000 1.135
-0.400000 0.600000 0.500000 1.078
-0.416667 0.583333 0.500000 1.030
-0.433333 0.566667 0.500000 0.991
-0.450000 0.550000 0.500000 0.961
-0.466667 0.533333 0.500000 0.939
-0.483333 0.516667 0.500000 0.926
-0.500000 0.500000 0.500000 0.921
-0.483333 0.508333 0.508333 0.925
-0.466667 0.516667 0.516667 0.934
-0.450000 0.525000 0.525000 0.951
-0.433333 0.533333 0.533333 0.974
-0.416667 0.541667 0.541667 1.003
-0.400000 0.550000 0.550000 1.039
-0.383333 0.558333 0.558333 1.082
-0.366667 0.566667 0.566667 1.131
-0.350000 0.575000 0.575000 1.186
-0.333333 0.583333 0.583333 1.249
-0.316667 0.591667 0.591667 1.317
-0.300000 0.600000 0.600000 1.392
-0.283333 0.608333 0.608333 1.474
-0.266667 0.616667 0.616667 1.562
-0.250000 0.625000 0.625000 1.657
-0.233333 0.633333 0.633333 1.758
-0.216667 0.641667 0.641667 1.865
-0.200000 0.650000 0.650000 1.979
-0.183333 0.658333 0.658333 2.099
-0.166667 0.666667 0.666667 2.225
-0.150000 0.675000 0.675000 2.357
-0.133333 0.683333 0.683333 2.495
-0.116667 0.691667 0.691667 2.639
-0.100000 0.700000 0.700000 2.788
-0.083333 0.708333 0.708333 2.941
-0.066667 0.716667 0.716667 3.096
-0.050000 0.725000 0.725000 3.250
-0.033333 0.733333 0.733333 3.387
-0.016667 0.741667 0.741667 3.469
0.000000 0.750000 0.750000 3.450

The x1,y1,z1,... are inputs to s(x,y,z); the E1,... are known energies. I would like to fit my function s(x,y,z) to the E1,..., using the row number in the datafile as the independent variable, and taking e0,t as parameters of the fit. I have tried

s(x,y,z) = e0 + t*(cos(x)*cos(y) + cos(y)*cos(z) + cos(z)*cos(x))
e0 = 14.5
t = -5.7

fit s(x,y,z) "test.dat" 0:(s($1,$2,$3)-$4) via e0,t

plot 'test.dat' u 0:(s($1,$2,$3)) w l , 'test.dat' u 0:4 w p

but this threw an error. Does anyone know if this is possible to do?


Solution

  • If my understanding is correct, it seems you have to explicitly define z as dummy variable. Then you have x,y,z (or columns 1,2,3) as independent input variables and then you try to fit your function s(x,y,z) to column 4.

    Script:

    ### fit with 3 independent variables
    reset session
    
    $Data <<EOD
    0.000000 1.000000 0.500000 4.429
    -0.016667 0.983333 0.500000 4.363
    -0.033333 0.966667 0.500000 4.206
    -0.050000 0.950000 0.500000 4.013
    -0.066667 0.933333 0.500000 3.809
    -0.083333 0.916667 0.500000 3.604
    -0.100000 0.900000 0.500000 3.402
    -0.116667 0.883333 0.500000 3.205
    -0.133333 0.866667 0.500000 3.015
    -0.150000 0.850000 0.500000 2.831
    -0.166667 0.833333 0.500000 2.656
    -0.183333 0.816667 0.500000 2.489
    -0.200000 0.800000 0.500000 2.329
    -0.216667 0.783333 0.500000 2.178
    -0.233333 0.766667 0.500000 2.035
    -0.250000 0.750000 0.500000 1.901
    -0.266667 0.733333 0.500000 1.775
    -0.283333 0.716667 0.500000 1.658
    -0.300000 0.700000 0.500000 1.549
    -0.316667 0.683333 0.500000 1.449
    -0.333333 0.666667 0.500000 1.357
    -0.350000 0.650000 0.500000 1.275
    -0.366667 0.633333 0.500000 1.201
    -0.383333 0.616667 0.500000 1.135
    -0.400000 0.600000 0.500000 1.078
    -0.416667 0.583333 0.500000 1.030
    -0.433333 0.566667 0.500000 0.991
    -0.450000 0.550000 0.500000 0.961
    -0.466667 0.533333 0.500000 0.939
    -0.483333 0.516667 0.500000 0.926
    -0.500000 0.500000 0.500000 0.921
    -0.483333 0.508333 0.508333 0.925
    -0.466667 0.516667 0.516667 0.934
    -0.450000 0.525000 0.525000 0.951
    -0.433333 0.533333 0.533333 0.974
    -0.416667 0.541667 0.541667 1.003
    -0.400000 0.550000 0.550000 1.039
    -0.383333 0.558333 0.558333 1.082
    -0.366667 0.566667 0.566667 1.131
    -0.350000 0.575000 0.575000 1.186
    -0.333333 0.583333 0.583333 1.249
    -0.316667 0.591667 0.591667 1.317
    -0.300000 0.600000 0.600000 1.392
    -0.283333 0.608333 0.608333 1.474
    -0.266667 0.616667 0.616667 1.562
    -0.250000 0.625000 0.625000 1.657
    -0.233333 0.633333 0.633333 1.758
    -0.216667 0.641667 0.641667 1.865
    -0.200000 0.650000 0.650000 1.979
    -0.183333 0.658333 0.658333 2.099
    -0.166667 0.666667 0.666667 2.225
    -0.150000 0.675000 0.675000 2.357
    -0.133333 0.683333 0.683333 2.495
    -0.116667 0.691667 0.691667 2.639
    -0.100000 0.700000 0.700000 2.788
    -0.083333 0.708333 0.708333 2.941
    -0.066667 0.716667 0.716667 3.096
    -0.050000 0.725000 0.725000 3.250
    -0.033333 0.733333 0.733333 3.387
    -0.016667 0.741667 0.741667 3.469
    0.000000 0.750000 0.750000 3.450
    EOD
    
    s(x,y,z) = e0 + t*(cos(x)*cos(y) + cos(y)*cos(z) + cos(z)*cos(x))
    e0 = 14.5
    t = -5.7
    set dummy x,y,z
    set fit brief nolog
    fit s(x,y,z) $Data u 1:2:3:4 via e0,t
    set palette rgb 33,13,10
    
    splot $Data u 1:2:3:4 w p pt 7 lc palette 
    ### end of script
    

    Result:

    With your data subset you provided you will get the following fitted values for e0 and t.

    Final set of parameters            Asymptotic Standard Error
    =======================            ==========================
    e0              = 21.3695          +/- 0.1207       (0.5649%)
    t               = -8.84404         +/- 0.05524      (0.6246%)
    

    The "3D"-plot represents column 4 as color from a palette.

    enter image description here