Finding intersection between straight line and contour

前端 未结 3 1507
不知归路
不知归路 2021-01-20 01:49

I am trying to find the intersection point of a straight(dashed red) with the contour-line highlighted in red(see plot). I used .get_paths in the second plot to isolate said

3条回答
  •  独厮守ぢ
    2021-01-20 02:28

    Use shapely can find the intersection point, than use the point as the init guess value for fsolve() to find the real solution:

    #for contour 
    def p_0(num,t) :
        esc_p = np.sum((((-1)**n)*(np.exp(t)**n)*((math.factorial(n)*((n+1)**0.5))**-1)) for n in range(1,num,1))
        return esc_p+1
    
    tau = np.arange(-2,3,0.1)
    
    x,y= np.meshgrid(tau,tau)
    cs = plt.contour(x, y, np.log(p_0(51, y)/p_0(51, x)),[0.2],colors='k')
    
    p=0.75
    logp = (np.log(p*np.exp(tau)))
    plt.plot(tau,logp)
    
    from shapely.geometry import LineString
    v1 = cs.collections[0].get_paths()[0].vertices
    
    ls1 = LineString(v1)
    ls2 = LineString(np.c_[tau, logp])
    points = ls1.intersection(ls2)
    x, y = points.x, points.y
    
    from scipy import optimize
    
    def f(p):
        x, y = p
        e1 = np.log(0.75*np.exp(x)) - y
        e2 = np.log(p_0(51, y)/p_0(51, x)) - 0.2
        return e1, e2
    
    x2, y2 = optimize.fsolve(f, (x, y))
    
    plt.plot(x, y, "ro")
    plt.plot(x2, y2, "gx")
    
    print x, y
    print x2, y2
    

    Here is the output:

    0.273616328952 -0.0140657435002
    0.275317387697 -0.0123646847549
    

    and the plot:

    enter image description here

提交回复
热议问题