求解非线性方程组 [英] Solving nonlinear systems of equations

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

问题描述

我收到这个简单代码的错误,问题是错误只出现在我需要的一个方程中 (78 * x**0.3 * y**0.8 - 376).

I'm receiving an error with this simple code, the problem is that the error only appears with one of the equations that I need (78 * x**0.3 * y**0.8 - 376).

错误:在double_scalars中遇到无效值;F[0] = 78 * x**0.3 * y**0.8 - 376

如果我从第一个方程中删除 * y**0.8 ,代码运行完美,但显然它对我不起作用.

If I erase * y**0.8 from the first equation, the code runs perfectly, but obviously it doesn't work for me.

代码:

import numpy as np
from scipy.optimize import fsolve

def Funcion(z):
   x = z[0]
   y = z[1]

   F = np.empty((2))
   F[0] = 78 * x**0.3 * y**0.8 - 376
   F[1] = 77 * x**0.5 * y - 770
   return F

zGuess = np.array([1,1])
z = fsolve(Funcion,zGuess)
print(z)

推荐答案

你需要求解非线性方程组 F(x,y) = 0 服从于 x, y >= 0,这相当于最小化欧几里得范数 ||F(x,y)||英石.x,y >= 0. 要解决这个约束优化问题,您可以使用 scipy.optimize.minimize 如下:

You need to solve the nonlinear equation system F(x,y) = 0 subject to x, y >= 0, which is equivalent to minimize the euclidean norm ||F(x,y)|| s.t. x,y >= 0. To solve this constrained optimization problem, you can use scipy.optimize.minimize as follows:

import numpy as np
from scipy.optimize import minimize

def Funcion(z):
   x = z[0]
   y = z[1]

   F = np.empty((2))
   F[0] = 78 * x**0.3 * y**0.8 - 376
   F[1] = 77 * x**0.5 * y - 770
   return F

# initial point
zGuess = np.array([1.0, 1.0])

# bounds x, y >= 0
bounds = [(0, None), (0, None)]

# Solve the constrained optimization problem
z = minimize(lambda z: np.linalg.norm(Funcion(z)), x0=zGuess, bounds=bounds)

# Print the solution
print(z.x)

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

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