3

I have lot of algebra equations

this is equation

so how to solve this using javascript.

I need answer for this equation. you have any idea to solve this or any plugin for that.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • 8
    Please post the code you've tried. StackOverflow is not designed to do your homework for you. – Rory McCrossan Sep 19 '13 at 11:54
  • 2
    You can take the natural log `ln(x)` of 512, divide by 3, then use `E` (euler's number) and `pow(x,y)` to get the result... – nonsensickle Sep 19 '13 at 11:55
  • Already asked: http://stackoverflow.com/questions/12810765/calculating-cubic-root-for-negative-number – D_R Sep 19 '13 at 12:00
  • Have you thought about how you would represent the equation in JS variables in a way that could be input to a function that "solves" it? E.g., will it always be trying to calculate the nth root of a number such that you could use `n = -512` and `r = 3` as input, or...? – nnnnnn Sep 19 '13 at 12:07
  • You can "solve" arithmetic expressions such as above by evaluating as `-Math.pow(512.0,1/3.0)` (note that `Math.pow(-512.0,1/3.0)` will return `NaN`). But if you really mean to solve algebraic equations, see my answers below. – Manu Manjunath Sep 19 '13 at 12:10
  • Eval? You can use it if you want, but it is not recommended by most people... – Stardust Jan 10 '16 at 01:36

1 Answers1

6

Let's say you have an algebraic equation: x² − 7x + 12 = 0.

Then, you can create a function as below:

function f(x) {
    var y = x*x - 7*x + 12;
    return y;
}

and then apply numerical methods:

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);

Above code scans for roots of that quadratic equation in range [-100, 100] with 0.1 as a step.

The equation can also taken as user input (by constructing function f using eval function).

You can also use Newton-Raphson or other faster methods to solve algebraic equations in JavaScript.

Manu Manjunath
  • 6,201
  • 3
  • 32
  • 31
  • 1
    There are also several [computer algebra systems in JavaScript](https://stackoverflow.com/questions/2753489/javascript-computer-algebra-system) that can solve polynomial equations. – Anderson Green Jan 14 '21 at 20:19