栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

直方图获取曲线n次方程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

直方图获取曲线n次方程

Java 使用 Apache commons-math3 线性拟合、非线性拟合实例(带效果图)
可参考地址:https://www.cnblogs.com/wufeiwua/p/15110699.html
个人目前了解的曲线方程计算过程及说明

返回结果说明

对象textR: R的值越接近1,说明曲线拟合程度越好,通常大于0.8时比较准确
对象textY:多项拟合曲线公式

 
public static Map getRY(int dimension, double[] arrX, double[] arrY) {
    Map map = new HashMap<>();
    Polyfit polyfit = new Polyfit();
    double[] coefficient = polyfit.MultiLine(arrX, arrY, arrY.length, dimension);
    double doubleR = polyfit.CorrelationR(arrX, arrY, coefficient);
    if(Double.isNaN(doubleR)){
        map.put("textY", "");
        map.put("textR", "");
        return map;
    }
    BigDecimal bg = new BigDecimal(doubleR);
    double f1 = bg.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
    String textR = f1 + "";
    String textY = "";
    //去除-0 改为 0
    for (int i = 0; i < coefficient.length; i++) {
        if (Double.parseDouble(String.format("%.3f", coefficient[i])) == 0.0) {
            coefficient[i] = new Double("0.000");
        }
    }
    if (coefficient.length == 4) {
        textY = String.format("%.3f", coefficient[3]) + "x3";
        if (coefficient[2] >= 0) {
            textY += "+" + String.format("%.3f", coefficient[2]) + "x2";
        } else {
            textY += String.format("%.3f", coefficient[2]) + "x2";
        }
        if (coefficient[1] >= 0) {
            textY += "+" + String.format("%.3f", coefficient[1]) + "x";
        } else {
            textY += String.format("%.3f", coefficient[1]) + "x";
        }
        if (coefficient[0] >= 0) {
            textY += "+" + String.format("%.3f", coefficient[0]);
        } else {
            textY += String.format("%.3f", coefficient[0]);
        }
    } else if (coefficient.length == 5) {
        textY = String.format("%.3f", coefficient[4]) + "x4";
        if (coefficient[3] >= 0) {
            textY += "+" + String.format("%.3f", coefficient[3]) + "x3";
        } else {
            textY += String.format("%.3f", coefficient[3]) + "x3";
        }
        if (coefficient[2] >= 0) {
            textY += "+" + String.format("%.3f", coefficient[2]) + "x2";
        } else {
            textY += String.format("%.3f", coefficient[2]) + "x2";
        }
        if (coefficient[1] >= 0) {
            textY += "+" + String.format("%.3f", coefficient[1]) + "x";
        } else {
            textY += String.format("%.3f", coefficient[1]) + "x";
        }
        if (coefficient[0] >= 0) {
            textY += "+" + String.format("%.3f", coefficient[0]);
        } else {
            textY += String.format("%.3f", coefficient[0]);
        }
    } else if (coefficient.length == 6) {
        textY = String.format("%.3f", coefficient[5]) + "x5";
        if (coefficient[4] >= 0) {
            textY += "+" + String.format("%.3f", coefficient[4]) + "x4";
        } else {
            textY += String.format("%.3f", coefficient[4]) + "x4";
        }
        if (coefficient[3] >= 0) {
            textY += "+" + String.format("%.3f", coefficient[3]) + "x3";
        } else {
            textY += String.format("%.3f", coefficient[3]) + "x3";
        }
        if (coefficient[2] >= 0) {
            textY += "+" + String.format("%.3f", coefficient[2]) + "x2";
        } else {
            textY += String.format("%.3f", coefficient[2]) + "x2";
        }
        if (coefficient[1] >= 0) {
            textY += "+" + String.format("%.3f", coefficient[1]) + "x";
        } else {
            textY += String.format("%.3f", coefficient[1]) + "x";
        }
        if (coefficient[0] >= 0) {
            textY += "+" + String.format("%.3f", coefficient[0]);
        } else {
            textY += String.format("%.3f", coefficient[0]);
        }
    } else if (coefficient.length == 7) {
        textY = String.format("%.3f", coefficient[6]) + "x6";
        if (coefficient[5] >= 0) {
            textY += "+" + String.format("%.3f", coefficient[5]) + "x5";
        } else {
            textY += String.format("%.3f", coefficient[5]) + "x5";
        }

        if (coefficient[4] >= 0) {
            textY += "+" + String.format("%.3f", coefficient[4]) + "x4";
        } else {
            textY += String.format("%.3f", coefficient[4]) + "x4";
        }
        if (coefficient[3] >= 0) {
            textY += "+" + String.format("%.3f", coefficient[3]) + "x3";
        } else {
            textY += String.format("%.3f", coefficient[3]) + "x3";
        }
        if (coefficient[2] >= 0) {
            textY += "+" + String.format("%.3f", coefficient[2]) + "x2";
        } else {
            textY += String.format("%.3f", coefficient[2]) + "x2";
        }
        if (coefficient[1] >= 0) {
            textY += "+" + String.format("%.3f", coefficient[1]) + "x";
        } else {
            textY += String.format("%.3f", coefficient[1]) + "x";
        }
        if (coefficient[0] >= 0) {
            textY += "+" + String.format("%.3f", coefficient[0]);
        } else {
            textY += String.format("%.3f", coefficient[0]);
        }
    }
    map.put("textY", textY);
    map.put("textR", textR);
    return map;

//根据曲线方程 通过x轴坐标获取y值: 通过自定义的公式规律解析的

 
public static double getYByX(String formula, double xValue) {
    String[] one = formula.split("\+");
    List list = new ArrayList<>();
    List listReduce = new ArrayList<>();
    String[] two;
    for (String str : one) {
        two = str.split("-");
        int i = 0;
        for (String ss : two) {
            if (i > 0) {
                listReduce.add(ss);
            } else {
                list.add(ss);
            }
            i++;
        }
    }
    double total = 0;
    //整数集合列表
    String[] dd;
    String splitX = "x";
    for (String data : list) {
        if (StringUtils.isEmpty(data)) {
            continue;
        }
        if (data.contains(splitX)) {
            dd = data.split(splitX);
            if (dd.length == 1) {
                total = total + Double.parseDouble(dd[0]) * xValue;
            } else {
                total = total + Double.parseDouble(dd[0]) * Math.pow(xValue, Integer.parseInt(dd[1]));
            }
        } else {
            total = total + Double.parseDouble(data);
        }
    }
    //负数列表
    for (String data : listReduce) {
        if (data.contains(splitX)) {
            dd = data.split(splitX);
            if (dd.length == 1) {
                total = total - Double.parseDouble(dd[0]) * xValue;
            } else {
                total = total - Double.parseDouble(dd[0]) * Math.pow(xValue, Integer.parseInt(dd[1]));
            }
        } else {
            total = total - Double.parseDouble(data);
        }
    }
    return total;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/880897.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号