python - 在 Python 中查找函数 x = a*sin(x) 的所有根
问题描述
我必须根据参数 a 找到解决方案的数量。在使用 scipy.optimize.root 数值求解方程时,我得到一些不是函数根的数字。例如对于 x = 7*sin(x),我得到数字 -7.71046524 和 7.71046524。我的代码是:
a = np.linspace(-5, 5)
def fun(x):
return x - b*np.sin(x)
for i in a:
solutions = []
b = i
c = abs(int(round(i)))
for j in range(-c, c+1):
y = root(fun, j)
if (round(y.x[0], 3) not in solutions):
solutions.append(round(y.x[0], 3))
print(len(solutions))
解决方案
如果使用scipy.optimize.root
,则返回值包含x
解决方案数组和布尔success
标志。您需要过滤掉任何结果 where success
is False
。
import numpy as np
from scipy.optimize import root
a = np.linspace(-7, 7)
def fun(x):
return x - b*np.sin(x)
for i in a:
solutions = []
b = i
c = abs(int(round(i)))
for j in range(-c, c+1):
y = root(fun, j)
if y.success and (round(y.x[0], 6) not in solutions):
solutions.append(round(y.x[0], 3))
print(i, solutions)
推荐阅读
- smartgit - Smartgit:无法存储
- python - 上传后如何通过 URL 访问媒体
- javascript - Vue vue.config.js 代理 404
- python - 从 Python 调用 CustomVision 预测 API - 第二次不是魅力
- acumatica - 为什么我无法在我的交叉引用 (INItemXRef) 表中获得“当前”记录?
- discord - 是否有可能让 awaitReactions 给每个做出反应的人一个角色?
- laravel - 带有“LIKE”语句的雄辩查询在 Laravel 6 中不起作用
- laravel - Laravel 419 页面过期,同时通过刀片中的表单发布请求
- python - 有没有办法在保留章节元数据的同时下载视频?
- three.js - 如何相对于向量旋转笛卡尔坐标?