如何自动组合不同的方程式

发布于 2025-01-22 14:20:15 字数 370 浏览 0 评论 0原文

我有9个不同的方程,只有7个未知数。我想生成一个代码,该代码创建7个方程的所有可能系统,并返回所有变量都具有积极结果的情况。 9个方程是:

    eq1 = 14*x+7*z-21.71
    eq2 = 15*x+11*z+w-38.55
    eq3=12*x+8*y+12*z+w-52.92
    eq4=12*x+8*y+14*z+t-61.7
    eq5=13*x+8*y+15*z+t-69.37
    eq6=4*x+17*y+14*r+s-98.32
    eq7=4*x+18*y+12*w+s-130.91
    eq8=4*x+18*y+15*w+2*t-165.45
    eq9=4*x+18*y+12*w+2*s-168.16

I have 9 different equations that contain only 7 unknowns. I would like to generate a code that creates all the possible systems of 7 equations and return the cases in which all the variables have a positive result. The 9 equations are:

    eq1 = 14*x+7*z-21.71
    eq2 = 15*x+11*z+w-38.55
    eq3=12*x+8*y+12*z+w-52.92
    eq4=12*x+8*y+14*z+t-61.7
    eq5=13*x+8*y+15*z+t-69.37
    eq6=4*x+17*y+14*r+s-98.32
    eq7=4*x+18*y+12*w+s-130.91
    eq8=4*x+18*y+15*w+2*t-165.45
    eq9=4*x+18*y+12*w+2*s-168.16

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不知所踪 2025-01-29 14:20:15

改编自 this 回答

您想做的事情是通过迭代的所有仅具有7方程式的组合。您可以使用嵌套循环进行此操作,但是它会变得非常丑陋,因为您将拥有7嵌套。

itertools python中的库(标准库)具有以下循环的内置方法,因此,如果创建一个方程列表,则可以通过执行以下

import itertools
eq = [eq1,eq2eq3,eq4,eq5,eq6,eq7,eq8,eq9]

for system in itertools.combinations(eq, 7):
    # do stuff to solve equations however you want

system < /代码>将是系统中包含7方程的元组。从那里使用任何要解决的求解技术,并确定所有输出是否为正(即numpy和矩阵方程)

Adapted from this answer

What you want to do is iterate through all the combinations that only have 7 equations. You can do this with nested loops but it will get very ugly as you will have 7 nests.

itertools library in python (standard library) has a built in method for looping like this, so if you create a list of equations, you can iterate through all unique systems by doing the following

import itertools
eq = [eq1,eq2eq3,eq4,eq5,eq6,eq7,eq8,eq9]

for system in itertools.combinations(eq, 7):
    # do stuff to solve equations however you want

system is going to be a tuple containing the 7 equations in the system. From there use whatever solving technique you want to solve them and determine if all outputs are positive (i.e. numpy and matrix equations)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文