如何使用 Python 求解一对非线性方程? [英] How to solve a pair of nonlinear equations using Python?

查看:29
本文介绍了如何使用 Python 求解一对非线性方程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Python 求解一对非线性方程的(最佳)方法是什么.(Numpy、Scipy 或 Sympy)

What's the (best) way to solve a pair of non linear equations using Python. (Numpy, Scipy or Sympy)

例如:

  • x+y^2 = 4
  • e^x+ xy = 3

解决上述问题的代码片段会很棒

A code snippet which solves the above pair will be great

推荐答案

数值求解,可以使用fsolve:

for numerical solution, you can use fsolve:

http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fsolve.html#scipy.optimize.fsolve

from scipy.optimize import fsolve
import math

def equations(p):
    x, y = p
    return (x+y**2-4, math.exp(x) + x*y - 3)

x, y =  fsolve(equations, (1, 1))

print equations((x, y))

这篇关于如何使用 Python 求解一对非线性方程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