Newbie says Hi! and seeks help with subs()

18 views
Skip to first unread message

Jeremy Swift

unread,
Nov 23, 2022, 5:11:58 PM11/23/22
to sympy
Hi there,

I have just discovered sympy and am making my first tentative experiments. 

Over the years I've probably forgotten more maths than I remember so my knowledge is somewhere between basic engineering maths and multiplication tables. Having retired I'm trying to dredge some of it back from the depths.

I'm comfortable with programming and have been writing some python lately, primarily for use in Rhino3D.

I have some simultaneous equations to solve for four variables, F1, F2, F3 and F4. and a lot of given values V1x, V2x etc. Given the number of parameters, making substitutions manually is error prone, so I figured I'd get sympy to do the grunt work. However, subs() doesn't seem to be working for me. Could somebody show me where I am going wrong?

Here's a simplified interactive example:

>>> import sympy as s
>>> s.init_printing()
>>> F1,F2,F3,F4=s.symbols('F1 F2 F3 F4')
>>> V1x,V2x,V3x,V4x=s.symbols('V1x V2x V3x V4x')
>>> V1y,V2y,V3y,V4y=s.symbols('V1y V2y V3y V4y')
>>>
>>> eqx=F1*V1x+F2*V2x+F3*V3x+F4*V4x
>>> eqy=F1*V1y+F2*V2y+F3*V3y+F4*V4y
>>> eqx
F₁⋅V1x + F₂⋅V2x + F₃⋅V3x + F₄⋅V4x
>>> eq1=s.solve(eqx, F1)
>>> eq1
⎡-F₂⋅V2x - F₃⋅V3x - F₄⋅V4x⎤
⎢─────────────────────────⎥
⎣           V1x           ⎦
>>> eq2=eqy.subs(F1, eq1)
>>> eq2
F₁⋅V1y + F₂⋅V2y + F₃⋅V3y + F₄⋅V4y
>>> 

I was expecting sympy to substitute the solution in eq1 into the formula in eqy, but that hasn't happened. Why not?

TIA
Jeremy

Aaron Meurer

unread,
Nov 24, 2022, 12:02:19 AM11/24/22
to sy...@googlegroups.com
The problem is that eq1 is a list. solve() returns a list because in
general an equation can have more than one solution. To get the
solution, use eq1[0]. This probably should have produced an error, but
for some reason it didn't.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/dc6d0e11-7407-435e-b6ca-ae434487dbecn%40googlegroups.com.

Jeremy Swift

unread,
Nov 24, 2022, 3:22:05 PM11/24/22
to sympy
Thanks for the quick advice, Aaron. I'm making progress again as a result.

Jeremy

Reply all
Reply to author
Forward
0 new messages