JS求解三元一次方程组值的方法

作者:lilili123 时间:2024-05-02 17:39:00 

本文实例讲述了JS求解三元一次方程组值的方法。分享给大家供大家参考,具体如下:


// 求用js 码一段代码求 三元一次方程组的值!
// a1 = y * b1 + z * c1 - d1 * x; (1)
// a2 = y * b2 + z * c2 - d2 * x; (2)
// a3 = y * b3 + z * c3 - d3 * x; (3)
// (1) - (2)
// a1 - a2 = y * (c1 - c2) + z * (d1 - d2)  (4)
// (1) - (3)
// a1 - a3 = y * (c1 - c3) + z * (d1 - d3)  (5)
// (4) * (c1 - c3)/(c1 - c2)
// (a1 - a2) * ((c1 - c3)/(c1 - c2)) = y * (c1 - c3) + z * (d1 - d2) * ((c1 - c3)/(c1 - c2));  (6)
// (6) - (5)
// (a1 - a2) * ((c1 - c3)/(c1 - c2)) - (a1 - a3) = z * ((d1 - d2) * ((c1 - c3)/(c1 - c2)) - (d1 - d3));  (7)
//
//==================================================================================================
// (1) * (d2/d1)
// ( a1 * (d2/d1) ) = y * b1 * (d2/d1) + z * c1 * (d2/d1) - d1 * x * (d2/d1)  (4)
// ( a1 * (d2/d1) ) = y * b1 * (d2/d1) + z * c1 * (d2/d1) - d2 * x       (4.1)
// (4.1) - (2)
// ( ( a1 * (d2/d1) ) - a2 ) = y * ( (b1 * (d2/d1)) - b2 ) + z * ( (c1 * (d2/d1)) - c2 );  (5)
// (1) * (d3/d1)
// ( a1 * (d3/d1) ) = y * ( b1 * (d3/d1) ) + z * ( c1 * (d3/d1) ) - d1 * x * (d3/d1)  (6)
// ( a1 * (d3/d1) ) = y * ( b1 * (d3/d1) ) + z * ( c1 * (d3/d1) ) - d3 * x       (6.1)
// (6.1) - (3)
// ( ( a1 * (d3/d1) ) - a3 ) = y * ( ( b1 * (d3/d1) ) - b3 ) + z * ( ( c1 * (d3/d1) ) - c3 )  (7)
// (5) * ( ( (b1 * (d3/d1)) - b3 ) / ( ( b1 * (d2/d1) ) - b2 ) )
// ( ( ( a1 * (d2/d1) ) - a2 ) * ( ( (b1 * (d3/d1)) - b3 ) / ( ( b1 * (d2/d1) ) - b2 ) ) ) = y * ( (b1 * (d3/d1)) - b3 ) + z * ( ( (c1 * (d2/d1)) - c2 ) * ( ( (b1 * (d3/d1)) - b3 ) / ( ( b1 * (d2/d1) ) - b2 ) ) )  (8)
// (8) - (7)
// ( ( ( ( a1 * (d2/d1) ) - a2 ) * ( ( (b1 * (d3/d1)) - b3 ) / ( ( b1 * (d2/d1) ) - b2 ) ) ) - ( ( a1 * (d3/d1) ) - a3 ) )
// =
// z * ( ( ( (c1 * (d2/d1)) - c2 ) * ( ( (b1 * (d3/d1)) - b3 ) / ( ( b1 * (d2/d1) ) - b2 ) ) ) - ( ( c1 * (d3/d1) ) - c3 ) )
//var a1 = 41778.58102, a2 = 60925.87223, a3 = 88229.03849,
//b1 = 72931.77839, b2 = 61296.01212, b3 = 67138.30916,
//c1 = 120.1318412, c2 = 120.1308286, c3 = 120.1573771,
//d1 = 30.27345376, d2 = 30.30174637, d3 = 30.32151101;
//var a1 = 1, a2 = -9, a3 = -3,
//b1 = -1, b2 = 3, b3 = -3,
//c1 = 1, c2 = -2, c3 = -5,
//d1 = -2, d2 = -1, d3 = -4;
var a1 = 11, a2 = 0, a3 = -2,
b1 = 3, b2 = 1, b3 = -1,
c1 = 1, c2 = 1, c3 = -1,
d1 = -2, d2 = -1, d3 = -3;
var equations = [
 "a1 = y * b1 + z * c1 - d1 * x",
 "a2 = y * b2 + z * c2 - d2 * x",
 "a3 = y * b3 + z * c3 - d3 * x"
];
for (var i = 0; i < equations.length; i++) {
 equations[i] = equations[i].replace("a1", "(" + a1 + ")").replace("b1", "(" + b1 + ")").replace("c1", "(" + c1 + ")").replace("d1", "(" + d1 + ")")
 .replace("a2", "(" + a2 + ")").replace("b2", "(" + b2 + ")").replace("c2", "(" + c2 + ")").replace("d2", "(" + d2 + ")")
 .replace("a3", "(" + a3 + ")").replace("b3", "(" + b3 + ")").replace("c3", "(" + c3 + ")").replace("d3", "(" + d3 + ")");
}
console.log(equations);
// 计算三元一次方程
// 确保常量和未知数分开在2边,逻辑安装这样处理的
var LEquat = {
 formatFloat: function(f, digit) {
   var m = Math.pow(10, digit);
   return parseInt(f * m, 10) / m;
 },
 // 是否常量一边, 返回0常量的一边索引,1变量一边的索引,返回一个数组
 separateSide: function(equations) {
   var arr = [], equa = equations.split("=");
   if (equa[0].search(/x|y|z/) == -1) {
     arr = equa.slice(0);
   } else {
     arr[0] = equa[1];
     arr[1] = equa[0];
   }
   return arr;
 },
 // 分隔变量的一边,把x,y,z分隔
 // x 是索引0,y是索引1,z是索引2
 separateVariableSide: function(variableSide) {
   var variables = variableSide.split(/\s+(\+|\-)\s+/);
   var arr = [];
   for (var i = 0; i < variables.length; i++) {
     if (variables[i].search(/x/) != -1) {
       arr[0] = variables[i];
     } else if (variables[i].search(/y/) != -1) {
       arr[1] = variables[i];
     } else if (variables[i].search(/z/) != -1) {
       arr[2] = variables[i];
     }
   }
   return arr;
 },
 // 取出方程里面变量一边对应x,y,z的常量系数
 // 0: x的系数,1:y的系数,2:z的系数
 getCoefficient: function(variableSideArray) {
   var me = LEquat;
   var arr = [];
   for (var i = 0; i < variableSideArray.length; i++) {
     var coeffs = variableSideArray[i].split("*");
     if (coeffs[0].search(/x|y|z/) == -1) {
       arr[i] = coeffs[0];
     } else {
       arr[i] = coeffs[1];
     }
   }
   return arr;
 },
 step: function() {
   var me = LEquat;
   // 分隔第一个方程
   var equation1 = me.separateSide(equations[0]);
   // 分隔第二个方程
   var equation2 = me.separateSide(equations[1]);
   // 分隔第三个方程
   var equation3 = me.separateSide(equations[2]);
   // 第一个方程的常量一边
   var a1 = equation1[0];
   // 第一个方程x系数
   var equa1 = me.getCoefficient(me.separateVariableSide(equation1[1]));
   // y的系数     z的系数     x的系数
   var b1 = equa1[1], c1 = equa1[2], d1 = equa1[0];
   // 第二个方程的常量一边
   var a2 = equation2[0];
   // 第二个方程变量一边系数
   var equa2 = me.getCoefficient(me.separateVariableSide(equation2[1]));
   var b2 = equa2[1], c2 = equa2[2], d2 = equa2[0];
   // 第三个方程的常量一边
   var a3 = equation3[0];
   // 第三个方程变量一边系数
   var equa3 = me.getCoefficient(me.separateVariableSide(equation3[1]));
   var b3 = equa3[1], c3 = equa3[2], d3 = equa3[0];
   // ( a1 * (d2/d1) )
   var left4_1 = " ( " + a1 + " * ( " + d2 + " / " + d1 + " ) ) ";
   // (4.1) 右边
   var right4_1 = " y " + " * " + b1 + " * ( " + d2 + " / " + d1 + " ) + z * " + c1 + " * ( " + d2 + " / " + d1 + " ) - " + d2 + " * x ";
   // 消x (4.1) - (2)
   // ( ( a1 * (d2/d1) ) - a2 ) = y * ( (b1 * (d2/d1)) - b2 ) + z * ( (c1 * (d2/d1)) - c2 );  (5)
   // (5) 中常量的一边
   var left5_1 = " ( " + left4_1 + " - " + a2 + " ) ";
   // (5) 中变量一边
   var right5_1 = " y * ( ( " + b1 + " * ( " + d2 + " / " + d1 + " ) ) - " + b2 + " ) + z * ( ( " + c1 + " * ( " + d2 + " / " + d1 + " ) ) - " + c2 + " ) ";
   // (1) * (d3/d1)
   // ( a1 * (d3/d1) ) = y * ( b1 * (d3/d1) ) + z * ( c1 * (d3/d1) ) - d1 * x * (d3/d1)  (6)
   // ( a1 * (d3/d1) ) = y * ( b1 * (d3/d1) ) + z * ( c1 * (d3/d1) ) - d3 * x       (6.1)
   var left6_1 = " ( " + a1 + " * ( " + d3 + " / " + d1 + " ) ) ";
   var right6_1 = " y * ( " + b1 + " * ( " + d3 + " / " + d1 + " ) ) + z * ( " + c1 + " * ( " + d3 + " / " + d1 + " ) ) - " + d3 + " * x ";
   // (6.1) - (3)
   // ( ( a1 * (d3/d1) ) - a3 ) = y * ( ( b1 * (d3/d1) ) - b3 ) + z * ( ( c1 * (d3/d1) ) - c3 )  (7)
   var left7_1 = " ( " + left6_1 + " - " + a3 + " ) ";
   var right7_1 = " y * ( ( " + b1 + " * ( " + d3 + " / " + d1 + " ) ) - " + b3 + " ) + z * ( ( " + c1 + " * ( " + d3 + " / " + d1 + " ) ) - " + c3 + " ) ";
   // (5) * ( ( (b1 * (d3/d1)) - b3 ) / ( ( b1 * (d2/d1) ) - b2 ) )
   // ( ( ( a1 * (d2/d1) ) - a2 ) * ( ( (b1 * (d3/d1)) - b3 ) / ( ( b1 * (d2/d1) ) - b2 ) ) )
   // = y * ( (b1 * (d3/d1)) - b3 ) + z * ( ( (c1 * (d2/d1)) - c2 ) * ( ( (b1 * (d3/d1)) - b3 ) / ( ( b1 * (d2/d1) ) - b2 ) ) )  (8)
   var left8_1 = " ( " + left5_1 + " * ( ( ( " + b1 + " * ( " + d3 + " / " + d1 + " ) ) - " + b3 + " ) / ( ( " + b1 + " * ( " + d2 + " / " + d1 + " ) ) - " + b2 + " ) ) ) ";
   var right8_1 = " y * ( ( " + b1 + " * ( " + d3 + " / " + d1 + " ) ) - " + b3 + " ) + z * ( ( ( " + c1 + " * ( " + d2 + " / " + d1 + " ) ) - " + c2 + " ) * ( ( ( " + b1 + " * ( " + d3 + " / " + d1 + " ) ) - " + b3 + " ) / ( ( " + b1 + " * ( " + d2 + " / " + d1 + " ) ) - " + b2 + " ) ) ) ";
   // (8) - (7)
   // ( ( ( ( a1 * (d2/d1) ) - a2 ) * ( ( (b1 * (d3/d1)) - b3 ) / ( ( b1 * (d2/d1) ) - b2 ) ) ) - ( ( a1 * (d3/d1) ) - a3 ) )
   // =
   // z * ( ( ( (c1 * (d2/d1)) - c2 ) * ( ( (b1 * (d3/d1)) - b3 ) / ( ( b1 * (d2/d1) ) - b2 ) ) ) - ( ( c1 * (d3/d1) ) - c3 ) )  (9)
   var left9_1 = " ( " + left8_1 + " - " + left7_1 + " ) ";
   var right9_1 = " z * ( ( ( ( " + c1 + " * ( " + d2 + " / " + d1 + " ) ) - " + c2 + " ) * ( ( ( " + b1 + " * ( " + d3 + " / " + d1 + " ) ) - " + b3 + " ) / ( ( " + b1 + " * ( " + d2 + " / " + d1 + " ) ) - " + b2 + " ) ) ) - ( ( " + c1 + " * ( " + d3 + " / " + d1 + " ) ) - " + c3 + " ) ) ";
   var z = left9_1 + " / " + " ( ( ( ( " + c1 + " * ( " + d2 + " / " + d1 + " ) ) - " + c2 + " ) * ( ( ( " + b1 + " * ( " + d3 + " / " + d1 + " ) ) - " + b3 + " ) / ( ( " + b1 + " * ( " + d2 + " / " + d1 + " ) ) - " + b2 + " ) ) ) - ( ( " + c1 + " * ( " + d3 + " / " + d1 + " ) ) - " + c3 + " ) ) ";
   // ( ( a1 * (d2/d1) ) - a2 ) = y * ( (b1 * (d2/d1)) - b2 ) + z * ( (c1 * (d2/d1)) - c2 );
   var y = " ( " + left5_1 + " - " + " z * ( ( " + c1 + " * ( " + d2 + " / " + d1 + " ) ) - " + c2 + " ) ) " + " / " + " ( ( " + b1 + " * ( " + d2 + " / " + d1 + " ) ) - " + b2 + " ) ";
   // 把z代入到(5)中
   y = y.replace("z", z);
   // 把y,z代入方程1
   // a1 = y * b1 + z * c1 - d1 * x
   var x = " ( y * " + b1 + " + z * " + c1 + " - " + a1 + " ) / " + d1;
   x = x.replace("z", z).replace("y", y);
   x = me.formatFloat(eval(x), 2);
   y = me.formatFloat(eval(y), 2);
   z = me.formatFloat(eval(z), 2);
   return {x: x,y: y,z: z};
 }
};
console.log("-----------计算结果--------------");
console.log(LEquat.step());

希望本文所述对大家JavaScript程序设计有所帮助。

标签:JS,求解,方程
0
投稿

猜你喜欢

  • 简单易懂的python环境安装教程

    2023-05-21 14:39:16
  • python turtle绘图命令及案例

    2022-04-29 10:26:58
  • 如何进行MySQL数据库表的故障检测

    2009-02-10 10:34:00
  • 什么是XSLT,什么是XPath

    2008-01-21 13:12:00
  • nodejs前端自动化构建环境的搭建

    2024-05-08 09:36:48
  • 从一个项目中来看三层架构

    2008-08-06 12:50:00
  • python双向链表实现实例代码

    2023-05-16 09:38:54
  • phpstudy无法启动MySQL服务的完美解决办法

    2024-05-22 10:08:59
  • Atlas—微软的Ajax工具包

    2007-09-07 09:52:00
  • Python3使用xml.dom.minidom和xml.etree模块儿解析xml文件封装函数的方法

    2023-12-19 22:42:41
  • MySQL数据备份方法的选择与思考

    2024-01-19 21:02:50
  • mysql执行计划介绍

    2024-01-29 10:32:36
  • Python实现的合并两个有序数组算法示例

    2021-07-11 10:44:10
  • 如何把IP表存到SQL数据库里去?

    2009-11-02 20:21:00
  • PHP连接MySQL数据库的三种方式实例分析【mysql、mysqli、pdo】

    2023-11-15 01:31:13
  • BootStrap的alert提示框的关闭后再显示怎么解决

    2024-04-28 09:50:24
  • Python实现异步IO的示例

    2021-01-11 13:25:38
  • Python 代码实现各种酷炫功能

    2022-12-24 10:40:16
  • 解决Python传递中文参数的问题

    2021-04-10 09:00:07
  • Django 内置权限扩展案例详解

    2022-08-21 23:25:45
  • asp之家 网络编程 m.aspxhome.com