Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sympy, I can't solve an equation symbolically. It keeps trying to give real solutions

import sympy as sym

# Python 3.13
# Used variables in question
x = sym.Symbol("x")
I1 = sym.Symbol("I₁")
L = sym.Symbol("L")
pi = sym.Symbol("Π")
v2 = sym.Symbol("v2")
P = sym.Symbol("P")
E = sym.Symbol("E")

I = I1 * (1 - x/L)

# For the part a of the question one in Assignment 3.
question_part = "a"
if question_part == "a":
    func_v = (1 - sym.cos((pi*x)/(2*L))) * v2
    moment = P * L
    second_derivative_v = sym.diff(func_v, x, x)
    internal_strain_e_func = 0.5 * moment *second_derivative_v / (E * I)
    internal_strain_e = sym.integrate(internal_strain_e_func, (x, 0, L))
    external_e = P * v2
    tip_deflection_eq = sym.Eq(internal_strain_e, external_e)
    tip_deflection = sym.solve(tip_deflection_eq, v2, exclude=[I1, L, pi, P, E], implicit=True)
    sym.pprint(tip_deflection)

Code above is for virtual displacement method for a beam deflection question. I can technically do it by hand however, I get a lot of questions that requires for me to solve integrals and I do make mistakes when I do it by hand. So in this one I thought I would do it by sympy and its okey up until the solve() method. For some reason I can't get a symbolically written solution for eq. The eq. I am trying to solve is:

enter image description here

Edit: I am sorry everyone v2 is not supposed be a part in function v. The question gave it to me and I took it without thinking about it although I did learn stuff that I didn't know and would probably ask later so not much of a waste of your time. Thanks!

like image 334
Hür Doğan ÜNLÜ Avatar asked Nov 17 '25 06:11

Hür Doğan ÜNLÜ


1 Answers

Don't declare a symbol called pi. You should use SymPy's sympy.pi if you mean the mathematical number pi.

This is your equation as stated:

In [42]: x, L, v2, E, I1, P, v2 = symbols('x, L, v2, E, I1, P, v2')

In [43]: eq = Eq(Integral((P*(L - x)*(pi/(2*L))**2*cos((pi*x)/(2*L)) * v2)/(E*I1*(1 - x/L)), (x, 0, L
    ...: )), P*v2)

In [44]: eq
Out[44]: 
L                                   
⌠                                   
⎮  2                 ⎛π⋅x⎞          
⎮ π ⋅P⋅v₂⋅(L - x)⋅cos⎜───⎟          
⎮                    ⎝2⋅L⎠          
⎮ ──────────────────────── dx = P⋅v₂
⎮            2 ⎛    x⎞              
⎮    4⋅E⋅I₁⋅L ⋅⎜1 - ─⎟              
⎮              ⎝    L⎠              
⌡                                   
0  

We can use doit to evaluate the integral and then solve for v2:

In [45]: eq.doit()
Out[45]: 
π⋅P⋅v₂       
────── = P⋅v₂
2⋅E⋅I₁       

In [46]: solve(eq.doit(), v2)
Out[46]: [0]

Apparently this is not the answer you were looking for but in general only v2 = 0 can solve the equation for arbitrary values of the parameters.

Maybe you wanted to solve for something else rather than v2?

This is the equation rearranged:

In [47]: eq2 = eq.doit()

In [48]: eq3 = Eq(factor(eq2.lhs - eq2.rhs), 0)

In [49]: eq3
Out[49]: 
-P⋅v₂⋅(2⋅E⋅I₁ - π)     
─────────────────── = 0
      2⋅E⋅I₁ 

At least one of the factors in the numerator on the lhs must be zero to satisfy the equation. If you want a value of v2 that can satisfy this then in general it has to be v2 = 0.

Maybe the equation has not been put together correctly?

like image 158
Oscar Benjamin Avatar answered Nov 18 '25 21:11

Oscar Benjamin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!