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:
curve fit

Subject: curve fit

From: arash

Date: 1 Oct, 2010 19:34:57

Message: 1 of 6

I have scattered points (x,y) and I want to draw the best line that fitted these points.
Also I want to know the slope of this line and the point it intersects x axes.

Subject: curve fit

From: Matt J

Date: 1 Oct, 2010 19:49:05

Message: 2 of 6

arash <arash_naji@yahoo.com> wrote in message <1254666220.32610.1285961727575.JavaMail.root@gallium.mathforum.org>...
> I have scattered points (x,y) and I want to draw the best line that fitted these points.
> Also I want to know the slope of this line and the point it intersects x axes.

poyfit(x,y,1)

Subject: curve fit

From: Walter Roberson

Date: 1 Oct, 2010 19:49:50

Message: 3 of 6

On 10-10-01 02:34 PM, arash wrote:
> I have scattered points (x,y) and I want to draw the best line that fitted these points.

You can construct a multinomial that will pass through all of the points
exactly (to within the precision of binary floating point)

> Also I want to know the slope of this line and the point it intersects x axes.

Multinomials do not have just *one* slope unless they are completely degenerate.

I suspect that you have not asked the question you wish to ask. I suspect you
want to draw a straight line that best fits the points for some meaning of
"best fits". Possibly you wish to do a linear regression. If so, then you
should read the documentation about the \ operator (also known as mldivide )

Subject: curve fit

From: Sean

Date: 1 Oct, 2010 19:54:21

Message: 4 of 6

arash <arash_naji@yahoo.com> wrote in message <1254666220.32610.1285961727575.JavaMail.root@gallium.mathforum.org>...
> I have scattered points (x,y) and I want to draw the best line that fitted these points.
> Also I want to know the slope of this line and the point it intersects x axes.

x = [3;4;5];
y = [3;10;9.999];

B = ones(numel(x),2);
B(:,1) = x;

BTB = B'*B;
a = BTB\(B'*y);

plot(x,y,'b*',x,(a(1)*x+a(2)),'g--')

a(1) = slope
a(2) = intercept

Subject: curve fit

From: Matt J

Date: 1 Oct, 2010 20:15:23

Message: 5 of 6

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i85e9c$a5l$1@fred.mathworks.com>...

> BTB = B'*B;
> a = BTB\(B'*y);

Evaluating BTB is not a good idea. In the first place, it is unnecessary, since this is essentially the same as B\y


>> BTB\(B'*y) - B\y

ans =

  1.0e-013 *

   -0.0577
    0.1688


Secondly, BTB has poorer conditioning than B. Compare the following

>> B=vander(1:8); y=B(:,1);
>> B.'*B\B.'*y
Warning: Matrix is close to singular or badly scaled.
         Results may be inaccurate. RCOND = 7.526799e-019.

ans =

    1.0000
    0.0000
   -0.0000
    0.0003
   -0.0011
    0.0024
   -0.0026
    0.0011

>> B\y

ans =

     1
     0
     0
     0
     0
     0
     0
     0

Subject: curve fit

From: Sean

Date: 1 Oct, 2010 20:28:04

Message: 6 of 6

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i85fgr$2pa$1@fred.mathworks.com>...
> "Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i85e9c$a5l$1@fred.mathworks.com>...
>
> > BTB = B'*B;
> > a = BTB\(B'*y);
>
> Evaluating BTB is not a good idea. In the first place, it is unnecessary, since this is essentially the same as B\y
>
>
> >> BTB\(B'*y) - B\y
>
> ans =
>
> 1.0e-013 *
>
> -0.0577
> 0.1688
>
>
> Secondly, BTB has poorer conditioning than B. Compare the following
>
> >> B=vander(1:8); y=B(:,1);
> >> B.'*B\B.'*y
> Warning: Matrix is close to singular or badly scaled.
> Results may be inaccurate. RCOND = 7.526799e-019.
>
> ans =
>
> 1.0000
> 0.0000
> -0.0000
> 0.0003
> -0.0011
> 0.0024
> -0.0026
> 0.0011
>
> >> B\y
>
> ans =
>
> 1
> 0
> 0
> 0
> 0
> 0
> 0
> 0

Good to know and it makes sense; thanks!

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