简体   繁体   English

如何解决Python中的(x+1)e^x=c(constant)?

[英]How to solve (x+1)e^x=c(constant) in Python?

I would like to solve the (x+1)e^x=c equation in Python.我想解 Python 中的(x+1)e^x=c方程。

The equation has been successfully solved by hand using lambert w functions as depicted in the figure below:该方程已成功地使用朗伯 w 函数手动求解,如下图所示:

在此处输入图像描述

Using same steps, I would like to solve (x+1)e^x programmatically.使用相同的步骤,我想以编程方式解决(x+1)e^x I have coded it using the module SymPy as per the step shown in the figure above, but without success.我已经按照上图所示的步骤使用模块 SymPy 对其进行了编码,但没有成功。

Is there any to solve these kinds of equations in Python?在 Python 中有没有解决这些方程的方法?

import numpy as np
from sympy import *
n = symbols('n')
sigmao=0.06866
sigmas=0.142038295
theta=38.9
rad=(np.pi/180)*38.9076
cos=np.cos(rad)
sec=1/np.cos(rad)
out = (0.06*0.7781598455*n*(1-exp(-2*0.42*sec*n))+exp(-2*0.42*n*sec)*sigmas)/sigmao
#Apply diff for the above expression. 
fin=diff(out, n)
print(solve(fin,n))
from scipy.optimize import fsolve
import numpy as np

const = 20
def func(x):
    return [(x[0]+1) * np.exp(x[0]) - const]

result = fsolve(func, [1])[0]
print('constant: ', const, ', solution: ', result)
#check
print('check: ', (result+1) * np.exp(result))


#Output[]:
    constant:  20.0 , solution:  1.9230907433218063
    check:  20.0

Preview: https://onlinegdb.com/By8Z2Jwgw预览: https://onlinegdb.com/By8Z2Jwgw

Your expression is very numeric.你的表达非常数字化。 As sympy's solve tries to find a perfect symbolic solution, sympy gets into troubles.当 sympy 的solve试图找到一个完美的符号解决方案时,sympy 陷入了困境。

To find numeric solutions, sympy has nsolve (which allows sympy's expressions but behind the scenes calls mpmath's numeric solver).为了找到数值解,sympy 有nsolve (它允许 sympy 的表达式,但在幕后调用 mpmath 的数值求解器)。 Unlike solve , here an initial guess is needed:solve不同,这里需要一个初始猜测:

from sympy import symbols, exp, diff, nsolve, pi, cos

n = symbols('n')
sigmao = 0.06866
sigmas = 0.142038295
theta = 38.9076
rad = (pi / 180) * theta
sec = 1 / cos(rad)
out = (0.06 * 0.7781598455 * n * (1 - exp(-2 * 0.42 * sec * n)) + exp(-2 * 0.42 * n * sec) * sigmas) / sigmao
# Apply diff for the above expression.
fin = diff(out, n)

result = nsolve(fin, n, 1)
print(result, fin.subs(n, result).evalf())

Result: 1.05992379637846 -7.28565300819065e-17结果: 1.05992379637846 -7.28565300819065e-17

Note that when working with numeric values, you should be very careful to use as many digits as possible to avoid accumulation of errors.请注意,在使用数值时,您应该非常小心地使用尽可能多的数字以避免累积错误。 Whenever you have an exact expression, it is recommended to leave that expression into the code, instead of replacing it with digits.每当您有一个精确的表达式时,建议将该表达式留在代码中,而不是用数字替换它。 (Usually, 64 bits or about 16 digits are used in calculations, but for intermediate calculations 80 bits can be taken into account). (通常在计算中使用 64 位或大约 16 位,但对于中间计算,可以考虑 80 位)。

To solve the original question with sympy:用 sympy 解决原来的问题:

from sympy import symbols, Eq, exp, solve

x = symbols('x')
solutions = solve(Eq((x + 1) * exp(x), 20))
for s in solutions:
    print(s.evalf())

Result: 1.92309074332181结果: 1.92309074332181

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM