Home >Web Front-end >JS Tutorial >Example tutorial on solving linear equations of two variables based on Algebra.js

Example tutorial on solving linear equations of two variables based on Algebra.js

零下一度
零下一度Original
2017-06-17 17:20:003214browse

This article mainly introduces the function of solving linear equations of two variables based on the algebraic equation library Algebra.js, and analyzes the specific usage skills of the equation library Algebra.js to calculate equations based on specific examples. Friends who need it can refer to it

The example in this article describes the function of solving linear equations of two variables based on the algebraic equation library Algebra.js. Share it with everyone for your reference, the details are as follows:

Assume that the linear equation of two variables is as follows:


x + y = 11
x - y = 5

The solution of the equation is as follows:


<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <title>测试algebra函数库(解两元一次方程)</title>
  <script src="js/algebra.min.js"></script>
  <script type="text/javascript">
    var Fraction = algebra.Fraction;
    var Expression = algebra.Expression;
    var Equation = algebra.Equation;
    //=======================================
    var x1 = algebra.parse("x+y=11");
    var answer1 = x1.solveFor("y");
    //console.log("y = " + answer1.toString());
    var x2 = algebra.parse("x-y=5");
    var answer2 = x2.solveFor("y");
    //console.log("y = " + answer2.toString());
    //=================================================
    //解出X值
    var eq = new Equation(answer1, answer2);
    console.log("x表达式:" + eq.toString());
    var answerX = eq.solveFor("x");
    console.log("解出X的值为:" + answerX.toString());
    //=================================================
    //解出Y值(把x的值代入x1或x2)
    eq = x1.eval({
      x: answerX
    });
    console.log("y表达式:" + eq.toString());
    var answerY = eq.solveFor("y");
    console.log("解出Y的值为:" + answerY.toString());
  </script>
</head>
<body>
</body>
</html>

Console output:

x表达式:-x + 11 = x - 5
解出X的值为:8
y表达式:y + 8 = 11
解出Y的值为:3

The code is written very roughly without any packaging. Just understand it.

The above is the detailed content of Example tutorial on solving linear equations of two variables based on Algebra.js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn