求两线交点

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/flyfish1986/article/details/82871214

求两线交点

flyfish

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import math
from scipy import optimize
from scipy.optimize import fsolve

#数据
x= np.arange(1, 140, 1)
y = np.array([55,65,74,82,90,97,103,109,114,118,120,122,123,124,123,121,118,114,
              110,104,98,91,83,75,66,57,47,36,26,15,4,-6,-17,-27,-38,-48,-58,-67,
              -76,-84,-92,-99,-105,-111,-115,-119,-121,-123,-124,-124,-122,-120,
              -117,-113,-109,-103,-96,-89,-81,-73,-64,-54,-44,-34,-23,-12,-2,8,19,
              30,40,51,60,70,78,86,94,101,107,112,116,119,122,123,124,123,122,119,
              116,112,107,101,94,87,79,70,61,51,41,31,20,9,-1,-12,-23,-33,-44,-54,
              -63,-72,-81,-89,-96,-103,-108,-113,-117,-120,-122,-123,-124,-123,-121,
              -119,-115,-111,-105,-99,-92,-85,-76,-68,-58,-48,-38,-28,-17,-6,4])

#直线
line = lambda x : 0 * x

def PointOfIntersection(func1, func2, x0):
    return fsolve(lambda x: func1(x) - func2(x), x0)

#曲线
fitting = np.polyfit(x, y, 15)
polynomial= np.poly1d(fitting)# 生成多项式对象

result = PointOfIntersection(polynomial, line, [27,59,93])
print("交点:")
print(result)#[ 31.42890063  67.17838009 102.86532738]
plt.plot(x, y, 'b-', label='Origin curve')
plt.plot(x,line(x), color="green",label = "Origin line")
plt.plot(result,line(result), "o", label = "intersection")
plt.plot(x, polynomial(x), 'r:', label='Poly Fitting curve(deg=15)')

plt.legend()
plt.show()

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/flyfish1986/article/details/82871214