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:
simple linear fit

Subject: simple linear fit

From: Steve Sachay

Date: 16 Mar, 2011 06:28:04

Message: 1 of 9

I have two sets of numbers, and I want to find the scale factor that will bring one as close as possible to the other, ie the simple y=Ax model. I have tried \ but that seems to perfectly divide the first points in each series. Polyval(x,y,1) does not seem to work either, the fit is slightly better but can be improved. What should I be using?

Subject: simple linear fit

From: Nasser M. Abbasi

Date: 16 Mar, 2011 06:44:37

Message: 2 of 9

On 3/15/2011 11:28 PM, Steve Sachay wrote:
> I have two sets of numbers, and I want to find the scale factor that will bring one as
> close as possible to the other, ie the simple y=Ax model.

You mean like

---------------
%date
>> y = 9;
>> x = 4;

% ALGORITHM ENGINE
>> A = y/x;

%result
>> A*scale_factor
--------------

ans =
      9

--Nasser

Subject: simple linear fit

From: Steve Sachay

Date: 16 Mar, 2011 07:39:05

Message: 3 of 9

"Nasser M. Abbasi" <nma@12000.org> wrote in message <ilpm8s$shj$1@speranza.aioe.org>...
>
> You mean like
>
> ---------------
> %date
> >> y = 9;
> >> x = 4;
>
> % ALGORITHM ENGINE
> >> A = y/x;
>
> %result
> >> A*scale_factor
> --------------
>
> ans =
> 9
>
> --Nasser


Thanks. Yes, that is exactly what I mean. Sorry I was not too clear, by sets of numbers, I meant a single scale factor that will bring one set as close as possible to the other. Least squares is fine, I already wrote it myself but there has to be a function for this.

y=

     0.070451
     0.060747
     0.058232
     0.054135
     0.057377
     0.045665
     0.040489
     0.037545
      0.03935
     0.033759


x =
       1.2774
       1.2609
       1.2402
       1.1554
       1.3805
       1.1212
       1.1133
       1.0016
      0.85825
      0.96502

The long way to do it of course:

function cost=lls(f,x,y)

cost=sum((x*f-y).^2)


then run fminsearch on lls. I get 22.62.

Subject: simple linear fit

From: Torsten

Date: 16 Mar, 2011 07:44:37

Message: 4 of 9

On 16 Mrz., 08:39, "Steve Sachay" <core...@gmail.com> wrote:
> "Nasser M. Abbasi" <n...@12000.org> wrote in message <ilpm8s$sh...@speranza.aioe.org>...
>
>
>
>
>
>
>
> > You mean like
>
> > ---------------
> > %date
> > >> y = 9;
> > >> x = 4;
>
> > % ALGORITHM  ENGINE
> > >> A = y/x;  
>
> > %result
> > >> A*scale_factor
> > --------------
>
> > ans =
> >       9
>
> > --Nasser
>
> Thanks. Yes, that is exactly what I mean. Sorry I was not too clear, by sets of numbers, I meant a single scale factor that will bring one set as close as possible to the other. Least squares is fine, I already wrote it myself but there has to be a function for this.  
>
> y=
>
>      0.070451
>      0.060747
>      0.058232
>      0.054135
>      0.057377
>      0.045665
>      0.040489
>      0.037545
>       0.03935
>      0.033759
>
> x =
>        1.2774
>        1.2609
>        1.2402
>        1.1554
>        1.3805
>        1.1212
>        1.1133
>        1.0016
>       0.85825
>       0.96502
>
> The long way to do it of course:
>
> function cost=lls(f,x,y)
>
> cost=sum((x*f-y).^2)
>
> then run fminsearch on lls. I get 22.62.- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

A=(x'*y)/(x'*x)

Best wishes
Torsten.

Subject: simple linear fit

From: Bruno Luong

Date: 16 Mar, 2011 07:48:04

Message: 5 of 9

