Solution of the 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)

for unknown variables x, y, aand b. Note that it cos(x)/x=ahas several solutions. Similarly for a variable y. I am only interested in the meanings xand y, which are the first positive roots (if that matters).

It is safe to assume that a, b, cand dare known real constants, all positive.

In Mathematica, the code for the solution would look something like this:

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}}]

which as a result returns

{x -> 1.31636, y -> 1.29664, a -> 0.456034, b -> 0.543966}

, , - python. , - ( , ), .

+3
1

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.

:

print(your_funcs(sol2.x))

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

, ( , e-11 0).

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:

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]
+4

Source: https://habr.com/ru/post/1673318/


All Articles