help-octave
[Top][All Lists]

## Re: polyfit -> polyval problem

 From: Sergei Steshenko Subject: Re: polyfit -> polyval problem Date: Wed, 22 Jun 2011 07:03:37 -0700 (PDT)

```
--- On Wed, 6/22/11, Ben Abbott <address@hidden> wrote:

> Subject: Re: polyfit -> polyval problem
> Date: Wednesday, June 22, 2011, 6:21 AM
>
> On Jun 22, 2011, at 9:16 AM, Sergei Steshenko wrote:
>
> > Hello,
> >
> > I can't figure out what I an doing wrong in the
> following example:
> >
> > "
> > octave:42> [p, s, mu] = polyfit ((1:10)(:), 2 *
> (1:10)(:), 1);
> > octave:43> p
> > p =
> >
> >    6.0553   11.0000
> >
> > octave:44> polyval(p, 1:10)
> > ans =
> >
> >   17.055   23.111   29.166   35.221   41.277   47.332   53.387   59.442
> >65.498   71.553
> >
>
> I think the problem is with the syntax you're using for
> polyval. If "mu" is an output for polyfit(), then it must be
> used as an input for polval().
>
> octave:3>  [p, s, mu] = polyfit ((1:10)(:), 2 *
> (1:10)(:), 1);
> octave:4> polyval(p, 1:10, [], mu)
> ans =
>
>     2.0000    4.0000
> 6.0000
> 8.0000   10.0000   12.0000   14.0000   16.0000   18.0000   20.0000
>
> Ben
>
>

Thanks.

Well, this:

"
octave:48> p = polyfit ((1:10)(:), 2 * (1:10)(:), 1);
octave:49> p
p =

2.0000e+00   2.2753e-15

octave:50> polyval(p, 1:10)
ans =

2.0000    4.0000    6.0000    8.0000   10.0000   12.0000   14.0000
16.0000   18.0000   20.0000

octave:51>
"

also works.

Now, this is the documentation:

"
octave:47> help polyfit
-- Function File: [P, S, MU] = polyfit (X, Y, N)
Return the coefficients of a polynomial P(X) of degree N that
minimizes the least-squares-error of the fit.

The polynomial coefficients are returned in a row vector.

The second output is a structure containing the following fields:

`R'
Triangular factor R from the QR decomposition.

`X'
The Vandermonde matrix used to compute the polynomial
coefficients.

`df'
The degrees of freedom.

`normr'
The norm of the residuals.

`yf'
The values of the polynomial for each value of X.

The second output may be used by `polyval' to calculate the
statistical error limits of the predicted values.

When the third output, MU, is present the coefficients, P, are
associated with a polynomial in XHAT = (X-MU(1))/MU(2).  Where
MU(1) = mean (X), and MU(2) = std (X).  This linear transformation
of X improves the numerical stability of the fit.

/mnt/sdb8/sergei/AFSWD_debug/20110424/octave-3.0.5/share/octave/3.0.5/m/polynomial/polyfit.m

Additional help for built-in functions and operators is
available in the on-line version of the manual.  Use the command
`doc <topic>' to search the manual index.

Help and information about Octave is also available on the WWW
at http://www.octave.org and via the address@hidden
mailing list.
"

- could you point exactly which part of the above documentation _clearly_
explains how to use 'polyval' with 'polyfit' results ?

I.e., for starters, I do not even see in the documentation that

p = polyfit(...);

is a legal form of usage.

Thanks,
Sergei.

```