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 |
From: Gary R. <ga...@em...> - 2004-03-19 13:49:37
|
First, let me say, I don't know if there is code to do exactly what you want but here are my thoughts. It sounds to me like you're asking for Lagrange polynomial fitting routines. Googling for "lagrange polynomial python" does return some code here: <http://www.stanford.edu/~sturdza/akimamod/akimamod.py> Another possibility is the spline fitting routines in Scipy (scipy.interpolate). These may be appropriate if what you're really after is just a way to fit smooth functions through points. I've used the splrep and splev functions there successfully to fit spline functions through points. When I was looking for curve fitting routines recently, I also came across some more generalized curve fitting modules for Python but I can't recall where :-( I think they were SWIG wrappers for a C library. Also, look at this: <http://www.scipy.org/site_content/remap?rmurl=http%3A//www.scipy.net/pipermail/scipy-user/2003-August/001864.html> HTH, regards, Gary ----- Original Message ----- From: Jean-Baptiste Cazier <Jea...@de...> Date: Fri, 19 Mar 2004 11:49:21 +0000 To: "Gary Ruben" <ga...@em...>, jdh...@ni... Subject: Re: [Matplotlib-users] Polyfit > > 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 piecewise polynomial to a list of (X,Y) points. > something like > p=piece-wiseFit([1,2,5,7,8],[3,4,2,5,5],2) > would return [[A0,B0,C0],[A1,B1,C1}[A2,B2,C2},[A3,B3,C3]}, coefficients for the 4 polynoms > 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. <snip> -- ___________________________________________________________ Sign-up for Ads Free at Mail.com http://promo.mail.com/adsfreejump.htm |
From: Jean-Baptiste C. <Jea...@de...> - 2004-03-21 19:50:32
|
S=E6l ! Thanks for the tip, but I do not want to use the lagrangian: I want many sm= all polynoms, not 1 What I want to do is very typical in Finite Element Method eventhough this = is not the case here. I just want to define many small polynoms between consecutives points. Each one is stsifying the continuity of the value as well as the derivative if I define my function as f(x)=3Dax*x+bx+c At my points (X0,Y0) (X1,Y1) as well at teh derivative a X0 to be Z0 I get = the follwoing f(X0)=3DY0=3Da*X0*X0+b*X0+c f(X1)=3DY1=3Da*X1*X1+b*X1+c f'(X0)=3DZ0=3D 2aX0+b After small manipulkation I can directly infere a,b,c This is a very simple but useful routine that I was hoping people would hav= e already written Actually it is much smoother with a cubic polynomial, but it is a bit more = complicated to implement Thanks anyway Jean-Baptiste On Fri, 19 Mar 2004 23:49:28 +1000 "Gary Ruben" <ga...@em...> wrote: > First, let me say, I don't know if there is code to do exactly what you w= ant but here are my thoughts. > It sounds to me like you're asking for Lagrange polynomial fitting routin= es. Googling for "lagrange polynomial python" does return some code here: <= http://www.stanford.edu/~sturdza/akimamod/akimamod.py> > Another possibility is the spline fitting routines in Scipy (scipy.interp= olate). These may be appropriate if what you're really after is just a way = to fit smooth functions through points. I've used the splrep and splev func= tions there successfully to fit spline functions through points. When I was= looking for curve fitting routines recently, I also came across some more = generalized curve fitting modules for Python but I can't recall where :-( I= think they were SWIG wrappers for a C library. > Also, look at this: > <http://www.scipy.org/site_content/remap?rmurl=3Dhttp%3A//www.scipy.net/p= ipermail/scipy-user/2003-August/001864.html> > HTH, > regards, > Gary >=20 > ----- Original Message ----- > From: Jean-Baptiste Cazier <Jea...@de...> > Date: Fri, 19 Mar 2004 11:49:21 +0000 > To: "Gary Ruben" <ga...@em...>, jdh...@ni... > Subject: Re: [Matplotlib-users] Polyfit >=20 > >=20 > > Thanks to both of you. It worked just fine > >=20 > > I will push my luck and ask if any of you knows of a module to fit a pi= ecewise 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 > >=20 > > This is a classic and I expect the code to be written somewhere, eventh= ough I could not find it even when I "Feel lucky" with Google. > <snip> > --=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 |