On Jan 31, 2008, at 9:26 PM, Ben Abbott wrote: 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
I should mention that nothing has been done to ensure that the second output respects the normalization of the dependent variable.
I'll look into what needs to be done there, if it is possible ... or more to the point, if I'm up to the task ;-)
If anyone has experience or background in how the job might be done, please feel free to contribute or to point me in the right direction!
Ben
|