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.

Takk

Kveðja

Jean-Baptiste

···

On Fri, 19 Mar 2004 09:45:10 +1000 "Gary Ruben" <gazzar@...25...> wrote:

Hi Jean-Baptiste,
Your problem is that polyfit (and polyval) expect Numeric array arguments, so you need to do:

>>> x=array([1,2,3])
>>> y=array([1,2,1])
>>> p=polyfit(x,y,2)
>>> p
array([-1., 4., -2.])
>>> polyval(p,array([1,2,3]))
array([ 1., 2., 1.])

Note that your example only has 3 points, so can fit a 2nd order polynomial exactly as I've done.
If you try to fit a 3rd order, the method polyfit is using seems to fail. ie.
>>> p=polyfit(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])

ideally p would have been array([0., -1., 4., -2.]) so you'll have to be careful.

Gary

----- Original Message -----
From: Jean-Baptiste Cazier <Jean-Baptiste.cazier@...15...>
Date: Thu, 18 Mar 2004 14:36:45 +0000
To: matplotlib-users@lists.sourceforge.net
Subject: [Matplotlib-users] Polyfit

> Sæll
>
> One mroe question from me today :slight_smile:
>
> I have some trouble running polyfit with matplotlib-0.52
> Can you please help me finding out what I do wrong
> I give a list of x values and y values as well as the degree of the desired polynome. But all I get is the following error
>
> >>> x=[1,2,3]
> >>> y=[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 = Matrix(vander(x, N+1))
> File "/usr/lib/python2.2/site-packages/matplotlib/mlab.py", line 383, in vander
> X = ones( (len(x),N), x.typecode())
> AttributeError: 'list' object has no attribute 'typecode'
>
>
> Any idea ?
>
> Thanks
>
> Jean-Baptiste
>
> --
> -----------------------------
> Jean-Baptiste.Cazier@...15...
>
> Department of Statistics
> deCODE genetics Sturlugata,8
> 570 2993 101 Reykjavík
>
>
>
> -------------------------------------------------------
> 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_id70&alloc_id638&op=click
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users@lists.sourceforge.net
> matplotlib-users List Signup and Options
>

--
___________________________________________________________
Sign-up for Ads Free at Mail.com
http://promo.mail.com/adsfreejump.htm

--
-----------------------------
Jean-Baptiste.Cazier@...15...

Department of Statistics
deCODE genetics Sturlugata,8
570 2993 101 Reykjavík