用python求解超越方程组 [英] Solving a system of transcendental equations with python
问题描述
假设我有以下四个等式:
Assuming I have the following four equations:
- cos(x)/x = a
- cos(y)/y = b
- a + b = 1
- c sinc(x) = d sinc(y)
对于未知变量x, y, a
和b
.请注意,cos(x)/x=a
有多种解决方案.变量 y
也类似.我只对 x
和 y
值感兴趣,它们是第一个正根(如果重要的话).
for unknown variables x, y, a
and b
. Note that cos(x)/x=a
has multiple solutions. Similar goes for variable y
. I am only interested in x
and y
values, which are first positive roots (if that matters).
您可以安全地假设 a, b, c
和 d
是已知的实常数,都是正的.
You can safely assume a, b, c
and d
are known real constants, all positive.
在 Mathematica 中,解决这个问题的代码如下所示:
In Mathematica the code to solve this would look something like:
FindRoot[{Cos[x]/x == 0.2 a + 0.1,
Cos[y]/y == 0.2 b + 0.1,
a + b == 1.0,
1.03*Sinc[x] == Sinc[y]*1.02},
{{x, .1}, {y, .1}, {a, .3}, {b, .1}}]
结果返回
{x -> 1.31636, y -> 1.29664, a -> 0.456034, b -> 0.543966}
虽然这很容易,但我不知道如何在 python 中做这样的事情.因此,如果有人能指导我(或简单地告诉我如何)解决这个问题,我将不胜感激.
While this was quite easy, I have no idea how to do anything like that in python. So if somebody could kinda guide me (or simply show me how) to solve this, I would highly appreciate it.
推荐答案
您可以使用 root
:
You can use root
:
import numpy as np
from scipy.optimize import root
def your_funcs(X):
x, y, a, b = X
f = [np.cos(x) / x - 0.2 * a - 0.1,
np.cos(y) / y - 0.2 * b - 0.1,
a + b - 1,
1.03 * np.sinc(x) - 1.02 * np.sinc(y)]
return f
sol2 = root(your_funcs, [0.1, 0.1, 0.3, 0.1])
print(sol2.x)
会打印
[ 1.30301572 1.30987969 0.51530547 0.48469453]
您的函数必须以求值为 0 的方式定义,例如a + b - 1
而不是 a + b = 1
.
Your functions have to be defined in a way that they evaluate to 0, e.g. a + b - 1
instead of a + b = 1
.
快速检查:
print(your_funcs(sol2.x))
给予
[-1.9356960478944529e-11, 1.8931356482454476e-11, 0.0, -4.1039033282785908e-11]
所以,解应该没问题(请注意e-11
基本为0).
So, the solution should be ok (please note that e-11
is basically 0).
或者,您也可以使用 fsolve
:
Alternatively, you can also use fsolve
:
from scipy.optimize import fsolve
sol3 = fsolve(your_funcs, [0.1, 0.1, 0.3, 0.1])
给你同样的结果:
[ 1.30301572 1.30987969 0.51530547 0.48469453]
您可以使用 args
参数传递其他参数:
You can pass additional arguments using the args
argument:
def your_funcs(X, fac_a, fac_b):
x, y, a, b = X
f = [np.cos(x) / x - fac_a * a - 0.1,
np.cos(y) / y - fac_b * b - 0.1,
a + b - 1,
1.03 * np.sinc(x) - 1.02 * np.sinc(y)]
return f
sol2 = root(your_funcs, [0.1, 0.1, 0.3, 0.1], args=(0.2, 0.2))
print(sol2.x)
它为您提供旧"输出:
[ 1.30301572 1.30987969 0.51530547 0.48469453]
如果你跑
sol2 = root(your_funcs, [0.1, 0.1, 0.3, 0.1], args=(0.4, 0.2))
print(sol2.x)
然后你会收到:
[ 1.26670224 1.27158794 0.34096159 0.65903841]
这篇关于用python求解超越方程组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!