python - 使用 Python 在二分法中添加解决方案
问题描述
我正在使用 python 的二分法并尝试将答案附加到解决方案数组中。因为二分法仅适用于 1 个根。我不知道为什么我的答案没有附加。
import math
for i in range (0,3):
solutions = []
a=i
b=3
func = lambda x: math.sin(3*x) - math.cos(2*x)
def bisection(f,a,b,tol):
fa = f(a)
if abs(fa) < tol:
return a
fb = f(b)
if abs(fb) < tol:
return b
for _ in range (100):
c=(a+b)/2
fc=f(c)
if fc == tol:
break
if fa*fc>0:
a, fa = c, fc
if fa*fc<0:
b, fb = c, fc
return c
ans = bisection(func,a,b,1e-10)
solutions.append(ans)
print("The solution of the equation: ",solutions)
解决方案
推荐阅读
- windows - 用于发现不正确的预处理器指令的 Powershell 脚本
- discord.py - 有没有办法为 discord.py 中的命令设置默认冷却时间
- wordpress - 检查 WooCommerce 订单中是否至少一项具有特定属性项
- date - Google 表格 - IF 声明 - 空日期 (1/1/2500) 解决方法
- r - tibble 的交叉表变量并使输出可读
- android - 按钮错误地用drawable内部包装内容
- google-analytics - 我可以复制 GA4 属性吗?
- java - 以编程方式添加新的约束映射 HV000121:无法解析约束映射文件
- java - 在 Flutter 中实现 Firebase 原生广告
- heroku - Heroku PS:Exec with ENV Vars