3

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
finefoot
  • 9,914
  • 7
  • 59
  • 102
Simplex
  • 63
  • 4

1 Answers1

1

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]])
finefoot
  • 9,914
  • 7
  • 59
  • 102
  • Using LU factorization, nice! The only concern I have is that LU is normally used when we know A will always stay the same and b will change...If however both stay the same, looking from completely theoretical correct standpoint this doesn't make sense and we avoid using it. Also the "print" you get isn't the same as everything else I got using sympy. If I don't find any other way this is a perfect solution. Thank you! – Simplex Jan 02 '19 at 19:48