Polyfit with scaling

Ben Abbott bpabbott at mac.com
Thu Jan 31 20:26:18 CST 2008


On Jan 31, 2008, at 3:08 PM, Thomas Weber wrote:

> On 31/01/08 07:47 -0500, Ben Abbott wrote:
>> It doesn't appear that the example has much comparative value.
>
> Honestly, I don't care what we change. However, what shouldn't  
> happen is
> that things just stay the same. If we have a better implementation in
> wpolyfit, we should take that for polyfit. Having a better algorithm
> lying around and not using is just a waste (actually, it's even worse:
> we not even have a better algorithm, but it's also already  
> implemented).
>
> 	Thomas

? hmmm ... I agree we  should use the better algorithm.

In any event, I've modified polyfit.m to optionally use the method  
employed by wpolyfit. To do this; I wrote two new scripts. One to  
offset a polynomial's dependent variable (polyshift), and one to scale  
a polynomial's dependent variable (polyscale).

Below I compared the present version of polyfit to the one attached to  
this email.

octave:1> x = 1:4;
octave:2> y = polyval ([1, 1, 1], x);
octave:3> polyfit (x, y, 2)
ans =   1.00000   1.00000   1.00000
octave:4> polyfit (x, y, 2, 1)
ans =   1.00000   1.00000   1.00000
octave:5> polyfit (x, y, 3)
ans =   0   1   1   1
octave:6> polyfit (x, y, 3, 1)
ans =   5.2633e-16   1.0000e+00   1.0000e+00   1.0000e+00
octave:7> polyfit (x, y, 4)
ans =  -0.0020445   0.0204453   0.9284416   1.1022263   0.9509314
octave:8> p = polyfit (x, y, 4, 1)
p =    -5.0886    50.8856  -177.0996   255.4281  -121.1255
octave:9> polyval (p, x)
ans =    3.0000    7.0000   13.0000   21.0000
octave:10> polyval ([1, 1, 1], x)
ans =    3    7   13   21

Comparing to Matlab ...

 >> x = 1:4;
 >> y = polyval([1, 1, 1], x);
 >> polyfit(x, y, 2)
ans =    1.0000    1.0000    1.0000
 >> polyfit(x, y, 3)
ans =    0.0000    1.0000    1.0000    1.0000
 >> polyfit(x, y, 4)
Warning: Polynomial is not unique; degree >= number of data points.
 > In polyfit at 72
ans =    0.0200   -0.2000    1.7000    0    1.4800

The offset and scaling appears to significantly modify the result when  
the order of the fit exceeds to order of the original polynomial.

I am not an expert in such matters. How might we quantitatively  
determine which algorithm is best?

I've attached the modified polyfit.m as well as the two new scripts it  
relies upon.

Perhaps it is worth the effort for others to compare their results  
with mine, and to compare Matlab's results on a computer other than an  
Intel/ppc based Mac (each gave the same result).

Ben




-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://www.cae.wisc.edu/pipermail/octave-maintainers/attachments/20080131/b356e420/attachment-0002.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: polyfit.m
Type: application/octet-stream
Size: 4700 bytes
Desc: not available
Url : https://www.cae.wisc.edu/pipermail/octave-maintainers/attachments/20080131/b356e420/attachment-0003.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: polyscale.m
Type: application/octet-stream
Size: 1723 bytes
Desc: not available
Url : https://www.cae.wisc.edu/pipermail/octave-maintainers/attachments/20080131/b356e420/attachment-0004.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: polyshift.m
Type: application/octet-stream
Size: 1895 bytes
Desc: not available
Url : https://www.cae.wisc.edu/pipermail/octave-maintainers/attachments/20080131/b356e420/attachment-0005.obj 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://www.cae.wisc.edu/pipermail/octave-maintainers/attachments/20080131/b356e420/attachment-0003.html 


More information about the Octave-maintainers mailing list