JavaScript/jQuery equation solver
Is there a plugin that can solve a simple system of equations?
I bas开发者_如何学JAVAically need the ability to solve a 3X3 matrix.
Sylvester is a JavaScript library designed to let you do mathematics with vectors and matrices.
[Ceres.js]: https://github.com/Pterodactylus/Ceres.js is another tool for numerically solving multiple non-linear equations in JavaScript.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script src="https://www.desmos.com/api/v1.4/calculator.js?apiKey=dcb31709b452b1cf9dc26972add0fda6"></script>
</head>
<body>
<h2>Quadratic Function</h2>
<p>This is an example of the solution of the quadratic function using <a href="https://github.com/Pterodactylus/Ceres.js">Ceres.js</a></p>
<div id="calculator" style="width: 600px; height: 400px;"></div>
<script>
var elt = document.getElementById('calculator');
var calculator = Desmos.GraphingCalculator(elt);
calculator.setExpression({ id: 'exp1', latex: 'x+10*y-20 = 0' });
calculator.setExpression({ id: 'exp2', latex: "\\sqrt{5}*x-y^2 = 0" });
</script>
<p>
<textarea id="demo" rows="40" cols="170">
</textarea>
</p>
<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]+10*x[1]-20); //this equation is of the for f1(x) = 0
}
var fn2 = function f2(x){
return (Math.sqrt(5)*x[0]-Math.pow(x[1], 2)); //this equation is of the for f2(x) = 0
}
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_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] //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>
精彩评论