是否有一个JavaScript库或函数可以解决变量的方程式?
例如9 = 3 + x
并解决x。但它也应该解决更高级的方程,包括正弦,余弦和正切。
答案 0 :(得分:5)
您可以通过执行excel调用“目标搜索”来近似解决方案 - 测试x
的值,直到等式的两边大致匹配。您可以通过"="
符号拆分等式,用值x
替换eval
的每个出现,并确定差异是否低于某个阈值来执行此操作。虽然相对简单,但这种方法存在缺陷(除了它是近似的事实),例如算法可能认为双方正在收敛,而实际上它只是一个局部最小值/最大值并且会在之后发散。差异刚好低于你的门槛。您还需要测试多个起点以解决具有多个解决方案的方程式。
对于一个实际求解方程式的程序(通过重新排列方程的两边并应用反函数,导数/积分等等)要复杂得多,并且不知怎的感觉完全专有;)
答案 1 :(得分:4)
我想提出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());
<script src="https://cdn.jsdelivr.net/npm/nerdamer@0.7.16/nerdamer.core.js"></script>
<script src="https://cdn.jsdelivr.net/npm/nerdamer@0.7.16/Algebra.js"></script>
<script src="https://cdn.jsdelivr.net/npm/nerdamer@0.7.16/Calculus.js"></script>
<script src="https://cdn.jsdelivr.net/npm/nerdamer@0.7.16/Solve.js"></script>
答案 2 :(得分:3)
快速搜索会显示algebra.js和js-solver。我对他们一无所知,但他们看似合法。 algebra.js有一个很好的OOP API,但似乎不能处理三角函数。
答案 3 :(得分:1)
查看 Newton's Method Program for f(x)=0 上的脚本。它使用Newton's tangent method来解决方程式。
答案 4 :(得分:0)
Ceres.js可以找到形式为f(x)= 0的方程式数组的解。它使用Emscripten从C ++移植到JavaScript。该文件有点大,但是如果您需要真正高性能的求解器,那么这是您的最佳选择。它以网络组装方式运行,因此速度很高。 这是一个示例:
<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.4.13.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>