# [SciPy-user] Solving an equation using scipy.optimize.newton

Anne Archibald peridot.faceted@gmail....
Wed Sep 5 21:21:50 CDT 2007

On 05/09/07, fdu.xiaojf@gmail.com <fdu.xiaojf@gmail.com> wrote:

> I'm trying to solve an equation f(x) = 0 with scipy.optimize.newton.
>
> However the problem isn't so simple. There are bound constraints for my
> equation: the equation cannot be evaluated when x is out of [Min, Max], but
> the root is always in the interval of [Min, Max]
>
> When newton() iterates to find a root, it sometimes try to evaluate the
> equation with a x out of [Min, Max], and then error occurs.
>
> How to solve this problem ?
>
> I couldn't easily find two points with different signs every time, so methods
> like brentq don't work here.

Are you sure your function has a zero at all? If it's something like a
polynomial, you may find that sometimes it fails to have a root, which
will of course be a problem for a root-finding algorithm.

It's probably a good idea to look at this as two problems:
* Find points of opposite sign in your interval.
* Narrow this down to an actual root.

Once you've done the first, the second can be done using (say) brentq
without worrying that you're going to leave the interval of interest.

So how do you find a place where your function crosses the y-axis?
Ideally you'd know something about it analytically. But it sounds like
you've tried that, to no avail.

You could blindly evaluate the function, perhaps on a grid, a
pseudorandom or subrandom sequence of points, hoping to find one that
gave a negative value.

You could run a one-dimensional minimizer, with a wrapper around your
function that raises an exception as soon as it sees a negative value,
but here too to get started you need three points where the middle one
is the lowest.

If you're really stuck, you can try one of the constrained
multidimensional minimizers (but be warned some of them evaluate at
points that violate the constraints!), again with the
exception-raising trick to bail out as soon as you've found a point
with a negative value.

Anne M. Archibald

More information about the SciPy-user mailing list