Search code examples
gnuplotellipsedata-fitting

Fitting ellipse to data using gnuplot


I need to make a graph of the equipotential lines between two electrodes based on data collected on lab using a multimeter, I have a collection of (x,y) coordinates divided by the voltage measured in them.

Points with 3,07 V

2.0   0.0
2.3   2.0
3.1   3.9
2.3  -2.0
3.1  -4.0

Points with 3,93 v

0.0   0.0
0.1   1.9
0.2   2.9
0.1  -1.0
0.2  -2.0

Points with 4,98 V

-2.0   0.0
-2.3   2.0
-2.4   2.9
-2.3  -2.0
-2.5  -3.0

I've tried to write the ellipsis in the parametric form and use the "fit" command to fit the ellipse, but the results are terrible.

set grid

set parametric
set trange [0:2*pi]

fx(t)=a*cos(t)+k
fy(t)=b*sin(t)+h
 
fit fx(t) '3,07.txt' via a,k
fit fy(t) '3,07.txt' via b,h

plot fx(t),fy(t) notitle ls 7, '3,07.txt' notitle 

I think It has to do with how the "fit" algorithm use the least square method, because It approaches for y and not for x. Am I right ?

I've also tried to fit the ellipse with Its general equation.

set grid

f(x, y) = x*x + c1*y*y + d1*x + e1*y + f1
fit f(x, y) '3,07.txt' u 1:2:(0) via ,c1,d1,e1,f1

set contour 
set view map 
unset surface 
set cntrparam levels discrete 0
set isosamples 1000,1000

set table 'contour 1.txt'
splot f(x, y)

set xr [-60:60]
set yr [-60:60]

unset table
unset contour

plot \
    'contour 1.txt'  lw 2 lc rgb 'red', \
    '3,07.txt'  w p ps 1.5 lc rgb 'black'
    

But got even worse results, does anyone have any idea of what else should I try ?


Solution

  • It is easy to understand why you got bad results. For example consider the first data (case 3.07V) :

    The points are not close to an ellipse but they are on an hyperbola. Any method of fitting leads to bad result if one want to fit an ellipse to an hyperbola, even approximately.

    enter image description here

    If the physical phenomena definitively implies that the curve is an ellipse of course one can fit an ellipse to the data. But don't be surprised by the large deviation between the experimental points and the ellipse. One have to accept it or to try again with more accurate and more numerous measurements.

    For information:

    The method of fittig used above is valid for ellipse, hyperbola and parabola. It is valid for any number of points (more than four). This allows to see which one of those curves is convenient for the best fit. See page 16 in https://fr.scribd.com/doc/14819165/Regressions-coniques-quadriques-circulaire-spherique. The above notations are consistent with the notations in this paper.