# need help with polyfit and polyval

 Classic List Threaded
5 messages
Reply | Threaded
Open this post in threaded view
|

## need help with polyfit and polyval

 I am trying to convert some data from wikipedia to octave and I ran into this problem.here is the code**********************************************data = [    4.0000    3.1000    5.0000    4.7400    6.0000    6.1300    7.0000    7.2600    8.0000    8.1000    9.0000    8.1400   10.0000    8.7400   11.0000    8.7700   12.0000    9.1300   13.0000    9.1400   14.0000    9.2600  ];  x = data(:,1);  y = data(:,2);  [P, S, MU] = polyfit (x, y, 1)y3=S.yfy2=polyval(P,x)plot(x,y,'x',x,y,x,y2,x,y3)**********************************************poly fit  should return the polynomial in PBut when i use it in polyval it is obviously wrong.but polyfit also returns the new calculated y values in S.yf.S.yf look correct ut P seems to be wrong.I get P =   1.8410   7.5009which is      y=x*1.841 + 7.5009and this is obviously wrong.Can someone run this and see if you get what I get?And why is polyfit giving back a wrong P for this data set?-- DAS
Reply | Threaded
Open this post in threaded view
|

## Re: need help with polyfit and polyval

 On Mon, May 6, 2019 at 9:31 AM Doug Stewart <[hidden email]> wrote:I am trying to convert some data from wikipedia to octave and I ran into this problem.here is the code**********************************************data = [    4.0000    3.1000    5.0000    4.7400    6.0000    6.1300    7.0000    7.2600    8.0000    8.1000    9.0000    8.1400   10.0000    8.7400   11.0000    8.7700   12.0000    9.1300   13.0000    9.1400   14.0000    9.2600  ];  x = data(:,1);  y = data(:,2);  [P, S, MU] = polyfit (x, y, 1)y3=S.yfy2=polyval(P,x)plot(x,y,'x',x,y,x,y2,x,y3)**********************************************poly fit  should return the polynomial in PBut when i use it in polyval it is obviously wrong.but polyfit also returns the new calculated y values in S.yf.S.yf look correct ut P seems to be wrong.I get P =   1.8410   7.5009which is      y=x*1.841 + 7.5009and this is obviously wrong.Yes. It looks like [P, S, MU] = polyfit (x, y, 1) form has a problem.if you just do p = polyfit (x, y, 1)you get a correct p:P=polyfit (x,y,1)P =   0.55509   2.50509octave:12> y2=polyval(P,x)y2 =    4.7255    5.2805    5.8356    6.3907    6.9458    7.5009    8.0560    8.6111    9.1662    9.7213   10.2764Dmitri.--
Reply | Threaded
Open this post in threaded view
|

## Re: need help with polyfit and polyval

 On Mon, May 6, 2019 at 9:50 AM Dmitri A. Sergatskov <[hidden email]> wrote: > > > > On Mon, May 6, 2019 at 9:31 AM Doug Stewart <[hidden email]> wrote: >> >> I am trying to convert some data from wikipedia to octave and I ran into this problem. >> >> here is the code >> ********************************************** >> data = [    4.0000    3.1000 >>     5.0000    4.7400 >>     6.0000    6.1300 >>     7.0000    7.2600 >>     8.0000    8.1000 >>     9.0000    8.1400 >>    10.0000    8.7400 >>    11.0000    8.7700 >>    12.0000    9.1300 >>    13.0000    9.1400 >>    14.0000    9.2600  ]; >>   x = data(:,1); >>   y = data(:,2); >>   [P, S, MU] = polyfit (x, y, 1) >> y3=S.yf >> y2=polyval(P,x) >> >> plot(x,y,'x',x,y,x,y2,x,y3) >> >> ********************************************** >> >> >> poly fit  should return the polynomial in P >> But when i use it in polyval it is obviously wrong. >> but polyfit also returns the new calculated y values in S.yf. >> S.yf look correct ut P seems to be wrong. >> I get >> P =   1.8410   7.5009 >> which is      y=x*1.841 + 7.5009 >> and this is obviously wrong. > > > Yes. It looks like > [P, S, MU] = polyfit (x, y, 1) form has a problem. > if you just do p = polyfit (x, y, 1) > you get a correct p: > P=polyfit (x,y,1) > P = > >    0.55509   2.50509 > > octave:12> y2=polyval(P,x) > y2 = > >     4.7255 >     5.2805 >     5.8356 >     6.3907 >     6.9458 >     7.5009 >     8.0560 >     8.6111 >     9.1662 >     9.7213 >    10.2764 > In fact it looks like a feature, from the help: 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). I think text in help can be improved > > > Dmitri. > -- > Dmitri.
