SymPy `solve` fails to solve Matrix equation A*x = b when symbols are replaced by values with `subs`

Viewed 944

I have linear equation I want to solve, as Ax = b. I want to show step by step only in symbols and at the end insert numbers and show problem's solution in numbers. I have problems with inserting in numbers. If previously calculated values are whole numbers everything works fine. When I put in something with decimals it prints

[]

This is simplified code to illustrate my problem:

Force = 100 #If I try to put in value of lets say 100.23 the problem happens
x_t = 15
L = 20
A, x, b = sym.symbols('A, x, b')
A_x, A_y, B_y = sym.symbols('A_x, A_y, B_y')
b, F, xt, l = sym.symbols('b, F, xt, L')

A = sym.Matrix([[1, 0, 0],
            [0, 1, 0],
            [0, 0, 1]])
b = sym.Matrix([[0],
            [F],
            [F*xt/l]])
x = sym.Matrix([[A_x],
            [A_y + B_y],
            [B_y]])

linear_eq = sym.Eq(A * x, b)
solution = sym.solve(linear_eq, x) #This always works fine(only symbols)
solution = sym.solve(linear_eq.subs( {F: Force, xt: x_t, l: L }), x )
solution
1 Answers

Hmm, I'm not sure why solve doesn't work at all. You could use LUsolve, though. Which works for F: 100.23, too:

from sympy import *

A, x, b = symbols("A, x, b")
A_x, A_y, B_y = symbols("A_x, A_y, B_y")
b, F, x_t, L = symbols("b, F, x_t, L")

A = Matrix([
    [1, 0, 0],
    [0, 1, 0],
    [0, 0, 1]
])
x = Matrix([
    [A_x],
    [A_y + B_y],
    [B_y]
])
b = Matrix([
    [0],
    [F],
    [F * x_t / L]
])

solution = A.LUsolve(b.subs({
    F: 100.23,
    x_t: 15,
    L:20
}))
print(solution)

Which prints:

Matrix([[0], [100.230000000000], [75.1725000000000]])
Related