0

I'm new to python but I been working on a code which can solve an integral equation which range is also changing according the unknown parameter. I tried to use Sympy solve function but it does not return any result. I solved the problem with a for loop, but its really slow, inefficient. I'm sure there must be a better solution, a solver. Maybe there is an other approach? I'am messing up something? I attach also the code.


import sympy as sy
from sympy.solvers import solve

alphasum = 1.707
Lky = 3.078
g = 8
Ep = 195
sigp1 = 1401.927
sigp0 = 1476
e = 2.718282
u = 0.05
k = 0.007

lsl = sy.Symbol('lsl')


def lefthand(g, Ep):
    return g * Ep


def rigthhand(sigp1, sigp0, e, u, alphasum, Lky, k, lsl):
    return (sigp1 - (-sigp1 + 2 * sigp0 - 2 * sigp0 * (1 - e ** (-u * (((alphasum / Lky) * lsl) + k * lsl)))))


equr = (sy.integrate(rigthhand(sigp1, sigp0, e, u, alphasum, Lky, k, lsl), (lsl, 0, lsl)))
equl = lefthand(g, Ep)

print(equr)
print (equl)
print (equr-equl)

result = solve(equr-equl, lsl,warn =True,check=False,minimal=True,quick=True,simplify=True,quartics=True)

print(result)
4

2 回答 2

0

什么时候u是 0 你的方程是线性的,你可以解决 if for lsl; 让这成为 的值lsl在 的较大值时的初始猜测u。将您的值u增加到目标值。让我们将大写字母U用于我们将控制的参数,将其替换为越来越接近的值u

>>> U = Symbol('U')
>>> equr = (sy.integrate(rigthhand(sigp1, sigp0, e, U, alphasum, Lky, k, lsl), (lsl, 0, lsl)))
>>> equl = lefthand(g, Ep)
>>> z = equr-equl
>>> u0 = solve(z.subs(U,0),lsl)[0]
>>> for i in range(1,10):  # get to u in 9 steps
...   u0 = nsolve(z.subs(U,i*u/10.), u0)
>>> print(u0)
-4.71178322070344

现在定义一个更大的值u

>>> u = 0.3
>>> for i in range(1,10):  # get to u in 9 steps
...    u0 = nsolve(z.subs(U,i*u/10.), u0)
...
>>> u0
-2.21489271112540

我们最初的猜测(通常)会非常好,因为它是u0 的准确时间;如果失败,那么您可能需要采取 9 个以上的步骤才能达到目标值。

于 2020-05-20T22:58:45.133 回答
0

您可以使用nsolve来找到数值解:

In [11]: sympy.nsolve(equr-equl, lsl, 8)                                                                                          
Out[11]: 8.60275245116315

In [12]: sympy.nsolve(equr-equl, lsl, -4)                                                                                         
Out[12]: -4.53215114758428
于 2020-05-20T20:07:38.490 回答