Reply | Threaded
Open this post in threaded view
|

## Re: need help with polyfit and polyval

 On Mon, May 6, 2019 at 9:54 AM Dmitri A. Sergatskov <[hidden email]> wrote: > > On Mon, May 6, 2019 at 9:50 AM Dmitri A. Sergatskov > <[hidden email]> wrote: > > > > > > > > On Mon, May 6, 2019 at 9:31 AM Doug Stewart <[hidden email]> wrote: > >> > >> I am trying to convert some data from wikipedia to octave and I ran into this problem. > >> > >> here is the code > >> ********************************************** > >> data = [    4.0000    3.1000 > >>     5.0000    4.7400 > >>     6.0000    6.1300 > >>     7.0000    7.2600 > >>     8.0000    8.1000 > >>     9.0000    8.1400 > >>    10.0000    8.7400 > >>    11.0000    8.7700 > >>    12.0000    9.1300 > >>    13.0000    9.1400 > >>    14.0000    9.2600  ]; > >>   x = data(:,1); > >>   y = data(:,2); > >>   [P, S, MU] = polyfit (x, y, 1) > >> y3=S.yf > >> y2=polyval(P,x) So if you used MU in polifit, you should use it in polyval as well: y2=polyval(P,x,[],MU) y2 =     4.7255     5.2805     5.8356     6.3907     6.9458     7.5009     8.0560     8.6111     9.1662     9.7213    10.2764 Dmitri. --
Reply | Threaded
Open this post in threaded view
|

## Re: need help with polyfit and polyval

 On Mon, May 6, 2019 at 11:04 AM Dmitri A. Sergatskov <[hidden email]> wrote:On Mon, May 6, 2019 at 9:54 AM Dmitri A. Sergatskov <[hidden email]> wrote: > > On Mon, May 6, 2019 at 9:50 AM Dmitri A. Sergatskov > <[hidden email]> wrote: > > > > > > > > On Mon, May 6, 2019 at 9:31 AM Doug Stewart <[hidden email]> wrote: > >> > >> I am trying to convert some data from wikipedia to octave and I ran into this problem. > >> > >> here is the code > >> ********************************************** > >> data = [    4.0000    3.1000 > >>     5.0000    4.7400 > >>     6.0000    6.1300 > >>     7.0000    7.2600 > >>     8.0000    8.1000 > >>     9.0000    8.1400 > >>    10.0000    8.7400 > >>    11.0000    8.7700 > >>    12.0000    9.1300 > >>    13.0000    9.1400 > >>    14.0000    9.2600  ]; > >>   x = data(:,1); > >>   y = data(:,2); > >>   [P, S, MU] = polyfit (x, y, 1) > >> y3=S.yf > >> y2=polyval(P,x) So if you used MU in polifit, you should use it in polyval as well: y2=polyval(P,x,[],MU) y2 =     4.7255     5.2805     5.8356     6.3907     6.9458     7.5009     8.0560     8.6111     9.1662     9.7213    10.2764 Dmitri. -- Thank you very much. that explains it very well.-- DAS