Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.graphics.apps.gnuplot > #2782 > unrolled thread

Fit 3D Line to data

Started byErik Zweigle <erikzweigle@gmail.com>
First post2015-02-22 22:27 -0800
Last post2015-02-25 19:38 +0100
Articles 10 — 5 participants

Back to article view | Back to comp.graphics.apps.gnuplot


Contents

  Fit 3D Line to data Erik Zweigle <erikzweigle@gmail.com> - 2015-02-22 22:27 -0800
    Re: Fit 3D Line to data Karl <mail.kfr@gmx.net> - 2015-02-24 10:36 +0100
    Re: Fit 3D Line to data Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2015-02-24 20:16 +0100
      Re: Fit 3D Line to data Karl <mail.kfr@gmx.net> - 2015-02-25 14:34 +0100
        Re: Fit 3D Line to data Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2015-02-25 19:25 +0100
        Re: Fit 3D Line to data Axel Berger <Axel_Berger@B.Maus.De> - 2015-02-28 03:02 +0100
          Re: Fit 3D Line to data Erik Zweigle <erikzweigle@gmail.com> - 2015-02-28 12:47 -0800
          Re: Fit 3D Line to data Karl <mail.kfr@gmx.net> - 2015-03-01 06:46 +0100
    Re: Fit 3D Line to data Gavin Buxton <gavinbuxton@gmail.com> - 2015-02-25 06:28 -0800
      Re: Fit 3D Line to data Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2015-02-25 19:38 +0100

#2782 — Fit 3D Line to data

FromErik Zweigle <erikzweigle@gmail.com>
Date2015-02-22 22:27 -0800
SubjectFit 3D Line to data
Message-ID<68e9ed33-a7ef-4106-8730-151263df2011@googlegroups.com>
Hello,

I am trying to fit a line to the 4 data points shown below.

377.4202, -345.5518, 2.1142
377.4201,	-345.5505, 2.5078
377.4206,	-345.556, 2.8359
377.4288, -345.5555, 3.2109

I only seem to be able to plot a plane with poor fitting.  Documentation doesn't mention parametric equation of line for 3D and it didn't work anyway.  Any pointers?

set datafile separator ","
set xlabel "X"
set ylabel "Y"
set zlabel "Z"

#set xrange [377.0:377.6]
#set yrange [-345.5:-346]
#set zrange [1.0:4.0]

f(x,y) = a*x + b*y + c

set title 'best fit trial'
fit f(x,y) 'data.csv' using 1:2:3 via a,b,c
splot 'data.csv' u 1:2:3 w p lt 3 ps 2, f(x,y)

[toc] | [next] | [standalone]


#2784

FromKarl <mail.kfr@gmx.net>
Date2015-02-24 10:36 +0100
Message-ID<mchgm3$a36$1@news.rz.uni-karlsruhe.de>
In reply to#2782
Am 23.02.2015 um 07:27 schrieb Erik Zweigle:
> Hello,
> 
> I am trying to fit a line to the 4 data points shown below.
> 
> 377.4202, -345.5518, 2.1142
> 377.4201,	-345.5505, 2.5078
> 377.4206,	-345.556, 2.8359
> 377.4288, -345.5555, 3.2109
> 
> f(x,y) = a*x + b*y + c
> 
> set title 'best fit trial'
> fit f(x,y) 'data.csv' using 1:2:3 via a,b,c
> splot 'data.csv' u 1:2:3 w p lt 3 ps 2, f(x,y)

a=59, b=-82, c=-5e4 ?

I'm afraid that´s the best you´ll get. Those four points simply don´t
lie on a plane.

[toc] | [prev] | [next] | [standalone]


#2785

FromHans-Bernhard Bröker <HBBroeker@t-online.de>
Date2015-02-24 20:16 +0100
Message-ID<cl40vhFs5kkU1@mid.dfncis.de>
In reply to#2782
Am 23.02.2015 um 07:27 schrieb Erik Zweigle:
> I am trying to fit a line to the 4 data points shown below.

That's not a well-defined task, I'm afraid.  Fitting a line to a dataset 
is reasonably well-defined in 2D, but not in 3D.

> 377.4202, -345.5518, 2.1142
> 377.4201,	-345.5505, 2.5078
> 377.4206,	-345.556, 2.8359
> 377.4288, -345.5555, 3.2109
>
> I only seem to be able to plot a plane with poor fitting.

Of course.  Those points don't even lie on any plane, much less on a 
single line.


[toc] | [prev] | [next] | [standalone]


#2786

FromKarl <mail.kfr@gmx.net>
Date2015-02-25 14:34 +0100
Message-ID<mckj1t$g7a$1@news.rz.uni-karlsruhe.de>
In reply to#2785
Am 24.02.2015 um 20:16 schrieb Hans-Bernhard Bröker:
> Am 23.02.2015 um 07:27 schrieb Erik Zweigle:
>> I am trying to fit a line to the 4 data points shown below.
> 
> That's not a well-defined task, I'm afraid.  Fitting a line to a dataset
> is reasonably well-defined in 2D, but not in 3D.
> 

Hm. I´d say you define the line with parameters x0,y0,z0 and two angles,
derive the minimum distance d(x,y,z) of a point xyz from that line, and
minimise that. Gnuplot´s fit can be abused for minimisations:


$dat << EOD
9 1
8 3
7 2
6 5
5 4
4 5
3 8
2 9
EOD

