1

I have a polynomial equation which needs to be solved programmatically. The equation is as below.

x/207000 + (x/1349)^(1/0.282) = (260)^2/(207000*x)

I need to solve for x. I am using javascript for this. Is there any library in javascript which can solve mathematical equation?

Amr
  • 81
  • 3
  • 15
  • `x/207000 + (x/1349)^(1/0.282) = (260)^2/(207000*x)` is not a polynomial equation because `(260)^2/(207000*x)` is not a polynomial. – Anderson Green Aug 10 '19 at 17:55

3 Answers3

3

Some computer algebra systems in JavaScript can solve systems of polynomial equations. Using Algebrite, you can solve an equation like this one:

roots(x^2 + 2x = 4)

The roots of this equation are [-1-5^(1/2),-1+5^(1/2)].

It's also possible to solve systems of polynomial equations using Nerdamer. Its documentation includes this example:

var sol = nerdamer.solveEquations('x^3+8=x^2+6','x');
console.log(sol.toString());
//1+i,-i+1,-1
Anderson Green
  • 30,230
  • 67
  • 195
  • 328
2

There are JavaScript solvers out there that will numerically solve your problem. The non-linear solver that I know of is Ceres.js. Here is an example adapted to your question. The first thing we have to do is rearrange to the form F(x)=0.

x0 = 184.99976046503917

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

<h2>User Function</h2>
<p>This is an example of the solution of a user supplied 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]/207000 + Math.pow((x[0]/1349),(1/0.282)) - Math.pow((260),2)/(207000*x[0]);
    }
    
    let solver = new Ceres()
    solver.add_function(fn1) //Add the first equation to the solver.
    
    solver.promise.then(function(result) { 
        var x_guess = [1] //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>
1

Try math.js

http://mathjs.org/

It allows you to do mathematical functions in a much cleaner look.

lakeIn231
  • 1,177
  • 3
  • 14
  • 34
  • It can solve linear systems using [`usolve`](https://mathjs.org/docs/reference/functions/usolve.html), but I don't see a solver for polynomial equations. – Anderson Green Aug 13 '19 at 14:42
  • @AndersonGreen How about https://algebra.js.org/ then? Although given the question is over two years old it's probably too late. – Dave Newton Aug 13 '19 at 14:51