【问题标题】:How to solve Equation without any Modules in Python?如何在没有任何模块的 Python 中求解方程?
【发布时间】:2019-01-28 13:55:20
【问题描述】:

我想在没有任何模块(NumPy、Sympy...等)的情况下求解这个方程

Px + Qy = W

(例如 5x + 6y = 55)

谢谢。

【问题讨论】:

  • 您在寻找什么答案?您有 1 个方程,但有 2 个变量。您需要x(y) = ... 还是y(x) = ...
  • @Sqoshu 对不起。我的意思是输出满足方程的 x 和 y 值的代码。
  • 好吧,在这种情况下,除了暴力破解 for 循环、在特定范围内迭代值并接受一些错误之外,您别无他法,因为这类任务会对所有数字都有无限的答案。
  • 我明白了。感谢你们对我的帮助!祝你有美好的一天:D

标签: python-3.x


【解决方案1】:

这是一种非常粗略的方法,但您可以使用蛮力技术,正如我在您的问题下的评论中所说的那样。它可能可以优化很多,只给出int 输出,但总体显示方法:

import numpy as np

# Provide the equation:
print("Provide a, b and c to evaluate in equation of form {ax + by - c = 0}")
a = float(input("a: "))
b = float(input("b: "))
c = float(input("c: "))

x_range = int(input("x-searching range (-a, a): "))
y_range = int(input("y-searching range (-b, b): "))
error = float(input("maximum accepted error from the exact solution: "))

x_range = np.arange(-x_range, x_range, 1)
y_range = np.arange(-y_range, y_range, 1)

for x in x_range:
    for y in y_range:
        if -error <= a * x + b * y - c <= error:
            print("Got an absolute error of {} or less with numbers x = {} and y = {}.".format(error, x, y))

a = 1b = 2c = 3x_range = 10y_range = 10error = 0.001 的示例输出:

Got an error of 0.001 or less with numbers x = -9 and y = 6.
Got an error of 0.001 or less with numbers x = -7 and y = 5.
Got an error of 0.001 or less with numbers x = -5 and y = 4.
Got an error of 0.001 or less with numbers x = -3 and y = 3.
Got an error of 0.001 or less with numbers x = -1 and y = 2.
Got an error of 0.001 or less with numbers x = 1 and y = 1.
Got an error of 0.001 or less with numbers x = 3 and y = 0.
Got an error of 0.001 or less with numbers x = 5 and y = -1.
Got an error of 0.001 or less with numbers x = 7 and y = -2.
Got an error of 0.001 or less with numbers x = 9 and y = -3.

我正在使用numpy,但不是用于求解方程本身的内置函数,只是为了创建一个数组。当然,这可以在没有它的情况下完成。

【讨论】:

    【解决方案2】:

    使用 python 求解方程有数千种方法。

    其中之一是:

    def myfunc (x=None, y=None):
        return ((55-6*y)/5.0) if y else ((55-5*x)/6.0)
    
    
    print(myfunc(x=10)) # OUTPUT: 0.833333333333, y value for x == 10
    print(myfunc(y=42)) # OUTPUT: -39.4, x value for y == 42
    

    您只需在函数内部定义求解方程所需的步骤。
    在我们的示例中,如果我们有 y 值,我们将 6*y 减去 55 然后除以 5.0(我们添加 .0 以得到浮点数),否则(意味着我们有 x)我们从55 中减去5*x,然后除以6.0

    同理,可以概括:

    def myfunc (x=None, y=None, P=None, Q=None, W=None):
        if not W:
            return P*x + Q*y
        elif not x:
            return (W-Q*y)/float(P)
        elif not y:
            return (W-P*x)/float(Q)
        elif not P:
            return (W-Q*y)/float(x)
        elif not Q:
            return (W-P*x)/float(y)
    
    
    print(myfunc(x=10, P=5, Q=6, W=55)) # OUTPUT: 0.833333333333, y value for x == 10
    print(myfunc(y=42, P=5, Q=6, W=55)) # OUTPUT: -39.4, x value for y == 42
    

    查看this QA 了解解决此问题的其他有趣方法

    【讨论】:

    • 谢谢你给我答复!但我的意思是输出满足方程的 x 和 y 值的代码。
    猜你喜欢
    • 1970-01-01
    • 2021-12-22
    • 2017-07-05
    • 2020-09-15
    • 2012-05-17
    • 2011-11-01
    • 2022-07-06
    • 1970-01-01
    相关资源
    最近更新 更多