Python:查找非线性方程的多个根 [英] Python: Finding multiple roots of nonlinear equation

查看:192
本文介绍了Python:查找非线性方程的多个根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

承担以下功能:

f(x) = x * cos(x-4)

使用x = [-2.5, 2.5]时,此功能在f(0) = 0f(-0.71238898) = 0处交叉0.

With x = [-2.5, 2.5] this function crosses 0 at f(0) = 0 and f(-0.71238898) = 0.

这是通过以下代码确定的:

This was determined with the following code:

import math
from scipy.optimize import fsolve
def func(x):
    return x*math.cos(x-4)
x0 = fsolve(func, 0.0)
# returns [0.]
x0 = fsolve(func, -0.75)
# returns [-0.71238898]

使用fzero(或任何其他Python根查找器)在一次调用中找到两个根的正确方法是什么?有其他的scipy函数可以做到这一点吗?

What is the proper way to use fzero (or any other Python root finder) to find both roots in one call? Is there a different scipy function that does this?

fzero参考

推荐答案

定义函数,使其可以使用标量或numpy数组作为参数:

Define your function so that it can take either a scalar or a numpy array as an argument:

>>> import numpy as np
>>> f = lambda x : x * np.cos(x-4)

然后将参数向量传递给fsolve.

Then pass a vector of arguments to fsolve.

>>> x = np.array([0.0, -0.75])
>>> fsolve(f,x)
array([ 0.        , -0.71238898])

这篇关于Python:查找非线性方程的多个根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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