无论如何,我可以从 3 个线性方程中找到 5 个点的交点吗?找到交点后还需要方程图

Is there anyway I can find intersection of 3 points from 5 linear equations? Need a plot for the equations also after finding the intersection points

提问人:SMSM 提问时间:9/11/2023 更新时间:9/11/2023 访问量:24

问:

我有 5 个方程,我必须一次组合 3 个方程来检查我是否得到了 3 条线的任何交点。 使用的公式:

Eq1: y=-11.5 (slope zero)
Eq2: y=34.01+1.333 x
Eq3 : y=11.2449 +0.6666 x
Eq4 : x=-31.0
Eq5 : y=13.8283 +0.75 x

以 Xmin=-44, Xmax = -20, Ymin = -18 Ymax=2.0 为例,当您使用 X 的值时,您必须给出 Xmin 和 Xmax 之间的值。 绘制交点图并展开方程。

from itertools import combinations
from sympy import symbols, Eq, solve

# Define symbols for x and y
x, y = symbols('x y')

# List of equations in the form y = mx + c
equations = [
    Eq(y, 0*x - 11.5),
    Eq(y, 1.333*x + 34.01),
    Eq(y, 0.6666*x + 11.2449),
    Eq(x, 0*y - 31.0),
    Eq(y, 0.75*x + 13.8283)
]

# Generate all combinations of 3 equations
equation_combinations = list(combinations(equations, 3))
print(equation_combinations)

solution = []

# Calculate and compare intersection points for each combination
for eq_combination in equation_combinations:
    eq1, eq2, eq3 = eq_combination

    eq1_m, eq1_c = eq1.rhs.as_coefficients_dict()[x], eq1.rhs.as_coefficients_dict()[1]
    eq2_m, eq2_c = eq2.rhs.as_coefficients_dict()[x], eq2.rhs.as_coefficients_dict()[1]
    eq3_m, eq3_c = eq3.rhs.as_coefficients_dict()[x], eq3.rhs.as_coefficients_dict()[1]

    # Solve for intersection points of first two equations
    solution12 = solve((Eq(y, eq1_m*x + eq1_c), Eq(y, eq2_m*x + eq2_c)), (x, y))
    x12, y12 = solution12[x], solution12[y]
    x12 = round(x12, 1)
    y12 = round(y12, 1)
    solution.append(solution12)
    print(solution)

    # Calculate y value for the third equation at x12
    y3 = eq3_m * x12 + eq3_c
    y3 = round(y3, 1)
    x3 =  (y12 - eq3_c)/eq3_m
    x3 = round(x3, 1)
    print('y3:', y3)

    # Compare y3 with y12 within a tolerance of 3
    if abs(y3 - y12) <= 3:
        # Reduce decimal points for output
        x12 = round(x12, 1)
        y12 = round(y12, 1)
        print(f"Common intersection point: ({x12}, {y12})")

这是我得到的错误: 当我更改方程的顺序时,我遇到了错误,而且我没有得到所需的 2 个交点,而是得到了 3 个点。 <cell line: 23>() 中的类型错误
30 # 求解前两个方程的交点 31 solution12 = solve((方程(y, eq1_m x + eq1_c), 方程(y, eq2_m x + eq2_c)), (x, y)) ---> 32 x12,y12 = 解 12[x]、解 12[y] 33 x12 = 圆形(x12, 1) 34 Y12 = 圆形(Y12, 1)

TypeError: list indices must be integers or slices, not Symbol
python-3.x 数学 线性方程 线交集

评论


答: 暂无答案