help-octave
[Top][All Lists]

## Re: Problem of polyfit

 From: Fredrik Lingvall Subject: Re: Problem of polyfit Date: Thu, 06 Oct 2005 12:51:10 +0200 User-agent: Mozilla Thunderbird 1.0.7 (X11/20050923)

```Paul Kienzle wrote:

```
```
On Oct 6, 2005, at 5:46 AM, Tetsuro KURITA wrote:

```
The following code in "polyfit.m" never give the best fit data in the least squares sense as described in document.
```
X = (x * ones (1, n+1)) .^ (ones (l, 1) * (n : -1 : 0));
p = X \ y;

This code do nothing to mimimize `sumsq (p(x(i)) - y(i))'.

I think this code should be modified as follows.

X = (x * ones (1, n+1)) .^ (ones (l, 1) * (n: -1 : 0))
W = X'*X
z = X'*y
p = inv(W)*z
```
```

p = inv(X'*X)*X'*y
= inv(X)*inv(X')*X'*y
= inv(X)*I*y
= inv(X)*y
= X \ y

```
wpolyfit does the same with weighting on y, except that it uses QR decomposition to solve X \ y.
```

```
```Try,

[U,D,V] = svd(X);
semilogy(diag(D));

If you have singular values close to zero your in trouble.

Fredrik

-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------

```