f

#### best fit line through coordinates

```Hi guys,

I have an issue I'd like some help with.

coordinates =

107.35                       107                    111.51                       146
117                    154.61                    111.29                       114

In the above matrix 'coordinates', its actually in the form [ x1 y1 x2 y2] for each of the 2 rows.

Is there any function I can obtain a best fit line using these 4 points?

thanks
Kurtis
```
 0
Kurt
10/16/2010 6:02:04 AM
comp.soft-sys.matlab 211266 articles. 25 followers. lunamoonmoon (257) is leader.

6 Replies
479 Views

Similar Articles

[PageSpeed] 34

```Go look up the demo on the function polyfit, u can tell it to make a first order regression of those points which would be the line of best fit

"Kurt " <rerty258@gmail.com> wrote in message <i9bf4s\$9dq\$1@fred.mathworks.com>...
> Hi guys,
>
> I have an issue I'd like some help with.
>
>
> coordinates =
>
>                     107.35                       107                    111.51                       146
>                        117                    154.61                    111.29                       114
>
>
> In the above matrix 'coordinates', its actually in the form [ x1 y1 x2 y2] for each of the 2 rows.
>
> Is there any function I can obtain a best fit line using these 4 points?
>
>
> thanks
> Kurtis
```
 0
Stuart
10/16/2010 6:25:07 AM
```On Sat, 16 Oct 2010 06:25:07 +0000 (UTC), "Stuart "
<imanotarat@gmail.com> wrote:

>Go look up the demo on the function polyfit, u can tell it to make a first order regression of those points which would be the line of best fit
>
>
>"Kurt " <rerty258@gmail.com> wrote in message <i9bf4s\$9dq\$1@fred.mathworks.com>...
>> Hi guys,
>>
>> I have an issue I'd like some help with.
>>
>>
>> coordinates =
>>
>>                     107.35                       107                    111.51                       146
>>                        117                    154.61                    111.29                       114
>>
>>
>> In the above matrix 'coordinates', its actually in the form [ x1 y1 x2 y2] for each of the 2 rows.
>>
>> Is there any function I can obtain a best fit line using these 4 points?
>>
>>
>> thanks
>> Kurtis

or

help backslash
```
 0
Richard
10/16/2010 3:12:36 PM
```"Kurt " <rerty258@gmail.com> wrote in message <i9bf4s\$9dq\$1@fred.mathworks.com>...
> .........
> In the above matrix 'coordinates', its actually in the form [ x1 y1 x2 y2] for each of the 2 rows.
> Is there any function I can obtain a best fit line using these 4 points?
> .......
- - - - - - - - -
For best orthogonal fit, rearrange coordinates in the form [x1,y1;x2,y2;x3,y3,x4,y4] and use 'svd'.

Roger Stafford
```
 0
Roger
10/16/2010 6:03:03 PM
```"Kurt " <rerty258@gmail.com> wrote in message <i9bf4s\$9dq\$1@fred.mathworks.com>...
>
> In the above matrix 'coordinates', its actually in the form [ x1 y1 x2 y2] for each of the 2 rows.
>
> Is there any function I can obtain a best fit line using these 4 points?
=====

help polyfit
```
 0
Matt
10/16/2010 6:29:05 PM
```Roger,

do you mean like this? :

X=

107.35 107
111.51 146
117 154.61
111.29 114

[U,S,V] = svd(X)

My question is which component should I use to do the orthogonal fitting?

thanks
kurt
```
 0
Kurt
10/16/2010 7:14:05 PM
```"Kurt " <rerty258@gmail.com> wrote in message <i9cths\$hee\$1@fred.mathworks.com>...
> Roger,
>
> do you mean like this? :
>
>
> X=
>
> 107.35 107
> 111.51 146
> 117 154.61
> 111.29 114
>
>
> [U,S,V] = svd(X)
>
>
> My question is which component should I use to do the orthogonal fitting?
>
> thanks
> kurt
- - - - - - - - - -
First you must subtract the coordinates' mean values from them.  With your 4 by 2 array X, do this:

X0 = mean(X,1);
[~,~,V] = svd(bsxfun(@minus,X,X0),0); % Economy version
N = V(:,1); % Unit vector in direction of maximum variation

Then the best orthogonal fitting line is

P = X0 + N*t

as the parameter t varies.  This is the direction of maximum variation which means that variation in the orthogonal direction is minimum.  The sum of the squares of the points' orthogonal distances to this line is minimized.

This is distinct from linear regression which minimizes the y variation from the line of regression.  That regression assumes that all errors are in the y coordinates, whereas orthogonal fitting assumes the errors in both the x and y coordinates are of equal expected magnitudes.

Roger Stafford
```
 0
Roger
10/16/2010 11:22:04 PM