From: Jean-Baptiste C. <Jea...@de...> - 2004-03-19 11:49:47
|
Thanks to both of you. It worked just fine I will push my luck and ask if any of you knows of a module to fit a piecew= ise polynomial to a list of (X,Y) points. something like=20 p=3Dpiece-wiseFit([1,2,5,7,8],[3,4,2,5,5],2)=20 would return [[A0,B0,C0],[A1,B1,C1}[A2,B2,C2},[A3,B3,C3]}, coefficients for= the 4 polynoms=20 A0+B0.X+C0.X.X A1+B1.X+C1.X.X A2+B2.X+C2.X.X A3+B2.X+C3.X.X This is a classic and I expect the code to be written somewhere, eventhough= I could not find it even when I "Feel lucky" with Google. Takk Kve=F0ja Jean-Baptiste On Fri, 19 Mar 2004 09:45:10 +1000 "Gary Ruben" <ga...@em...> wrote: > Hi Jean-Baptiste, > Your problem is that polyfit (and polyval) expect Numeric array arguments= , so you need to do: >=20 > >>> x=3Darray([1,2,3]) > >>> y=3Darray([1,2,1]) > >>> p=3Dpolyfit(x,y,2) > >>> p > array([-1., 4., -2.]) > >>> polyval(p,array([1,2,3])) > array([ 1., 2., 1.]) >=20 > Note that your example only has 3 points, so can fit a 2nd order polynomi= al exactly as I've done. > If you try to fit a 3rd order, the method polyfit is using seems to fail.= ie. > >>> p=3Dpolyfit(x,y,3) > >>> p > array([ 1.09375, -5.0625 , 7.125 , -2.875 ]) > >>> polyval(p,array([1,2,3])) > array([ 0.28125, -0.125 , 2.46875]) >=20 > ideally p would have been array([0., -1., 4., -2.]) so you'll have to be= careful. >=20 > Gary >=20 > ----- Original Message ----- > From: Jean-Baptiste Cazier <Jea...@de...> > Date: Thu, 18 Mar 2004 14:36:45 +0000 > To: mat...@li... > Subject: [Matplotlib-users] Polyfit >=20 > > S=E6ll=20 > >=20 > > One mroe question from me today :) > >=20 > > I have some trouble running polyfit with matplotlib-0.52 > > Can you please help me finding out what I do wrong=20 > > I give a list of x values and y values as well as the degree of the des= ired polynome. But all I get is the following error > >=20 > > >>> x=3D[1,2,3] > > >>> y=3D[1,2,1] > > >>> polyfit(x,y,3) > > Traceback (most recent call last): > > File "<<console>>", line 1, in ? > > File "/usr/lib/python2.2/site-packages/matplotlib/mlab.py", line 341,= in polyfit > > X =3D Matrix(vander(x, N+1)) > > File "/usr/lib/python2.2/site-packages/matplotlib/mlab.py", line 383,= in vander > > X =3D ones( (len(x),N), x.typecode()) > > AttributeError: 'list' object has no attribute 'typecode' > >=20 > >=20 > > Any idea ? > >=20 > > Thanks > >=20 > > Jean-Baptiste > >=20 > > --=20 > > ----------------------------- > > Jea...@de... > >=20 > > Department of Statistics > > deCODE genetics Sturlugata,8 > > 570 2993 101 Reykjav=EDk > >=20 > >=20 > >=20 > > ------------------------------------------------------- > > This SF.Net email is sponsored by: IBM Linux Tutorials > > Free Linux tutorial presented by Daniel Robbins, President and CEO of > > GenToo technologies. Learn everything from fundamentals to system > > administration.http://ads.osdn.com/?ad_id=1470&alloc_id638&op=3Dclick > > _______________________________________________ > > Matplotlib-users mailing list > > Mat...@li... > > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > >=20 >=20 > --=20 > ___________________________________________________________ > Sign-up for Ads Free at Mail.com > http://promo.mail.com/adsfreejump.htm >=20 --=20 ----------------------------- Jea...@de... Department of Statistics deCODE genetics Sturlugata,8 570 2993 101 Reykjav=EDk |