从与0.0级相交的线获取值

问题描述

问题是从与0.0电平相交的红线(黑线)中找到值。红线绘制如下:

plt.plot(C,iG,color='r')
plt.plot(C,'o',color='r',markersize=6)

,黑线绘制为:

plt.axhline(y=0,color='k')

变量在这里

C = [0,20,40,60,80,100]
iG = [1.3872346819371657,0.7872943505903507,0.17782668886707143,-0.44058186267346144,-1.0673973968907333,-1.7021324469635957]

所以,只有一条线(即形成红线的6 x,y对)。

在这里

figure

解决方法

您可以简单地用数学方法解决。如果采用y = mx + c行,则x截距位于y = 0处,因此求解0 = mx + c

0 = mx + c  
-c = mx  
-c/m = x  

因此在y = mx + c行中x_intercept = -c/m = x

为了将变量转换为y = mx + c形式,请使用点斜率形式:y - y1 = m(x - x1),其中y1x1是您的点的坐标第(x1,y1)行。使用两个坐标找到斜率,然后完成方程式。和以前一样,y = 0:

0 - y1 = m(x - x1)
-y1 = mx - mx1
-y1/m = x - x1
-y1/m + x1 = x

下面如何执行此操作的代码:

# points inputted as a list of the x and y values of the points
def find_xintercept(point1,point2):

    # slope (m) = rise over run
    slope = (point1[1] - point2[1]) / (point1[0] - point2[0])
    return -point1[1] / slope + point1[0]

示例:

print(find_xintercept([C[0],iG[0]],[C[1],iG[1]]))

打印:

46.24575510110953