如何使用javascript解决代数方程

san*_*ans 3 javascript jquery algebra

我有很多代数方程

这是方程

那么如何使用javascript解决这个问题。

我需要这个方程的答案。你有任何想法来解决这个问题或任何插件。

Man*_*ath 6

假设您有一个代数方程:x² ? 7x + 12 = 0.

然后,您可以创建一个函数,如下所示:

function f(x) {
    var y = x*x - 7*x + 12;
    return y;
}
Run Code Online (Sandbox Code Playgroud)

然后应用数值方法:

var min=-100.0, max=100.0, step=0.1, diff=0.01;
var x = min;
do {
    y = f(x);
    if(Math.abs(y)<=diff) {
        console.log("x = " + Math.round(x, 2));
        // not breaking here as there might be multiple roots
    }
    x+=step;
} while(x <= max);
Run Code Online (Sandbox Code Playgroud)

上面的代码扫描在该范围二次方程的根[-100, 100]0.1作为步骤。

该方程也可以作为用户输入(通过f使用eval函数构造函数)。

您还可以使用 Newton-Raphson 或其他更快的方法在 JavaScript 中求解代数方程。