pythonmathsympy

How to simplify a linear system of equations by eliminating intermediate variables


I have a linear system shown in the block diagram below.

enter image description here

This system is described with the following set of linear equations:

err = inp - fb
out = a * err
fb = f * out

I would like to use sympy to compute the output (out) of the function of the input (inp). Thus, I would like to eliminate the variables err and fb. I would like some help as I been unable to figure out how to express what I want. So far I have:

from sympy import symbols, Eq, solve

inp, err, out, fb, a, f = symbols("inp err out fb a f")

eqns = [
    Eq(err, inp - fb),
    Eq(out, a * err),
    Eq(fb, f * out),
]

solution = solve(eqns, [out])

solution
# []

That clearly does not work.

I thought perhaps simplify() might help here, but I don't know how to apply the simplify function to a system of equations.

The result I am hoping to achieve is:

         a
out = ------ * inp
      1 + af

Can anyone point me in the right direction?


Solution

  • I think you should specify all "outgoing" parameters, e.g., [out, fb, err], rather than [out] only, since [inp, a, f] could be treated as "constants" in this system of equations.

    from sympy import simplify, symbols, Eq, solve
    inp, err, out, fb, a, f = symbols("inp, err, out, fb, a, f")
    
    eqns = (
        Eq(inp - fb, err),
        Eq(a * err, out),
        Eq(f * out,  fb)
    )
    
    solution = solve(eqns, [out, fb, err])
    

    and you will see

    {err: inp/(a*f + 1), fb: a*f*inp/(a*f + 1), out: a*inp/(a*f + 1)}