Solving system of nonlinear equations with python

前端 未结 3 1869
面向向阳花
面向向阳花 2021-01-12 06:54

Can I solve a system of nonlinear equations in terms of parameters in python? Is there a example or tutorial? I can do this easily in maple, but the expressions for my parti

3条回答
  •  迷失自我
    2021-01-12 07:19

    Warning I'm a Sage developper, so I might not be neutral.

    I don't know how to do that in pure Python, but I would recommend the Sage system whose interface is in Python (actually the command line is a specifically configured IPython) and which allows to do such thing:

    +--------------------------------------------------------------------+
    | Sage Version 5.10, Release Date: 2013-06-17                        |
    | Type "notebook()" for the browser-based notebook interface.        |
    | Type "help()" for help.                                            |
    +--------------------------------------------------------------------+
    sage: var("sigma y x rho beta z")
    (sigma, y, x, rho, beta, z)
    sage: sys = [sigma*(y-x), x*(rho-z)-y, x*y-beta*z]
    sage: solve(sys, x, y, z)
    [[x == sqrt(beta*rho - beta), y == (beta*rho - beta)/(sqrt(rho - 1)*sqrt(beta)), z == rho - 1], [x == -sqrt(beta*rho - beta), y == -(beta*rho - beta)/(sqrt(rho - 1)*sqrt(beta)), z == rho - 1], [x == 0, y == 0, z == 0]]
    

    It is usually easier to use like this:

    sage: solve(sys, x, y, z, solution_dict=True)
    [{z: rho - 1,
      x: sqrt(beta*rho - beta),
      y: (beta*rho - beta)/(sqrt(rho - 1)*sqrt(beta))},
     {z: rho - 1,
      x: -sqrt(beta*rho - beta),
      y: -(beta*rho - beta)/(sqrt(rho - 1)*sqrt(beta))},
     {z: 0, x: 0, y: 0}]
    

    The main drawback is that Sage is a full distribution of math Software which ships its own Python interpreter (together with a huge bunch of other things written in many language including C/C++, Cython, lisp, fortran) and is notoriously hard to install if you want to use your own interpreter.

    A good news for your problem is that Scipy is already shipped with sage.

提交回复
热议问题