求三次函数的根 [英] Find the root of a cubic function
问题描述
事情就是这样.我正在尝试在 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.
有时,正确"的初始猜测可能与您所知道的正确答案相去甚远,但这样求解器就可以轻松应对.
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屋!