-
-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BUG: fsolve + pchip + max, can't find root #19370
Comments
Someone on stack found that if number of iterations was high enough, scipy.optimize.minimize() will return a non initial guess. But if you just run the default it returns the initial guess after about a minute (Matlab finds the root in about 3 seconds on the same machine) |
@mdhaber is it possible for this to get tagged? Perhaps I didn't meet some threshold for posting a minimal version of the code. But I don't know how to simplify it. Namely, this system (of equations) requires two pchip interpolation components, so if I wanted to remove one of the max terms, (comment out mu2, res[i+m], and res[i+3m]) I would have more unknowns than equations. Moreover, I know that the code I presented has a root, and even if I found a way to simplify, it may not |
@nealockwood hmm I'm not seeing evidence of a bug here. It's not surprising that a gradient-based rootfinder is having trouble with a function that involves
How many inputs and outputs does each equation have? That is, for each equation, how many values are varied to find the root, and how many outputs need to become zero? If each of your equations is scalar-in, scalar-out, you have other options. |
@mdhaber thank you so much for quickly responding! To briefly highlight why I think it's a bug: fsolve in matlab is able to find the root pretty fast, the max terms are squared (making them smooth functions), removing the squared term actually seems to improve the performance (returning something other than the initial guess), I have unsuccessfully tried other max alternatives, , and one is able to get a non-trivial solution by treating it as a minimization problem (minimizing normed value) when given a lot of iterations. Each function has a scalar output, but the goal is to solve the system over a fixed grid. So essentially, there are 4 equations I want to be 0 over a grid of m points. To do this, I stack the residual terms in a vector. So for example, with m=51, there are 204 residual terms, or you could think of it as each grid point corresponding to 4 of the 204 residual terms. |
The documentation of Matlab's Assuming that You might try different algorithms (exposed with There is an effort to replace the Fortran code of MINPACK (#18566), which we can hope will uncover and fix bugs in the implementation. In the meantime, I don't think there is much we can do to illuminate bugs with this example. Would you agree @ilayn? |
Thanks for the detail! |
Our past and current role was/is to only facilitate/vendor these external Fortran77 libraries and hope that these are internally consistent and tested over time by actual experts. As we started looking into these old libraries a bit more critically, it is becoming more apparent that there are a lot of edge cases that such historical code do not handle and do not report back. However we are also bounded by our own expertise in all different subjects hence it would be typical that we would not catch up the current state of art in integration or optimization and so on unfortunately. But our hope is that as we convert these tools to a more readable format, more experts can look into the code and spot obvious points of improvement, low-hanging-fruit fixes etc. or get more encouraged to teach us how to do things better. Also we would be able to see what happens when we push in functions like |
I will see if I can recruit some help! |
Describe your issue.
I originally posted this on stack thinking the error might be mine but now I think it's some sort of bug.
I am trying to find the roots of multiple equations over a grid (want the equations to be 0 evaluated at the points in the grid) and am stacking all the function values as a vector. I was able to do this in Matlab,
After playing around with every aspect of the code, I discovered the max functions are giving it trouble. The max terms are squared to make them a smooth function, but when they are squared, fsolve just returns the initial guess. Ironically, when the squared term is removed, fsolve does return something besides the initial guess, but it's not the root. When the max terms (and mu terms) are completely removed, fsolve successfully finds a root.
There is also a pchip being used, which may add some complications, but the same thing is happening when I use a spline
Reproducing Code Example
Is the function and the evaluation is
Changing the tolerance didn't help. I also tried simplifying and writing like
But it changed nothing. I looked a long time for something on this, searched github for files that used this combination of functions, but to no avail.
SciPy/NumPy/Python version and system information
The text was updated successfully, but these errors were encountered: