找到三次函数的根 [英] Find the root of a cubic function

查看:249
本文介绍了找到三次函数的根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是东西. 我试图在Python中使用fsolve函数来查找三次函数的根.此三次方函数的参数为​​deltaW.我要做的是将此参数deltaW-50更改为50,并同时找到三次函数的根.下面是我的脚本:

Here is the thing. I am trying to use fsolve function in Python to find the root of a cubic function. This cubic function has a parameter, deltaW. What I do is change this parameter deltaW from -50 to 50, and find the root of the cubic function at the same time. Below is my script:

from scipy.optimize import fsolve
import matplotlib.pyplot as plt
import numpy as np
import pylab

g = 5.61
gamma = 6.45
kappa = 6.45
J = 6.45
rs = 1.0                            #There are just parameters
m = 5.0*10**(-11)
wm = 2*3.14*23.4

X = []
X1 = []

def func(x):                                #Define the cubic function I  need to solve

        A = 1j*g**2*(kappa + 1j*deltaW)*x*x/(m*wm**2)
        B = J**2 + (1j*deltaW - gamma)*(1j*deltaW + kappa)
        C = A + B
        D = abs(C)*x - J*np.sqrt(2*kappa)*rs
        return D

for deltaW in np.linspace(-50, 50, 1000):
    x0 = fsolve(func, 0.0001)
    X.append(x0)

deltaW = np.linspace(-50, 50, 1000)
plt.plot(deltaW, X)    
plt.show()

运行此脚本时,我收到以下两条消息:

When I run this script, I get these two messages:

 RuntimeWarning: The iteration is not making good progress, as measured by the 
  improvement from the last five Jacobian evaluations.
  warnings.warn(msg, RuntimeWarning)

/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py:152: RuntimeWarning: The iteration is not making good progress, as measured by the 
  improvement from the last ten iterations.
  warnings.warn(msg, RuntimeWarning)

对不起,我没有足够的声誉来将此脚本的剧情放在这里.我的问题是为什么我会收到此消息,为什么我的情节在左侧看起来如此怪异.

I am sorry I do not have enough reputation to put the plot of this script here. My question is why do I get this message and why do my plot look so weird in the left part.

是因为我的代码错误吗?

Is it because of my code is wrong?

推荐答案

几乎在所有找到根的情况下,都必须有一个很好的初始猜测.有时,最好的初步猜测实际上是错误的.就是这种情况.可以通过绘制函数并在这些尖峰周围绘制找到的根源来更深入地查看脚本的行为,该行为在答案中显示了意外的尖峰"(嘿,您有一个Python控制台-真的很容易).
您发现的是,求解器返回的解决方案正在跳跃,尽管该函数看上去并没有什么不同.问题在于,您最初对0.0001的猜测接近该函数的极小最小值,而求解器无法弄清楚该如何退出该函数.将初始猜测值设置为1.0(相距很远,但是在函数的漂亮且容易下降的部分,它将直接指向根),结果是:

As in almost all cases of finding roots, a good initial guess is imperative. Sometimes the best initial guess is, in fact, known to be wrong. That is the case here. The behavior of your script, which shows unexpected 'spikes' in the answer, can be looked at more deeply by both plotting up the function, and plotting up the found roots around those spikes (hey, you've got a Python console - this is really easy).
What you find is that the solution returned by the solver is jumping around, even though the function really doesn't look that different. The problem is that your initial guess of 0.0001 lies close to a tiny minimum of the function, and the solver can't figure out how to get out of there. Setting the initial guess to 1.0 (way far away, but on a nice, easy descending portion of the function that will head directly to the root), results instead in:

因此,三件事: 1.求解器需要充满爱心的关怀和关注-他们很少会自动解决.

So, three things: 1. solvers need loving care and attention - they are rarely automagic.

  1. 有时候,正确的"初始猜测可能会与您所知道的正确答案相去甚远,但这种方式使求解器可以轻松应对.

  1. Sometimes the 'right' initial guess can be well away from what you know is the right answer, but in such a way that the solver has an easy time of it.

交互式Python控制台可让您快速查看发生了什么.利用它的力量!

the interactive Python console lets you look quickly at what is going on. Use the power of it!

这篇关于找到三次函数的根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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