我有四個多項式(2 次)函式,我需要找到這些函式的交集。但我不知道有什么方法可以一步找到所有交叉點。我的建議是均衡兩個函式并使用numpy.roots. 但我不確定這是不是真的。我應該怎么做才能找到交叉點?我的職能是:
y = 1.51250 * x -0.07969 * x^2 18.96116
y = 1.54264 * x -0.05879 * x^2 17.47277
y = 1.49669 * x -0.04627 * x^2 17.69008
y = 1.72147 * x 0.00052 * x^2 18.21067
我等于兩個第一個方程來找到它們的根,我們將有:
-0.03014x-0.0209x^2 1.48839=0
我使用 np.roots 來查找根:
coeff=[-0.03014,-0.0209,1.48839]
np.roots(coeff)
根源是:
[-7.38253508, 6.68910443]
但是當我把這些根中的每一個都放在這個等式-0.03014x-0.0209x^2 1.48839=0中時,輸出不為零!問題是什么?
uj5u.com熱心網友回復:
如果存在一個點,所有四條曲線都具有相同的值,那么應該可以找到任何一條曲線與其他三條曲線的交點,以及由此產生的交點,你會選擇那條曲線他們之間很常見。除此之外,我想不出一種“一步到位”的方法。至于找到每對曲線之間的交點,我會這樣做:
import numpy as np
from numpy.polynomial import Polynomial as Poly
from matplotlib import pyplot as plt
# Your functions:
# y = 1.51250 * x -0.07969 * x^2 18.96116
# y = 1.54264 * x -0.05879 * x^2 17.47277
# y = 1.49669 * x -0.04627 * x^2 17.69008
# y = 1.72147 * x 0.00052 * x^2 18.21067
y1 = Poly((18.96116, 1.51250, -0.07969))
y2 = Poly((17.47277, 1.54264, -0.05879))
y3 = Poly((17.69008, 1.49669, -0.04627))
y4 = Poly((18.21067, 1.72147, -0.00052))
# Showing how to find intersections between y1 and y2 only
# you could repeat this for other combinations as you please:
y1_y2_intersections = (y1-y2).roots()
# This returns the two x values at which the functions y1 and y2 have the same value
# Plotting the curves and their intersections:
x = np.arange(-15, 15)
fig, ax = plt.subplots()
ax.plot(x, y1(x), label = 'y1') # Plotting y1
ax.plot(x, y2(x), label = 'y2') # Plotting y2
# Plotting the intersection points
for x, y in zip(y1_y2_intersections, y1(y1_y2_intersections)):
ax.plot(x, y, marker = 'x', color = 'red', linestyle = 'None', label = f'Root ({x:.2f}, {y:.2f})')
ax.grid()
ax.legend()
產生這個:

uj5u.com熱心網友回復:
您可以均衡任意兩個多項式并求解生成的二次方程。當然,這些方程中的任何一個都有解。也許其中一些(或全部)是虛構的或復雜的。如果您只想為預期目的使用真正的解決方案,請忽略這些解決方案。
uj5u.com熱心網友回復:
對于方程
-0.03014x-0.0209x^2 1.48839=0
系數應按正確順序排列
coeff=[-0.0209, -0.03014,1.48839]
結果為
np.roots(coeff)
是
[-9.19068954 7.74858428]
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/535659.html
標籤:Python数学多项式
下一篇:計算PI的值
