Find the root of a cubic function

前端 未结 1 1730
清歌不尽
清歌不尽 2020-12-07 05:19

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

相关标签:
1条回答
  • 2020-12-07 05:31

    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: enter image description here

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

    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.

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

    0 讨论(0)
提交回复
热议问题