Bi Ao
Bi Ao

Reputation: 894

How to substitute variable in multiple equations in Sympy

for example, if I want to use solve a set of linear equations

eq1: x + y + 8z = 2

eq2: 2x + 6y + z = 5

suppose I already know the value of z, is there any way I can subsitute the z in eq1 and eq2 in one operation such as

linear_equations([eq1, eq2]).subs({z: 100})

Upvotes: 1

Views: 286

Answers (1)

sunilbaba
sunilbaba

Reputation: 445

You can use the Sympy library to achieve what exactly you are looking to solve. Below is the code which will perform the substitution of the Z value and then solve the linear equation to find the values of x & Y values

from sympy import symbols
from sympy.solvers import solve
x,y,z = symbols('x y z')

expression1 = x + y + 8*z - 2
expression2 = 2*x + 6*y + z - 5
expression1 = expression1.subs(z,100)
expression2 = expression2.subs(z,100)

solution = solve([expression1, expression2], [x, y])
print(solution)

Upvotes: 1

Related Questions