Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Linear regress

Subject: Linear regress

From: Toan Cao

Date: 13 Oct, 2010 01:24:03

Message: 1 of 9

hi, everybody
I have a set of data points in 2D: (x,y)
I would like to fit them into a straight line that is described by formula: x.cosθ +y.sinθ= ρ, instead of: y=ax+b
How can i find two parameters : θ, ρ
Looking forward to your reply!
Thank you very much !

Subject: Linear regress

From: Ross W

Date: 13 Oct, 2010 02:03:04

Message: 2 of 9

"Toan Cao" <cvt_ttvd@yahoo.com> wrote in message <i931nj$qr8$1@fred.mathworks.com>...
> hi, everybody
> I have a set of data points in 2D: (x,y)
> I would like to fit them into a straight line that is described by formula: x.cosθ +y.sinθ= ρ, instead of: y=ax+b
> How can i find two parameters : θ, ρ
> Looking forward to your reply!
> Thank you very much !

You can rewrite your x.cosθ +y.sinθ= ρ so that it is in the form y = (-cotθ).x+ρ/sinθ
That's just y=ax+b in disguise. Do you see what to do now?

Ross

Subject: Linear regress

From: John D'Errico

Date: 13 Oct, 2010 02:14:05

Message: 3 of 9

"Toan Cao" <cvt_ttvd@yahoo.com> wrote in message <i931nj$qr8$1@fred.mathworks.com>...
> hi, everybody
> I have a set of data points in 2D: (x,y)
> I would like to fit them into a straight line that is described by formula: x.cosθ +y.sinθ= ρ, instead of: y=ax+b
> How can i find two parameters : θ, ρ
> Looking forward to your reply!
> Thank you very much !

Simple, although it depends on whether you want an
orthogonal regression line, or the standard linear
regression line. The standard linear regression assumes
all of the errors lie in y, with x being exactly known.
Orthogonal regression allows error in both x and y.

The standard linear regression model, in the form
y = a*x + b, is given by

   ab = polyfit(x,y,1);
   a = ab(1);
   b = ab(2);

To recover the parameters theta and rho, we must
scale things properly.

   theta = atan2(1,-a);
   rho = b./sqrt(1 + a.^2);

If you have an orthogonal regression, the computation
is similarly easy.

John

Subject: Linear regress

From: Toan Cao

Date: 13 Oct, 2010 02:23:04

Message: 4 of 9

"Ross W" <rosswoodskiwi@hotmail.com> wrote in message <i9340o$nae$1@fred.mathworks.com>...
> "Toan Cao" <cvt_ttvd@yahoo.com> wrote in message <i931nj$qr8$1@fred.mathworks.com>...
> > hi, everybody
> > I have a set of data points in 2D: (x,y)
> > I would like to fit them into a straight line that is described by formula: x.cosθ +y.sinθ= ρ, instead of: y=ax+b
> > How can i find two parameters : θ, ρ
> > Looking forward to your reply!
> > Thank you very much !
>
> You can rewrite your x.cosθ +y.sinθ= ρ so that it is in the form y = (-cotθ).x+ρ/sinθ
> That's just y=ax+b in disguise. Do you see what to do now?
>
> Ross

Thank you for your reply, Ross
However, there is a problem with form y = (-cotθ).x+ρ/sinθ if my fitting line is in horizontal direction (i mean : sinθ=0 ) !
I'm confused about this problem, how is about your opinion ?

Subject: Linear regress

From: John D'Errico

Date: 13 Oct, 2010 02:34:05

Message: 5 of 9

"Toan Cao" <cvt_ttvd@yahoo.com> wrote in message <i93568$9d8$1@fred.mathworks.com>...
> "Ross W" <rosswoodskiwi@hotmail.com> wrote in message <i9340o$nae$1@fred.mathworks.com>...
> > "Toan Cao" <cvt_ttvd@yahoo.com> wrote in message <i931nj$qr8$1@fred.mathworks.com>...
> > > hi, everybody
> > > I have a set of data points in 2D: (x,y)
> > > I would like to fit them into a straight line that is described by formula: x.cosθ +y.sinθ= ρ, instead of: y=ax+b
> > > How can i find two parameters : θ, ρ
> > > Looking forward to your reply!
> > > Thank you very much !
> >
> > You can rewrite your x.cosθ +y.sinθ= ρ so that it is in the form y = (-cotθ).x+ρ/sinθ
> > That's just y=ax+b in disguise. Do you see what to do now?
> >
> > Ross
>
> Thank you for your reply, Ross
> However, there is a problem with form y = (-cotθ).x+ρ/sinθ if my fitting line is in horizontal direction (i mean : sinθ=0 ) !
> I'm confused about this problem, how is about your opinion ?

