[SciPy-User] [SciPy-user] How to find the intersections between two lines
seasoul
bioseasoul@gmail....
Sat Apr 10 00:54:26 CDT 2010
Really really works.
Alexander Baker wrote:
>
> bit rushed but seems to do the trick!
>
> Alex
>
> import numpy as np
> from scipy import interpolate
> from scipy import optimize
> import pylab
>
> x = np.linspace(0,10,20)
> y = np.random.random(20)*10
> f = interpolate.interp1d(x,y,kind=5)
> xx = np.linspace(0,10,100)
> pylab.plot(xx,f(xx),'r-',x,x,'b-')
>
> g = [(s,f(s)-s) for s in xx]
> h = np.array([(i,k,l) for i, (k,l) in enumerate(g)])
> for i,j in enumerate(h):
> if i < 98:
> if (h[i][2] > 0 and h[i+1][2] < 0) or (h[i][2] < 0 and
> h[i+1][2] > 0):
> print optimize.brentq(lambda x: f(x)-x, h[i][1],
> h[i+1][1])
>
>
> Mobile: 07788 872118
> Blog: www.alexfb.com
>
> --
> All science is either physics or stamp collecting.
>
>
> On 9 April 2010 08:23, seasoul <bioseasoul@gmail.com> wrote:
>
>>
>> A nice idea. Thank you Charles. I will give it a try.
>>
>> Charles R Harris wrote:
>> >
>> > 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
>> >
>> > _______________________________________________
>> > SciPy-User mailing list
>> > SciPy-User@scipy.org
>> > http://mail.scipy.org/mailman/listinfo/scipy-user
>> >
>> >
>>
>> --
>> View this message in context:
>> http://old.nabble.com/How-to-find-the-intersections-between-two-lines-tp28187442p28188548.html
>> Sent from the Scipy-User mailing list archive at Nabble.com.
>>
>> _______________________________________________
>> 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
>
>
:clap::clap:
--
View this message in context: http://old.nabble.com/How-to-find-the-intersections-between-two-lines-tp28187442p28200155.html
Sent from the Scipy-User mailing list archive at Nabble.com.
More information about the SciPy-User
mailing list