多项式曲线拟合

​ 我最近写的一个项目,需要通过散点图来形成一条拟合的曲线,以便于用来预测后续的数据。这个算法真是折磨我好几天,回顾了一波非线性回归以及查询很多资料,最终确定使用,多项式曲线拟合的方式来设计算法。

曲线拟合

曲线拟合的原理可以通过这个博客来了解一下:https://blog.csdn.net/llittleSun/article/details/115045660

本代码可以解决的问题是:二维坐标需要通过散点来确定曲线,再通过曲线表达式来预测后续点的坐标。

拟合曲线

代码分为三个部分:

1.创建辅助类

package com.ljh.controller;

/**
 * 多项式拟合辅助类
 */
public class PolynomialSoluter {
    private double[][] matrix;
    private double[] result;
    private int order;

    public PolynomialSoluter() {

    }

    // 检查输入项长度并生成增广矩阵
    private boolean init(double[][] matrixA, double[] arrayB) {
        order = arrayB.length;
        if (matrixA.length != order) {
            return false;
        }
        matrix = new double[order][order + 1];
        for (int i = 0; i < order; i++) {
            if (matrixA[i].length != order) {
                return false;
            }
            for (int j = 0; j < order; j++) {
                matrix[i][j] = matrixA[i][j];
            }
            matrix[i][order] = arrayB[i];
        }
        result = new double[order];
        return true;
    }

    public double[] getResult(double[][] matrixA, double[] arrayB) {
        if (!init(matrixA, arrayB)) {
            return null;
        }

        // 高斯消元-正向
        for (int i = 0; i < order; i++) {

            // 如果当前行对角线项为0则与后面的同列项非0的行交换
            if (!swithIfZero(i)) {
                return null;
            }
            // 消元
            for (int j = i + 1; j < order; j++) {
                if (matrix[j][i] == 0) {
                    continue;
                }
                double factor = matrix[j][i] / matrix[i][i];
                for (int l = i; l < order + 1; l++) {
                    matrix[j][l] = matrix[j][l] - matrix[i][l] * factor;
                }
            }
        }

        // 高斯消元-反向-去掉了冗余计算
        for (int i = order - 1; i >= 0; i--) {
            result[i] = matrix[i][order] / matrix[i][i];
            for (int j = i - 1; j > -1; j--) {
                matrix[j][order] = matrix[j][order] - result[i] * matrix[j][i];
            }
        }
        return result;
    }

    private boolean swithIfZero(int i) {
        if (matrix[i][i] == 0) {
            int j = i + 1;

            // 找到对应位置非0的列
            while (j < order && matrix[j][i] == 0) {
                j++;
            }

            // 若对应位置全为0则无解
            if (j == order) {
                return false;
            } else {
                switchRows(i, j);
            }
        }
        return true;
    }

    private void switchRows(int i, int j) {
        double[] tmp = matrix[i];
        matrix[i] = matrix[j];
        matrix[j] = tmp;
    }
}

2.创建可执行类

使用的话主要调用这个类中的方法。

package com.ljh.controller;

/**
 * 多项式曲线拟合
 */
public class LeastSquare {
    private double[][] matrixA;
    private double[] arrayB;
    private double[] factors;
    private int order;

    public LeastSquare() {

    }
    /**
     * 实例化后,计算前,先要输入参数并生成公式 arrayX为采样点的x轴坐标,按照采样顺序排列
     * arrayY为采样点的y轴坐标,按照采样顺序与x一一对应排列 order
     * 为进行拟合的阶数。用低阶来拟合高阶曲线时可能会不准确,但阶数过高会导致计算缓慢
     * @param arrayX  x坐标数组
     * @param arrayY  y坐标数组
     * @param order   为进行拟合的阶数
     * @return
     */
    public boolean generateFormula(double[] arrayX, double[] arrayY, int order) {
        if (arrayX.length != arrayY.length) {
            return false;
        }
        this.order = order;
        int len = arrayX.length;

        // 拟合运算中的x矩阵和y矩阵
        matrixA = new double[order + 1][order + 1];
        arrayB = new double[order + 1];

        // 生成y矩阵以及x矩阵中幂<=order的部分
        for (int i = 0; i < order + 1; i++) {
            double sumX = 0;
            for (int j = 0; j < len; j++) {
                double tmp = Math.pow(arrayX[j], i);
                sumX += tmp;
                arrayB[i] += tmp * arrayY[j];

            }
            for (int j = 0; j <= i; j++) {
                matrixA[j][i - j] = sumX;
            }
        }

        // 生成x矩阵中幂>order的部分
        for (int i = order + 1; i <= order * 2; i++) {
            double sumX = 0;
            for (int j = 0; j < len; j++) {
                sumX += Math.pow(arrayX[j], i);
            }

            for (int j = i - order; j < order + 1; j++) {
                matrixA[i - j][j] = sumX;
            }
        }

        // 实例化PolynomiaSoluter并解方程组,得到各阶的系数序列factors
        PolynomialSoluter soluter = new PolynomialSoluter();
        factors = soluter.getResult(matrixA, arrayB);
        if (factors == null) {
            return false;
        } else {
            return true;
        }
    }

    // 根据输入坐标,以及系数序列factors计算指定坐标的结果
    public double calculate(double x) {
        double result = factors[0];
        for (int i = 1; i <= order; i++) {
            result += factors[i] * Math.pow(x, i);
        }
        return result;
    }

}

3.此类是项目定制的方法

/**
 * 创建多项式曲线回归模型
 * @param x 游标点在中断面的x坐标
 * @param y 游标点在中断面的y坐标
 * @param v 游标点在中断面的速度v
 * @param fixedX 固定点x的坐标
 * @param fixedY 固定点y的坐标
 * @param order 为进行拟合的阶数。用低阶来拟合高阶曲线时可能会不准确,但阶数过高会导致计算缓慢,建议参数为:3
 * @return 返回中断面固定点的速度数组。
 */
public static double[]  curveFitting(double[] x,double[] y,double[] v,double[] fixedX,double[] fixedY,int order){
    double[] curveX=new double[x.length];
    double[] curveY=v;
    double[] curveFixedX=new double[fixedX.length];
    double[] curveFixedV=new double[fixedX.length];
    //计算固定点在曲线拟合的x坐标点
    for (int i = 0; i < fixedX.length; i++) {
        curveFixedX[i]=Math.sqrt(Math.pow(fixedX[i],2)+Math.pow(fixedY[i],2));
    }

    //创建拟合多项式的对象
    LeastSquare leastSquare = new LeastSquare();
    //计算回归曲线的x坐标
    for (int i = 0; i < x.length; i++) {
        curveX[i]=Math.sqrt(Math.pow(x[i],2)+Math.pow(y[i],2));
    }
    boolean b = leastSquare.generateFormula(curveX, curveY, order);
    System.out.println(b);

    for (int i = 0; i < fixedX.length; i++) {
        curveFixedV[i]=leastSquare.calculate(curveFixedX[i]);
    }
    return curveFixedV;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值