js计算散点图方程式

//pointArrayX,pointArrayY 为两个数组,分别为x轴对应的点和y轴对应的点
function getEquationPara(pointArrayX, pointArrayY) {
    var abr = [];
    var xavg = eval(pointArrayX.join("+")) / pointArrayX.length;
    var yavg = eval(pointArrayY.join("+")) / pointArrayY.length;
    var a, b, r;
    var xysum = 0, xxsum = 0;
    var divider = 0, dividend = 0, xxavgsum = 0, yyavgsum = 0;
    for (var i = 0; i < pointArrayX.length; i++) {
        xysum += pointArrayX[i] * pointArrayY[i];
        xxsum += pointArrayX[i] * pointArrayX[i];
        dividend += (pointArrayX[i] - xavg) * (pointArrayY[i] - yavg);
        xxavgsum += (pointArrayX[i] - xavg) * (pointArrayX[i] - xavg);
        yyavgsum += (pointArrayY[i] - yavg) * (pointArrayY[i] - yavg);
    }
    b = (xysum - pointArrayX.length * xavg * yavg) / (xxsum - (pointArrayX.length * xavg * xavg));
    a =fomatFloat(PointFloat(yavg - b * xavg,4),3);//fomatFloat--四舍六入方法,查看详细
if (a==0) { a = "0.0"; } b = fomatFloat(b, 4); divider = fomatFloat(Math.sqrt(xxavgsum) * Math.sqrt(yyavgsum), 5); r = dividend / divider; abr.push(a); abr.push(b); abr.push(r.toString().substr(0, 6)); return abr; }

  function PointFloat(src, pos) {

     return Math.round(src * Math.pow(10, pos)) / Math.pow(10, pos);
  }

 

 

posted @ 2015-11-24 13:06  潇潇与偕  阅读(862)  评论(0编辑  收藏  举报