[SciPy-User] How to find the intersections between two lines
Charles R Harris
charlesr.harris@gmail....
Fri Apr 9 00:06:15 CDT 2010
On Thu, Apr 8, 2010 at 10:15 PM, seasoul <bioseasoul@gmail.com> wrote:
>
> This is my first post in the mailing list.
> I want to find the intersections between two lines. One line is an
> arbitrary straight line, the other is a fitting line created by a
> interpolation procedure.
> Below is a simple code:
>
>
> x = linspace(0,10,20)
> y = rand(20)*10
> plt.plot(x,y,'o')
> f = interpolate.interp1d(x,y,kind=5)
> xx = linspace(0,10,100)
> yy = f(xx)
> plt.plot(xx,yy,'r-')
> y2 = x
> plt.plot(x,y2,'b-')
> def func(x):
> out = [x[1]-x[0]]
> out.append(x[1]-f(x[0]))
> return out
> fsolve(func,[1,1])
>
> The fitting line is a polynomial curve, the straight line is y=x. This
> function can only find one of the intersection points,not all.
>
>
This is spline interpolation, not polynomial. So I think you are stuck with
sampling the difference of the two curves on a grid, finding adjacent pairs
of sample points at which the function has opposite sign, and then calling
one of the 1d zero finders, brentq for instance.
Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20100408/e65f41c9/attachment.html
More information about the SciPy-User
mailing list