[Python] How to eliminate intermediate variable and solve set of equations for fewer variables?

Discussão em 'Python' iniciado por Stack, Junho 12, 2025.

  1. Stack

    Stack Membro Participativo

    I have a bond graph program that produces a bunch of equation relations, but should only boil down to solving a couple ODE equations in terms of non-intermediate variables. By substituting intermediate relations the following set of equations:

    Eq(SE_01, e_01)
    Eq(f_02, p_02/I_02)
    Eq(e_05, q_05/C_05)
    Eq(e_03, R_03*f_03)
    Eq(f_06, e_06/R_06)
    Eq(e_02, pdot_02)
    Eq(f_05, qdot_05)
    Eq(f_01, f_02)
    Eq(f_02, f_03)
    Eq(f_02, f_04)
    Eq(e_01, e_02 + e_03 + e_04)
    Eq(e_04, e_05)
    Eq(e_05, e_06)
    Eq(f_04, f_05 + f_06)


    should reduce to:

    pdot_02 = -R_03*p_02/I_02 - q_05/C_05 + SE_01
    qdot_05 = p_02/I_02 - q_05/(C_05*R_06)


    How do I get sympy to do these types of substitutions?

    Tried using sym.solve on the hard coded problem, which worked??, but generalizing doesn't.


    solution_explicit = sym.solve(equations, [pdot_02, qdot_05, e_01, e_02, e_03, e_04, e_05, e_06, f_01, f_02, f_03, f_04, f_05, f_06], dict=True)

    print("\nExplicit solution for pdot_02 and qdot_05:")
    if solution_explicit:
    sol = solution_explicit[0]
    pdot_02_sol = sol.get(pdot_02)
    qdot_05_sol = sol.get(qdot_05)

    if pdot_02_sol is not None:
    sym.pprint(sym.simplify(sym.Eq(pdot_02, pdot_02_sol)))
    else:
    print("pdot_02 not found in this solution set.")

    if qdot_05_sol is not None:
    sym.pprint(sym.simplify(sym.Eq(qdot_05, qdot_05_sol)))

    Continue reading...

Compartilhe esta Página