[SciPy-User] Points fitting (non lin)

Zachary Pincus zachary.pincus@yale....
Sun Dec 16 13:44:34 CST 2012


> Dear Zach,
> I didn't understand the trick...sorry.
> Please, can you explain it again?

You'll want to read up about parametric representation of curves on wikipedia etc.

Obviously, the coordinates you listed:
P1 = (1,1)
P2 = (2,2)
P3 = (4,2)
P4 = (3,1)
do not trace out y values that are a proper function of, because there are some places where the curve would have multiple y-values for a given x-value (at 3, say). So, as you correctly understood, you can't just try to fit a function y=f(x) to your data and expect that to work.

What you can, however, do is fit TWO functions to your data, using a dummy variable. For example, imagine a point moving along your curve at a given speed, so that at any given time t, the position of the point is given as (x(t), y(t)). 

So for each of your points above P1-P4, associate a value T1-T4.

For example,
t  x  y
0  1  1
1  2  2
2  4  2
3  3  1

is just your data above but with a t parameter indexing each point. So at t=0, x=1 and y=1. At t=1, x=2 and y=2. If you had a functional form for x(t) and y(t), you could then estimate where the point on the curve that was midway between the first (t=0) and second (t=1) points by evaluating x(0.5) and y(0.5).

To do this, one could fit x(t) and y(t) separately (because as long as the t values are monotonic, x(t) and y(t) are proper functions for any smooth curve that you could draw on a plane). Fitting a spline to these data is the most straightforward way to do this, and fortunately, scipy.interpolate.splprep does this all for you and gives you a spline that can be evaluated at different t positions by splev.

If spline fitting is unfamiliar to you, then parametric spline fitting will seem even less comprehensible. So you might want to experiment by fitting other simpler functional curves in scipy with splrep and then moving on. (There should be interpolation examples aplenty online.)

Zach



> Thanks a lot.
> Paolo
> 
> 
> Il 14/12/2012 17:50, Zachary Pincus ha scritto:
>> On Dec 14, 2012, at 11:42 AM, Paolo Zaffino wrote:
>> 
>>> Dear Paweł,
>>> thank you for the reply.
>>> I try to explain better the issue.
>>> I have these points (in this order):
>>> 
>>> P1 = (1,1)
>>> P2 = (2,2)
>>> P3 = (4,2)
>>> P4 = (3,1)
>>> 
>>> I need to fit the points in the order P1,P2,P3,P4 even if the x coord of P3 is greater than P4.
>>> I thought to quadratic piecewise curve but other solutions are welcome.
>>> 
>>> Thanks a lot.
>>> Paolo
>> You will want to fit a parametric spline of some degree with some amount (or no) smoothing. I'd look at the splprep function in scipy.interpolate.
>> 
>> The trick is you associate each point with some monotonic parameter value, and then interpolate along that parameter (say t) to get your x, y coordinates.
>> 
>> E.g.:
>> t  x  y
>> 0  1  1
>> 1  2  2
>> 2  4  2
>> 3  3  1
>> 
>> Then if you were interpolating linearly, at t=0.5, you would have (1.5, 1.5) as the coordinate.
>> 
>> As above, splprep will generate splines of a desired order (linear, quadratic, cubic, etc.) and with a user-specified smoothing parameter (s), which can be set to zero to get exact interpolation of the input coordinates, potentially at the cost of ringing (sometimes quite bad) away from the input coordinate. So you will need to plot the interpolated values, both at the input t-values, as well as at intermediate t's, to see if the output is sane.
>> 
>> Hopefully this is somewhat clear, or at least enough to get you started. Please read the documentation for splprep and splev for more information.
>> Zach
>> 
>> 
>> 
>>> Da: Paweł Kwaśniewski <pawel.kw@gmail.com>
>>> A: Paolo Zaffino <p.zaffino@yahoo.it>; SciPy Users List <scipy-user@scipy.org>
>>> Inviato: Venerdì 14 Dicembre 2012 17:25
>>> Oggetto: Re: [SciPy-User] Points fitting (non lin)
>>> 
>>> Dear Paolo,
>>> 
>>> I'm not sure I understand correctly your problem, but this sounds like a spline fitting job. You can read more about this here: http://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html
>>> 
>>> Is this what you are looking for?
>>> 
>>> Cheers,
>>> 
>>> Paweł
>>> 
>>> 
>>> 
>>> 2012/12/14 Paolo Zaffino <p.zaffino@yahoo.it>
>>> Dear Scipy community,
>>> 
>>> I have a set of points (2D) and I would compute a curve that fits them.
>>> The points are ordered in a precise way (not crescent order) and I can't change this order (the curve should fit the points in that order).
>>> I'm interseting in a non linear fit (the ideal case would be more intervals of quadratic curves).
>>> Has anyone any advice about?
>>> 
>>> Thank you very much.
>>> Regards.
>>> Paolo
>>> 
>>> _______________________________________________
>>> SciPy-User mailing list
>>> SciPy-User@scipy.org
>>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>> 
>>> 
>>> 
>>> 
>>> _______________________________________________
>>> SciPy-User mailing list
>>> SciPy-User@scipy.org
>>> http://mail.scipy.org/mailman/listinfo/scipy-user
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
> 
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user



More information about the SciPy-User mailing list