Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript equation solver library

Is there a JavaScript library or function that will solve equations for variables?

Such as 9 = 3 + x and solve for x. But it should also solve more advanced equations that include sine, cosine, and tangent.

like image 584
Will Avatar asked Dec 22 '10 22:12

Will


People also ask

Is JavaScript math a library?

Math. js is an extensive math library for JavaScript and Node.

How do I get math in JavaScript?

Math. js can be installed using various package managers like npm, or by just downloading the library from the website: https://mathjs.org/download.html.

What is math JavaScript?

Math is a built-in object that has properties and methods for mathematical constants and functions. It's not a function object. Math works with the Number type.


1 Answers

I'd like to propose nerdamer. It can algebraically solve up to quartic functions and it can numerically solve a range of functions. Another library to consider is 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>
like image 170
jiggzson Avatar answered Oct 19 '22 23:10

jiggzson