我是 Python 和 SageMath 的新用户。
我有两个非线性方程:
f(x)==h(x)
g(x)+S_{i,j,k}(x) == 0
我知道我可以解决 1. 在数值上,做:
x = var('x')
find_root(f(x)==h(x), x, x_min, x_max)
在2.中,S_{i,j,k}(x)
是x
和i
的三重和函数,j
和k
是总和的索引。我怎样才能用数值求解它?
使用 Python 和 sympy ,您可以使用 sympy.mpmath.nsum()
定义 S_{i,j,k}(x)
函数,然后使用 sympy.mpmath.findroot()
:
import sympy.mpmath
x = sympy.symbols('x')
def S(x_):
return sympy.mpmath.nsum(lambda i, j: x_*i + j, [0, 2], [3, 4])
print('Function: {}'.format(S(x)))
print('Solution: {}'.format(sympy.mpmath.findroot(S, -1)))
打印:
Function: 6.0*x + 21.0
Solution: -3.5
我选择了一个线性示例,但它也适用于非线性方程。
我是一名优秀的程序员,十分优秀!