18

是否有 JavaScript 库或函数可以求解变量方程?

9 = 3 + x并求解 x。但它还应该解决更高级的方程,包括正弦、余弦和正切。

4

5 回答 5

12

我想提议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>

于 2018-07-25T21:53:07.007 回答
11

您可以通过执行 excel 所谓的“目标搜索”来近似x解 - 测试值直到等式两边大致匹配。您可以通过将等式除以"="符号、将每次出现xeval虽然相对简单,但这种方法存在缺陷(除了它是一个近似值之外),例如,算法可能认为两侧正在收敛,而实际上它只是局部最小值/最大值,并且在差值刚好低于您的阈值。您还需要测试多个起点来求解具有多个解的方程。

对于一个像人类一样实际求解方程的程序(通过重新排列方程的两侧并应用反函数、导数/积分等)要复杂得多,并且不知何故感觉完全是专有的;)

于 2010-12-22T23:47:09.213 回答
4

快速搜索出现algebra.jsjs-solver。我对他们一无所知,但他们似乎是合法的。algebra.js 有一个不错的 OOP API,但似乎不能处理三角函数。

于 2015-10-04T12:41:40.920 回答
2

查看牛顿方法程序中 f(x)=0的脚本。它使用牛顿切线法求解方程。

于 2010-12-23T10:40:03.000 回答
0

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>

<h2>Powell Function</h2>
<p>This is an example of the solution of the powell function using Ceres.js</p>

<textarea id="demo" rows="40" cols="170">
</textarea>

<script type="module">
    import {Ceres} from 'https://cdn.jsdelivr.net/gh/Pterodactylus/Ceres.js@master/Ceres-v1.5.3.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.add_function(fn1) //Add the first equation to the solver.
    solver.add_function(fn2) //Add the second equation to the solver.
    solver.add_function(fn3) //Add the third equation to the solver.
    solver.add_function(fn4) //Add the forth equation to the solver.
    //solver.add_callback(c1) //Add the callback to the solver.
    //solver.add_lowerbound(0,1.6) //Add a lower bound to the x[0] variable
    //solver.add_upperbound(1,1.7) //Add a upper bound to the x[1] variable
    
    solver.promise.then(function(result) { 
        var x_guess = [1,2,3,4] //Guess the initial values of the solution.
        var s = solver.solve(x_guess) //Solve the equation
        var x = s.x //assign the calculated solution array to the variable x
        document.getElementById("demo").value = s.report //Print solver report
        solver.remove() //required to free the memory in C++
    })
</script>
</body>
</html>

于 2020-04-09T03:06:05.107 回答