对于方程组,fsolve,brentq和root的用法和精度有何不同? [英] What's the difference in the use and precision of fsolve, brentq and root for a system of equations?

查看:219
本文介绍了对于方程组,fsolve,brentq和root的用法和精度有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经问过这个问题对任何方程组都很好?,从中我得到了满意的答案.我在那里展示的系统

I have asked this question Is fsolve good to any system of equations?, from which I got a satisfactory answer. The system I presented there

x = A * exp(x + y)

x = A * exp (x+y)

y = 4 * exp(x + y)

y = 4 * exp (x+y)

只是一个玩具模型,与我的真实案例问题类似,fsolve进行了处理(以下答案中的代码):

, is just a toy model which is similar with my real case problem, fsolve did the work with (code in the answer below):

from scipy.optimize import fsolve
import matplotlib.pyplot as plt
import numpy as np
def f(p,*args):
  x, y = p
  A = args[0] 
 
  return (x -A* np.exp(x+y),y- 4* np.exp(x+y))
A = np.linspace(0,4,5)
X = []
Y =[]
for a in A:
  x,y =  fsolve(f,(0.0, 0.0) , args=(a))
  X.append(x)
  Y.append(y)
  print(x,y)

plt.plot(A,X)
plt.plot(A,Y)

但是,我在这里阅读stackoverflow.com/questions/6519380/…brenqtfsolve快得多.然后,我尝试使用它,但一直获取f(a) and f(b) must have different signs.我知道f must be continuous. f(a) and f(b) must have opposite signs.因此,我相信brenqt对于该系统不是一个好的选择.如果我错了,请纠正我.

However, I read here stackoverflow.com/questions/6519380/… that brenqt is much faster than fsolve. I've tried then to use it but keep getting f(a) and f(b) must have different signs. I understand that f must be continuous. f(a) and f(b) must have opposite signs. So, I believe brenqt is not a good choice for this system. Please correct me if I'm wrong here.

在我的真实情况下,我恰好在这里遇到了答案如何在python中求解3个非线性方程式说,即"fsolve())对初始条件非常敏感".我要避免首先最小化平方和".因为我有比该问题的操作要多的参数.如何使用optimize.root产生与我在原始问题中使用fsolve得到的结果相似的结果?

In my real case I'm encountering exactly what the answer here how to solve 3 nonlinear equations in python says, i.e."fsolve()) is quite sensitive to initial conditions" I want to avoid to "firstly minimize the sum-of-squares" as I have many more parameters than the OP of that question. How to use optimize.root to produce a similar result as the one I got with fsolve in my original question?

推荐答案

我现在理解(由于上面的评论),brentq仅适用于标量函数.我确实找到了optimize.root很好的解决方案,并且用他们的一些可用方法也给出了很好的解决方案,例如:

I now understand (thanks to the comment above) that the brentq only works for scalar functions. I did found a good solution with optimize.root and it gives a good solution with some of their available methods, for example:

def f(p,*args):
   x,y = p
   A = args[0] 
   return (x -A* np.exp(x+y),y- 4* np.exp(x+y))
A = np.linspace(0,4,5)
X = []
Y =[]
for a in A: 
   sol=optimize.root(f,[1.0,10.0],args=(a),method='lm')
   sol.message
   x,y= sol.x[0],sol.x[1]
   X.append(x)
   Y.append(y)
   print(x,y)
plt.plot(A,X)
plt.plot(A,Y)

由于求解器对它非常敏感,因此我仍在努力为我的系统获取合适的method.例如,如果我在上面的相同代码中使用method='broyden',则会得到完全不同的解决方案. 我将发布另一个问题以寻求帮助.

I'm still struggling to get an appropriate method to my system, as the solver is extremely sensitive to it. For example, if I use method='broyden' in the same code above I get a completely different solution. I'll post another question to ask for help.

这篇关于对于方程组,fsolve,brentq和root的用法和精度有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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