"Steve Sachay" wrote in message <ilppep$ica$1@ginger.mathworks.com>...
> "Nasser M. Abbasi" <nma@12000.org> wrote in message <ilpm8s$shj$1@speranza.aioe.org>...
> >
> > You mean like
> >
> > ---------------
> > %date
> > >> y = 9;
> > >> x = 4;
> >
> > % ALGORITHM ENGINE
> > >> A = y/x;
> >
> > %result
> > >> A*scale_factor
> > --------------
> >
> > ans =
> > 9
> >
> > --Nasser
>
>
> Thanks. Yes, that is exactly what I mean. Sorry I was not too clear, by sets of numbers, I meant a single scale factor that will bring one set as close as possible to the other. Least squares is fine, I already wrote it myself but there has to be a function for this.
>
> y=
>
> 0.070451
> 0.060747
> 0.058232
> 0.054135
> 0.057377
> 0.045665
> 0.040489
> 0.037545
> 0.03935
> 0.033759
>
>
> x =
> 1.2774
> 1.2609
> 1.2402
> 1.1554
> 1.3805
> 1.1212
> 1.1133
> 1.0016
> 0.85825
> 0.96502
>
> The long way to do it of course:

Shorterway
y'/x'

Bruno

Subject: simple linear fit

From: Steve Sachay

Date: 16 Mar, 2011 08:04:06

Message: 6 of 9

Thank you very much. Both methods posted give me the same answer of course, but slightly different than my explicit method. I have to say "by eye" my fit looks the best but they are both very good.

Subject: simple linear fit

From: Roger Stafford

Date: 16 Mar, 2011 08:14:04

Message: 7 of 9

"Steve Sachay" wrote in message <ilpl9k$8n2$1@ginger.mathworks.com>...
> I have two sets of numbers, and I want to find the scale factor that will bring one as close as possible to the other, ie the simple y=Ax model. I have tried \ but that seems to perfectly divide the first points in each series. Polyval(x,y,1) does not seem to work either, the fit is slightly better but can be improved. What should I be using?
- - - - - - - - - -
  If x and y are vectors of the points' respective coordinates, then

 theta = 1/2*atan2(2*sum(x.*y),sum(x.^2-y.^2));

gives the angle with respect to the x-axis of the line through the origin with the best mean square orthogonal fit. That is, the mean square orthogonal distances of the points to the line is minimized, given that the line is constrained to go through the origin.

Roger Stafford

Subject: simple linear fit

From: Bruno Luong

Date: 16 Mar, 2011 08:16:04

Message: 8 of 9

"Steve Sachay" wrote in message <ilpqtm$ls8$1@ginger.mathworks.com>...
> Thank you very much. Both methods posted give me the same answer of course, but slightly different than my explicit method. I have to say "by eye" my fit looks the best but they are both very good.

All three methods are based on least-square criteria, and should give the same result. The discrepancy you have seen is due to not fully converge by fminsearch. If it looks better, it's just pure luck.

Bruno

Subject: simple linear fit

From: Roger Stafford

Date: 16 Mar, 2011 11:06:04

Message: 9 of 9

"Roger Stafford" wrote in message <ilprgc$n7c$1@ginger.mathworks.com>...
> If x and y are vectors of the points' respective coordinates, then
>
> theta = 1/2*atan2(2*sum(x.*y),sum(x.^2-y.^2));
>
> gives the angle with respect to the x-axis of the line through the origin with the best mean square orthogonal fit. That is, the mean square orthogonal distances of the points to the line is minimized, given that the line is constrained to go through the origin.
>
> Roger Stafford
- - - - - - - - - - - -
  I should point out that the same result can be obtained with a slightly more robust computation involving the 'svd' function (assuming x and y are column vectors):

 [u,s,v] = svd([y,-x],0); % "Economy" version of svd

  The unit vector v(:,2) points along the desired line. Its slope, v(2,2)/v(1,2), will be the desired "scale factor".

  If desired, theta would be:

 theta = atan(v(2,2)/v(1,2));

Roger Stafford

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