在Python中以编程方式解决联立线性方程的更好方法

时间:2017-12-12 12:57:43

标签: python-3.x math

我有以下代码,通过从第一个方程开始并在x = 0时找到y,然后将y放入第二个方程并找到x,然后将x放回第一个方程等,从而求解联立线性方程。 ..

显然,这有可能达到无穷大,所以如果它达到+ -inf那么它会交换方程的顺序,所以螺旋/梯子会反过来。

这似乎有用,因为我不是一个好的数学家,我可以证明它总会超越预感,当然有些线条永远不会满足(我知道如何使用矩阵和线性代数直接检查他们是否永远不会相遇,但我对那个atm并不那么感兴趣。

是否有更好的方法可以“回避”答案?我对整个解决方案使用数学函数或numpy不感兴趣 - 我希望能够对解决方案进行编码。我不介意使用库来提高性能,例如使用某种统计方法。

从编码或数学的角度来看,这可能是一个非常天真的问题,但如果是这样,我想知道为什么!

我的代码如下:

# A python program to solve 2d simultaneous equations
# by iterating over coefficients in spirals

import numpy as np

def Input(coeff_or_constant, var, lower, upper):
    val = int(input("Let the {} {} be a number between {} and {}: ".format(coeff_or_constant, var, lower, upper)))

    if val >= lower and val <= upper :
        return val
    else:
        print("Invalid input")
        exit(0)

def Equation(equation_array):
    a = Input("coefficient", "a", 0, 10)
    b = Input("coefficient", "b", 0, 10)
    c = Input("constant", "c", 0, 10)
    equation_list = [a, b, c]

    equation_array.append(equation_list)
    return equation_array

def Stringify_Equations(equation_array):
    A = str(equation_array[0][0])
    B = str(equation_array[0][1])
    C = str(equation_array[0][2])

    D = str(equation_array[1][0])
    E = str(equation_array[1][1])
    F = str(equation_array[1][2])

    eq1 = str(A + "y = " + B + "x + " + C)
    eq2 = str(D + "y = " + E + "x + " + F)

    print(eq1)
    print(eq2)

def Spiral(equation_array):
    a = equation_array[0][0]
    b = equation_array[0][1]
    c = equation_array[0][2]

    d = equation_array[1][0]
    e = equation_array[1][1]
    f = equation_array[1][2]

    # start at y when x = 0
    x = 0
    infinity_flag = False
    count = 0
    coords = []
    coords.append([0, 0])
    coords.append([1, 1])

    # solve equation 2 for x when y = START
    while not (coords[0][0] == coords[1][0]):
        try:
            y = ( ( b * x ) + c ) / a
        except:
            y = 0
        print(y)

        try:
            x = ( ( d * y ) - f ) / e
        except:
            x = 0
        if x >= 100000 or x <= -100000:
            count = count + 1
            if count >= 100000:
                print("It\'s looking like these linear equations don\'t intersect!")
                break
        print(x)

        new_coords = [x, y]
        coords.append(new_coords)
        coords.pop(0)

        if not ((x == float("inf") or x == float("-inf")) and (y == float("inf") or y == float("-inf"))):
            pass
        else:
            infinity_flag if False else True
            if infinity_flag == False:
                # if the spiral is divergent this switches the equations around so it converges
                # the infinity_flag is to check if both spirals returned infinity meaning the lines do not intersect
                # I think this would mostly work for linear equations, but for other kinds of equations it might not
                x = 0
                a = equation_array[1][0]
                b = equation_array[1][1]
                c = equation_array[1][2]

                d = equation_array[0][0]
                e = equation_array[0][1]
                f = equation_array[0][2]
                infinity_flag = False
            else:
                print("These linear equations do not intersect")
                break

    y = round(y, 3)
    x = round(x, 3)
    print(x, y)

equation_array = []
print("Specify coefficients a and b, and a constant c for equation 1")
equations = Equation(equation_array)
print("Specify coefficients a and b, and a constant c for equation 1")
equations = Equation(equation_array)
print(equation_array)
Stringify_Equations(equation_array)
Spiral(equation_array)

0 个答案:

没有答案