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


Posted in Javascript onJanuary 03, 2017

本文实例讲述了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());

PS:这里再为大家推荐几款计算工具供大家进一步参考借鉴:

在线一元函数(方程)求解计算工具:
http://tools.3water.com/jisuanqi/equ_jisuanqi

科学计算器在线使用_高级计算器在线计算:
http://tools.3water.com/jisuanqi/jsqkexue

在线计算器_标准计算器:
http://tools.3water.com/jisuanqi/jsq

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

Javascript 相关文章推荐
锋利的jQuery jQuery中的DOM操作
Mar 21 Javascript
js实现图片放大缩小功能后进行复杂排序的方法
Nov 08 Javascript
早该知道的7个JavaScript技巧
Mar 27 Javascript
javascript页面渲染速度测试脚本分享
Apr 15 Javascript
使用百度地图api实现根据地址查询经纬度
Dec 11 Javascript
JS控制伪元素的方法汇总
Apr 06 Javascript
jQuery实现复选框的全选和反选
Feb 02 Javascript
如何开发出更好的JavaScript模块
Dec 22 Javascript
详解使用vue-admin-template的优化历程
May 20 Javascript
详解js访问对象的属性和方法
Oct 25 Javascript
vue实现的下拉框功能示例
Jan 29 Javascript
原生js实现无缝轮播图效果
Jan 28 Javascript
Vue.js系列之项目结构说明(2)
Jan 03 #Javascript
Vue.js系列之项目搭建(1)
Jan 03 #Javascript
jquery实现下拉框多选方法介绍
Jan 03 #Javascript
jQuery实现可拖拽3D万花筒旋转特效
Jan 03 #Javascript
详解JavaScript常量定义
Jan 03 #Javascript
两种简单的跨域方法(jsonp、php)
Jan 02 #Javascript
JavaScript实现图片瀑布流和底部刷新
Jan 02 #Javascript
You might like
PHP生成随机数的方法实例分析
2015/01/22 PHP
jQuery中append、insertBefore、after与insertAfter的简单用法与注意事项
2020/04/04 Javascript
node.js开发中使用Node Supervisor实现监测文件修改并自动重启应用
2014/11/04 Javascript
JS文字球状放大效果代码分享
2015/08/19 Javascript
JQuery日历插件My97DatePicker日期范围限制
2016/01/20 Javascript
浅析angularJS中的ui-router和ng-grid模块
2016/05/20 Javascript
jQuery实现打开页面渐现效果示例
2016/07/27 Javascript
预防网页挂马的方法总结
2016/11/03 Javascript
基于JavaScript实现活动倒计时效果
2017/04/20 Javascript
AngularJS折叠菜单实现方法示例
2017/05/18 Javascript
vue内置指令详解
2018/04/03 Javascript
对node.js中render和send的用法详解
2018/05/14 Javascript
vue移动端监听滚动条高度的实现方法
2018/09/03 Javascript
总结javascript三元运算符知识点
2018/09/28 Javascript
详解Vue项目在其他电脑npm run dev运行报错的解决方法
2018/10/29 Javascript
基于vue.js组件实现分页效果
2018/12/29 Javascript
说说如何在Vue.js中实现数字输入组件的方法
2019/01/08 Javascript
vue:el-input输入时限制输入的类型操作
2020/08/05 Javascript
Python中基本的日期时间处理的学习教程
2015/10/16 Python
python在线编译器的简单原理及简单实现代码
2018/02/02 Python
Python回文字符串及回文数字判定功能示例
2018/03/20 Python
python 从csv读数据到mysql的实例
2018/06/21 Python
Python爬取数据保存为Json格式的代码示例
2019/04/09 Python
python模块hashlib(加密服务)知识点讲解
2019/11/25 Python
Python爬虫之Selenium中frame/iframe表单嵌套页面
2020/12/04 Python
CSS3 渐变(Gradients)之CSS3 线性渐变
2016/07/08 HTML / CSS
YesStyle美国/全球:购买亚洲时装、美容化妆品和生活百货
2017/01/16 全球购物
奥地利手表、香水、化妆品和珠宝购物网站:Brasty.at
2021/01/17 全球购物
以下为Windows NT 下的32 位C++程序,请计算sizeof 的值
2016/12/07 面试题
一套Delphi的笔试题一
2016/02/14 面试题
经济管理专业自荐信
2013/12/30 职场文书
初中考试作弊检讨书
2014/02/01 职场文书
2014年综治维稳工作总结
2014/11/17 职场文书
学困生帮扶工作总结
2015/08/13 职场文书
2016年质量月活动总结报告
2016/04/05 职场文书
浅谈Python实现opencv之图片色素的数值运算和逻辑运算
2021/06/23 Python