As I suggested in my response...

Suppose that the regression line is of the form

   a*x + b*y = c

Divide by sqrt(a^2 + b^2). Now recover theta. atan2
is best for this.

   theta = atan2(b,a);
   rho = c./sqrt(a^2 + b^2);

John

Subject: Linear regress

From: Toan Cao

Date: 13 Oct, 2010 02:58:05

Message: 6 of 9

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <i935qt$kkb$1@fred.mathworks.com>...
> "Toan Cao" <cvt_ttvd@yahoo.com> wrote in message <i93568$9d8$1@fred.mathworks.com>...
> > "Ross W" <rosswoodskiwi@hotmail.com> wrote in message <i9340o$nae$1@fred.mathworks.com>...
> > > "Toan Cao" <cvt_ttvd@yahoo.com> wrote in message <i931nj$qr8$1@fred.mathworks.com>...
> > > > hi, everybody
> > > > I have a set of data points in 2D: (x,y)
> > > > I would like to fit them into a straight line that is described by formula: x.cosθ +y.sinθ= ρ, instead of: y=ax+b
> > > > How can i find two parameters : θ, ρ
> > > > Looking forward to your reply!
> > > > Thank you very much !
> > >
> > > You can rewrite your x.cosθ +y.sinθ= ρ so that it is in the form y = (-cotθ).x+ρ/sinθ
> > > That's just y=ax+b in disguise. Do you see what to do now?
> > >
> > > Ross
> >
> > Thank you for your reply, Ross
> > However, there is a problem with form y = (-cotθ).x+ρ/sinθ if my fitting line is in horizontal direction (i mean : sinθ=0 ) !
> > I'm confused about this problem, how is about your opinion ?
>
> As I suggested in my response...
>
> Suppose that the regression line is of the form
>
> a*x + b*y = c
>
> Divide by sqrt(a^2 + b^2). Now recover theta. atan2
> is best for this.
>
> theta = atan2(b,a);
> rho = c./sqrt(a^2 + b^2);
>
> John

Thanks, John !
But if we follow your suggestion, i think we will encounter with vertical fitting line in finding a,b because parameter 'a' is infinite.
Do you think so ?! and how to solve this ?!

Subject: Linear regress

From: Steven_Lord

Date: 13 Oct, 2010 13:56:10

Message: 7 of 9



"Toan Cao" <cvt_ttvd@yahoo.com> wrote in message
news:i9377t$kqp$1@fred.mathworks.com...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message
> <i935qt$kkb$1@fred.mathworks.com>...
>> "Toan Cao" <cvt_ttvd@yahoo.com> wrote in message
>> <i93568$9d8$1@fred.mathworks.com>...
>> > "Ross W" <rosswoodskiwi@hotmail.com> wrote in message
>> > <i9340o$nae$1@fred.mathworks.com>...
>> > > "Toan Cao" <cvt_ttvd@yahoo.com> wrote in message
>> > > <i931nj$qr8$1@fred.mathworks.com>...
>> > > > hi, everybody
>> > > > I have a set of data points in 2D: (x,y)
>> > > > I would like to fit them into a straight line that is described by
>> > > > formula: x.cosθ +y.sinθ= ρ, instead of: y=ax+b
>> > > > How can i find two parameters : θ, ρ
>> > > > Looking forward to your reply!
>> > > > Thank you very much !
>> > >
>> > > You can rewrite your x.cosθ +y.sinθ= ρ so that it is
>> > > in the form y = (-cotθ).x+ρ/sinθ
>> > > That's just y=ax+b in disguise. Do you see what to do now?
>> > >
>> > > Ross
>> >
>> > Thank you for your reply, Ross
>> > However, there is a problem with form y =
>> > (-cotθ).x+ρ/sinθ if my fitting line is in horizontal
>> > direction (i mean : sinθ=0 ) !
>> > I'm confused about this problem, how is about your opinion ?
>>
>> As I suggested in my response...
>>
>> Suppose that the regression line is of the form
>>
>> a*x + b*y = c
>>
>> Divide by sqrt(a^2 + b^2). Now recover theta. atan2
>> is best for this.
>>
>> theta = atan2(b,a);
>> rho = c./sqrt(a^2 + b^2);
>>
>> John
>
> Thanks, John !
> But if we follow your suggestion, i think we will encounter with vertical
> fitting line in finding a,b because parameter 'a' is infinite.
> Do you think so ?! and how to solve this ?!

