JavaScript 方程求解器库

发布于 2024-10-09 11:57:53 字数 98 浏览 7 评论 0原文

是否有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

山人契 2024-10-16 11:57:53

我想推荐 nerdamer。它可以代数求解四次函数,并且可以数值求解一系列函数。另一个需要考虑的库是 Algebrite

//solve linear equations
var x = nerdamer.solve('(x+1)*3=x-6', 'x');
console.log(x.toString());
//quadratic
var x2 = nerdamer.solve('x^2-8x+15', 'x');
console.log(x2.toString());
//quadratic algebraically
var x3 = nerdamer.solve('x^2-ax+3*b', 'x');
console.log(x3.toString());
//multiple roots
var x4 = nerdamer.solve('x^6+41*x^5+652*x^4+5102*x^3+20581*x^2+40361*x+30030', 'x');
console.log(x4.toString());
//functions - numerically around to zero up to a predefined range
var x5 = nerdamer.solve('cos(x)^2+sin(x-1)', 'x');
console.log(x5.toString());
//solve a system of linear equations
var x6 = nerdamer.solveEquations(['2x+y=7', 'x-y+3z=11', 'y-z=-1']);
console.log(x6.toString());
//solve a system of nonlinear equations
var x7 = nerdamer.solveEquations(['3*x^2/y=2', 'z*x*y-1=35', '5*z^2+7=52']);
console.log(x7.toString());
<script src="https://cdn.jsdelivr.net/npm/nerdamer@latest/nerdamer.core.js"></script>
<script src="https://cdn.jsdelivr.net/npm/nerdamer@latest/Algebra.js"></script>
<script src="https://cdn.jsdelivr.net/npm/nerdamer@latest/Calculus.js"></script>
<script src="https://cdn.jsdelivr.net/npm/nerdamer@latest/Solve.js"></script>

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.

//solve linear equations
var x = nerdamer.solve('(x+1)*3=x-6', 'x');
console.log(x.toString());
//quadratic
var x2 = nerdamer.solve('x^2-8x+15', 'x');
console.log(x2.toString());
//quadratic algebraically
var x3 = nerdamer.solve('x^2-ax+3*b', 'x');
console.log(x3.toString());
//multiple roots
var x4 = nerdamer.solve('x^6+41*x^5+652*x^4+5102*x^3+20581*x^2+40361*x+30030', 'x');
console.log(x4.toString());
//functions - numerically around to zero up to a predefined range
var x5 = nerdamer.solve('cos(x)^2+sin(x-1)', 'x');
console.log(x5.toString());
//solve a system of linear equations
var x6 = nerdamer.solveEquations(['2x+y=7', 'x-y+3z=11', 'y-z=-1']);
console.log(x6.toString());
//solve a system of nonlinear equations
var x7 = nerdamer.solveEquations(['3*x^2/y=2', 'z*x*y-1=35', '5*z^2+7=52']);
console.log(x7.toString());
<script src="https://cdn.jsdelivr.net/npm/nerdamer@latest/nerdamer.core.js"></script>
<script src="https://cdn.jsdelivr.net/npm/nerdamer@latest/Algebra.js"></script>
<script src="https://cdn.jsdelivr.net/npm/nerdamer@latest/Calculus.js"></script>
<script src="https://cdn.jsdelivr.net/npm/nerdamer@latest/Solve.js"></script>

↙温凉少女 2024-10-16 11:57:53

您可以通过执行 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 of x with a value, evaling 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 ;)

山川志 2024-10-16 11:57:53

快速搜索会发现 algebra.jsjs 解算器。我对他们一无所知,但他们看起来很合法。 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.

早茶月光 2024-10-16 11:57:53

查看脚本 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.

第七度阳光i 2024-10-16 11:57:53

Ceres.js 可以找到 f(x) = 形式的方程组的解0. 使用 Emscripten 从 C++ 移植到 JavaScript。该文件有点大,但如果您需要真正高性能的求解器,这是您最好的选择。它以网络组装方式运行,因此速度很高。
这是一个例子:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>

<h3>Powell Function</h3>
    <p>This is an example of the solution of the powell function using Ceres.js</p>
    <pre><code id="powell_demo_code" class="language-js"></code></pre>
    <textarea id="powell_demo" rows="40" cols="110"></textarea>
    <script id="powell_demo_code_source">
        async function ceresSolvePowell() {
            const {Ceres} = await import('https://cdn.jsdelivr.net/gh/Pterodactylus/Ceres.js@latest/dist/ceres.min.js');
    
            var fn1 = function f1(x){
                return (x[0]+10*x[1]);
            }
    
            var fn2 = function f2(x){
                return (Math.sqrt(5)*(x[2]-x[3]));
            }
            
            var fn3 = function f3(x){
                return Math.pow(x[1]-2*x[2],2);
            }
            
            var fn4 = function f4(x){
                return Math.sqrt(10)*Math.pow(x[0]-x[3],2);
            }
            
            let solver = new Ceres()
            solver.addFunction(fn1) //Add the first equation to the solver.
            solver.addFunction(fn2) //Add the second equation to the solver.
            solver.addFunction(fn3) //Add the third equation to the solver.
            solver.addFunction(fn4) //Add the forth equation to the solver.
            //solver.addCallback(c1) //Add the callback to the solver.
            //solver.addLowerbound(0,1.6) //Add a lower bound to the x[0] variable
            //solver.addUpperbound(1,1.7) //Add a upper bound to the x[1] variable
            
            var x_guess = [1,2,3,4] //Guess the initial values of the solution.
            let s = await solver.solve(x_guess) //Solve the equation
            var x = s.x //assign the calculated solution array to the variable x
            document.getElementById("powell_demo").value = s.report //Print solver report
            solver.remove() //required to free the memory in C++
            
        }
        ceresSolvePowell();
    </script>
</body>
</html>

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:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>

<h3>Powell Function</h3>
    <p>This is an example of the solution of the powell function using Ceres.js</p>
    <pre><code id="powell_demo_code" class="language-js"></code></pre>
    <textarea id="powell_demo" rows="40" cols="110"></textarea>
    <script id="powell_demo_code_source">
        async function ceresSolvePowell() {
            const {Ceres} = await import('https://cdn.jsdelivr.net/gh/Pterodactylus/Ceres.js@latest/dist/ceres.min.js');
    
            var fn1 = function f1(x){
                return (x[0]+10*x[1]);
            }
    
            var fn2 = function f2(x){
                return (Math.sqrt(5)*(x[2]-x[3]));
            }
            
            var fn3 = function f3(x){
                return Math.pow(x[1]-2*x[2],2);
            }
            
            var fn4 = function f4(x){
                return Math.sqrt(10)*Math.pow(x[0]-x[3],2);
            }
            
            let solver = new Ceres()
            solver.addFunction(fn1) //Add the first equation to the solver.
            solver.addFunction(fn2) //Add the second equation to the solver.
            solver.addFunction(fn3) //Add the third equation to the solver.
            solver.addFunction(fn4) //Add the forth equation to the solver.
            //solver.addCallback(c1) //Add the callback to the solver.
            //solver.addLowerbound(0,1.6) //Add a lower bound to the x[0] variable
            //solver.addUpperbound(1,1.7) //Add a upper bound to the x[1] variable
            
            var x_guess = [1,2,3,4] //Guess the initial values of the solution.
            let s = await solver.solve(x_guess) //Solve the equation
            var x = s.x //assign the calculated solution array to the variable x
            document.getElementById("powell_demo").value = s.report //Print solver report
            solver.remove() //required to free the memory in C++
            
        }
        ceresSolvePowell();
    </script>
</body>
</html>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文