Solving a system of transcendental equations with python

久未见 提交于 2019-12-04 12:03:15

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)

that will print

[ 1.30301572  1.30987969  0.51530547  0.48469453]

Your functions have to be defined in a way that they evaluate to 0, e.g. a + b - 1 instead of a + b = 1.

A quick check:

print(your_funcs(sol2.x))

gives

[-1.9356960478944529e-11, 1.8931356482454476e-11, 0.0, -4.1039033282785908e-11]

So, the solution should be ok (please note that e-11 is basically 0).

Alternatively, you can also use fsolve:

from scipy.optimize import fsolve

sol3 = fsolve(your_funcs, [0.1, 0.1, 0.3, 0.1])

which gives you the same result:

[ 1.30301572  1.30987969  0.51530547  0.48469453]

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)

which gives you the "old" output:

[ 1.30301572  1.30987969  0.51530547  0.48469453]

If you run

sol2 = root(your_funcs, [0.1, 0.1, 0.3, 0.1], args=(0.4, 0.2))
print(sol2.x)

then you receive:

[ 1.26670224  1.27158794  0.34096159  0.65903841]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!