Preprocess your data and determine if all your points have the same (or very
close to the same) x coordinate. If they have _exactly_ the same x
coordinate, then you know what your equation should be. If they have _very
close to_ the same x coordinate, then you know that the best fit line is
likely to be very close to a vertical line, and again you know what the
equation should be. If neither of those conditions are true, then perform
your regression as suggested by previous posters.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Linear regress

From: Bruno Luong

Date: 13 Oct, 2010 15:38:04

Message: 8 of 9

mx = mean(x);
my = mean(y);
[U S V] = svd([x(:)-mx y(:)-my]);
theta=atan2(V(2,2),V(1,2));
rho = mx*cos(theta)+my*sin(theta)

Bruno

Subject: Linear regress

From: John D'Errico

Date: 13 Oct, 2010 15:56:04

Message: 9 of 9

"Toan Cao" <cvt_ttvd@yahoo.com> wrote in message <i9377t$kqp$1@fred.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <i935qt$kkb$1@fred.mathworks.com>...
> > "Toan Cao" <cvt_ttvd@yahoo.com> wrote in message <i93568$9d8$1@fred.mathworks.com>...
> > > "Ross W" <rosswoodskiwi@hotmail.com> wrote in message <i9340o$nae$1@fred.mathworks.com>...
> > > > "Toan Cao" <cvt_ttvd@yahoo.com> wrote in message <i931nj$qr8$1@fred.mathworks.com>...
> > > > > hi, everybody
> > > > > I have a set of data points in 2D: (x,y)
> > > > > I would like to fit them into a straight line that is described by formula: x.cosθ +y.sinθ= ρ, instead of: y=ax+b
> > > > > How can i find two parameters : θ, ρ
> > > > > Looking forward to your reply!
> > > > > Thank you very much !
> > > >
> > > > You can rewrite your x.cosθ +y.sinθ= ρ so that it is in the form y = (-cotθ).x+ρ/sinθ
> > > > That's just y=ax+b in disguise. Do you see what to do now?
> > > >
> > > > Ross
> > >
> > > Thank you for your reply, Ross
> > > However, there is a problem with form y = (-cotθ).x+ρ/sinθ if my fitting line is in horizontal direction (i mean : sinθ=0 ) !
> > > I'm confused about this problem, how is about your opinion ?
> >
> > As I suggested in my response...
> >
> > Suppose that the regression line is of the form
> >
> > a*x + b*y = c
> >
> > Divide by sqrt(a^2 + b^2). Now recover theta. atan2
> > is best for this.
> >
> > theta = atan2(b,a);
> > rho = c./sqrt(a^2 + b^2);
> >
> > John
>
> Thanks, John !
> But if we follow your suggestion, i think we will encounter with vertical fitting line in finding a,b because parameter 'a' is infinite.
> Do you think so ?! and how to solve this ?!

Do you see anyplace where the above scheme will fail if a
OR b are zero? Infinity need never happen.

A vertical line has parameters

   a = 1, b = 0

yielding

   x = c

for some constant value c.

If you use polyfit, then there is a problem, but did I tell
you that you HAD to use polyfit? An orthogonal regression
line will give you a = 1, b = 0. And I KNOW that you
can find a tool to fit an orthogonal regression line, since
you asked this very same question in the comments to a
tool from the file exchange that does orthogonal
regression.

John

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us