gpt4 book ai didi

python - 在 Python/SageMath 中求解非线性方程

转载 作者:太空宇宙 更新时间:2023-11-04 05:52:26 25 4
gpt4 key购买 nike

我是 Python 和 SageMath 的新用户。

我有两个非线性方程:

  1. f(x)==h(x)
  2. 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)xi的三重和函数,jk 是总和的索引。我怎样才能用数值求解它?

最佳答案

使用 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

我选择了一个线性示例,但它也适用于非线性方程。

关于python - 在 Python/SageMath 中求解非线性方程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29515078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com