f(x) = a*x+b

a = 1
b = 1

plot $DAT, f(x)

# fit f(x) $dat using 1:2 via a,b # ordinary fix

fit f(x)-y $dat using 1:2:(0) via a,b # minimize y-distance fit

replot

Now you only need to replace f(x)-y by the function that returns the
minimum absolute distance and you´re done.

Not taking bets this is 100% mathematically correct, but i think it should.


  Karl


[toc] | [prev] | [next] | [standalone]


#2789

FromHans-Bernhard Bröker <HBBroeker@t-online.de>
Date2015-02-25 19:25 +0100
Message-ID<cl6icrFhlb2U2@mid.dfncis.de>
In reply to#2786
Am 25.02.2015 um 14:34 schrieb Karl:
> Am 24.02.2015 um 20:16 schrieb Hans-Bernhard Bröker:
>> Am 23.02.2015 um 07:27 schrieb Erik Zweigle:
>>> I am trying to fit a line to the 4 data points shown below.

>> That's not a well-defined task, I'm afraid.  Fitting a line to a dataset
>> is reasonably well-defined in 2D, but not in 3D.

> Hm. I´d say you define the line with parameters x0,y0,z0 and two angles,
> derive the minimum distance d(x,y,z) of a point xyz from that line, and
> minimise that.

While that's _one_ viable interpretation of the stated problem, it's far 
from being the only one.  Which is precisely what makes the task  not 
well-defined.

[toc] | [prev] | [next] | [standalone]


#2800

FromAxel Berger <Axel_Berger@B.Maus.De>
Date2015-02-28 03:02 +0100
Message-ID<54F121B1.43E0C8FD@B.Maus.De>
In reply to#2786
Karl wrote:
> derive the minimum distance d(x,y,z) of a point xyz from that line,

If that was what what a 2D regression did, it would be obvious how to
generalise to 3D. But it doesn't. It minmizes the squares of the
y-distances for given x values, which at very steep gradients is quite
different from the distance between points and line.

Axel

[toc] | [prev] | [next] | [standalone]


#2802

FromErik Zweigle <erikzweigle@gmail.com>
Date2015-02-28 12:47 -0800
Message-ID<2b07b72a-52e7-41dc-8d9b-74ad55b3cd22@googlegroups.com>
In reply to#2800
Thanks for all the pointers.  I abandoned a gnuplot fit and used perl to get 2D regression in XY projection and angle from XZ projection.

[toc] | [prev] | [next] | [standalone]


#2803

FromKarl <mail.kfr@gmx.net>
Date2015-03-01 06:46 +0100
Message-ID<mcu94g$2sq$1@news.rz.uni-karlsruhe.de>
In reply to#2800
Am 28.02.2015 um 03:02 schrieb Axel Berger:
> Karl wrote:
>> derive the minimum distance d(x,y,z) of a point xyz from that line,
> 
> If that was what what a 2D regression did, it would be obvious how to
> generalise to 3D. But it doesn't. It minmizes the squares of the
> y-distances for given x values, which at very steep gradients is quite
> different from the distance between points and line.

As said, you cannot directly fit the function defining your line, but
have to derive the function d(x,y,z) from the definition of your line,
and minimise that, by fitting it against zero.

Here´s the example to do exactly that for 2D data.

$dat << EOD
9 1
8 3
7 2
6 5
5 4
4 5
3 8
2 9
EOD

plot [0:10] $dat

# ordinary fit

f(x) = a*x+b; a=1; b=1
fit f(x) $dat using 1:2 via a,b


# minimisation of orthogonal distance

g(x) = y0 + tan(theta)*(x-x0)    # line definition with x0,y0,theta
d(x,y) = sqrt((x-x0)**2+(y-y0)**2) * sin(atan2(y-y0,x-x0)-theta)
# distance of point x,y from line defined by x0,y0,theta
# gives negative values for some points, but that doesn't matter here
# be careful when fitting: x0 and y0 are completely redundant

x0=5; y0=1; theta=1;

fit d(x,y) $dat using 1:2:(0) via y0,theta # minimize d(x,y)

replot f(x) title "f(x) via least squares regression" , \
       g(x) title "g(x) via minimisation of (d(x,y))^2"

[toc] | [prev] | [next] | [standalone]


#2788

FromGavin Buxton <gavinbuxton@gmail.com>
Date2015-02-25 06:28 -0800
Message-ID<4c6352e3-3fd5-4efc-a56e-7244067aece5@googlegroups.com>
In reply to#2782
Pick your 3 favourite points and you have a plane! Seriously though I suspect its because you have too few data points....

[toc] | [prev] | [next] | [standalone]


#2790

FromHans-Bernhard Bröker <HBBroeker@t-online.de>
Date2015-02-25 19:38 +0100
Message-ID<cl6j59Fi282U1@mid.dfncis.de>
In reply to#2788
Am 25.02.2015 um 15:28 schrieb Gavin Buxton:
> Pick your 3 favourite points and you have a plane! Seriously though I suspect its because you have too few data points....

That's the next part of the problem.  A straight line in 3D space has 4 
real degrees of freedom.  But I know of no implicit equation that really 
has just 4 parameters.  So there will be extraneous parameters in the 
fit, which would have to be cancelled out by constraints, to avoid 
interdependencies.

All that makes for an even more tricky job to set up for 'fit' than it 
already is.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.graphics.apps.gnuplot


csiph-web