Home > Enterprise >  How to combine different equations automatically
How to combine different equations automatically

Time:04-19

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

CodePudding user response:

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)

  • Related