JavaScript 方程求解器库
是否有 JavaScript 库或函数可以求解变量方程?
例如9 = 3 + x
并求解x。但它还应该求解更高级的方程,包括正弦、余弦和正切。
Is there a JavaScript library or function that will solve equations for variables?
Such as 9 = 3 + x
and solve for x. But it should also solve more advanced equations that include sine, cosine, and tangent.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我想推荐 nerdamer。它可以代数求解四次函数,并且可以数值求解一系列函数。另一个需要考虑的库是 Algebrite。
I'd like to propose nerdamer. It can algebraically solve up to quartic functions and it can numerically solve a range of functions. Another library to consider is Algebrite.
您可以通过执行 Excel 所谓的“目标寻求”来近似解决方案 - 测试
x
的值,直到等式两边近似匹配。您可以通过用"="
符号拆分方程,将每次出现的x
替换为一个值,对两边进行eval
来实现此目的,并确定差异是否低于某个阈值。虽然相对简单,但这种方法存在缺陷(除了它是近似值这一事实之外),例如,算法可能认为两侧正在收敛,而实际上它只是局部最小值/最大值,并且在差异正好低于您的阈值。您还需要测试多个起点来求解具有多个解的方程。对于一个像人类一样求解方程的程序来说(通过重新排列方程的两侧并应用反函数、导数/积分等等)要复杂得多,并且不知何故感觉完全是专有的; )
You can approximate the solution by doing what excel calls "Goal Seek" - testing values for
x
until both sides of the equation approximately match. You can do this by splitting the equation by the"="
sign, replacing each occurence ofx
with a value,eval
ing both sides, and determining if the difference falls below a certain threshold. While relatively simple, there are flaws to this method though (other than the fact that it is an approximation), for example the algorithm may think the two sides are converging when in fact it is just a local min/max and will diverge after the difference falls just below your threshold. You'll also need to test multiple start points to solve equations with more than one solution.For a program to actually solve an equation as a human would (by rearranging the two sides of the equation and applying inverse functions, derivatives/integrals and whatnot) is far more complex, and somehow feels entirely proprietary ;)
快速搜索会发现 algebra.js 和 js 解算器。我对他们一无所知,但他们看起来很合法。 algebra.js 有一个很好的 OOP API,但似乎无法处理三角函数。
A quick search turns up algebra.js and js-solver. I don't know anything about them, but they seem legit. algebra.js has a nice OOP API, but doesn't appear to handle trigonometric functions.
查看脚本 f(x)= 的牛顿方法程序0。它使用牛顿切线法求解方程。
Look at the script at Newton's Method Program for f(x)=0. It solves the equation using Newton's tangent method.
Ceres.js 可以找到 f(x) = 形式的方程组的解0. 使用 Emscripten 从 C++ 移植到 JavaScript。该文件有点大,但如果您需要真正高性能的求解器,这是您最好的选择。它以网络组装方式运行,因此速度很高。
这是一个例子:
Ceres.js can find the solution to an array of equations of the form f(x) = 0. It is ported from C++ to JavaScript with Emscripten. The file is a bit large but if you need a really high performance solver this is your best bet. It runs in web-assembly so the speed is high.
Here is an example: