If you want to solve or manipulate the equations symbolically, you should use SymPy. If you want a numeric solution, you should use NumPy. If you want to start with SymPy expressions and go to NumPy functions, you should use lambdify.
In your code, you created some SymPy equations, but you aren't using them. The problem is that you used F
instead of p['F']
in your definition of d
, creating a NumPy array with an unevaluated SymPy symbol F
, which it doesn't know how to deal with. Actually if you remove the SymPy parts entirely your code will still work, since you're not using it at all to derive the equations or anything.
By the way, since your equation is linear, SymPy can solve it exactly. Here's how to do that if you are interested.
>>> p = {M0: 4000, L1: 4, F: 1000, L: 6, L2: 2, q: 2000}
>>> solve([eq_Fx.subs(p), eq_MB.subs(p), eq_MA.subs(p)], [Ax, Ay, By])
{B_y: 2000, A_x: 1000, A_y: 10000/3}
Note that I defined p
using the symbols themselves, not strings, so that subs
would replace them.
To actually generate the equations with SymPy and convert them to NumPy with lambdify
, you might use something like
>>> linear_eq_to_matrix([eq_Fx, eq_MB, eq_MA], [Ax, Ay, By])
(Matrix([
[1, 0, 0],
[0, -L, 0],
[0, 0, L]]), Matrix([
[ F],
[-L_1**2*q/2 - M_0],
[ L_1**2*q/2 - M_0]]))
>>> C, D = linear_eq_to_matrix([eq_Fx, eq_MB, eq_MA], [Ax, Ay, By])
>>> p = {'L': 6, 'L_1': 4, 'L_2': 2, 'q': 2000, 'F': 1000, 'M_0': 4000}
>>> fC = lambdify([M0, F, q, L, L1, L2], C, 'numpy', dummify=False)
>>> fC(**p)
array([[ 1, 0, 0],
[ 0, -6, 0],
[ 0, 0, 6]])
>>> c = fC(**p)
>>> fD = lambdify([M0, F, q, L, L1, L2], D, 'numpy', dummify=False)
>>> d = fD(**p)
>>> import numpy
>>> np.linalg.solve(c, d)
array([[ 1000. ],
[ 3333.33333333],
[ 2000. ]])
Here I'm defining p
using strings again, so that we can use them as keyword arguments to the lambdified functions (otherwise you have to pass them in order, like fC(4000, 1000, 2000, 6, 4, 2)
). The dummify=False
argument is also necessary to make this work.
The linear_eq_to_matrix
function converts the symbolic equations into SymPy matrices. lambdify
then lets you convert these SymPy matrices with symbolic elements into numeric NumPy arrays.
A final note: you would avoid a lot of confusion for yourself if you were consistent in the use or non-use of underscores in your Symbol and variable names, e.g., either use
M0 = symbols("M0")
or
M_0 = symbols("M_0")
SymPy will print the 0 as a subscript in either case.