skrat skrat - 10 months ago 41
Python Question

How to prepare a system of equations for python

This VIDEO is a really nice demonstration for how to do it in a simplified case - in case of three equations and three variables.

Let's say I wan to solve the following system

enter image description here

for variables f1, x1 and x2. Since this is a rather small system I could easily do it manually. But this is a simplified example - in reality my system consists of 100 variables and 100 equations.

So my question is how to separate variables in order to solve this system? How to gather all the variables in one vector and rewrite the system so I can solve it?
In the end all I want is numerical values for f1, x1 and x2.

ps.: I just made this system by inserting random numbers. I'm not sure the system cae be solved but... you get the idea. (adjust the numerical values in that case).

Answer Source

As far as I understand, you have to adjust the matrix of your system to take care of RyA and other variables that currently in the right hand side. You can do it manually (in which case this question is out of the scope of this site, it is purely mathematical excercise) or use e.g. sympy instead of np.linalg.solve() which can do the algebra part of the problem for you:

from sympy import Matrix, symbols, solve

x1, x2, f1 = symbols('x1 x2 f1')
X = Matrix([0, x1, x2])
B = Matrix([f1, 50, 60])
M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

solve(M * X - B, [x1, x2, f1])