编程语言
首页 > 编程语言> > Java实现最小二乘法线性拟合,传感与检测,单臂半桥全桥实验,江南大学自动化

Java实现最小二乘法线性拟合,传感与检测,单臂半桥全桥实验,江南大学自动化

作者:互联网

因为作为资源上传不方便我们获取且我想免费分享给有需要的小伙伴,以后所有实验报告都通过文章形式记录输出了,仅供参考,欢迎交流。(最小二乘法代码在文末)

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

电桥特性曲线:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Java实现最小二乘法线性拟合及计算灵敏度、非线性误差的代码:

/**
 * @Author: Yeman
 * @Date: 2021-10-29-23:03
 * @Description:
 */
public class LeastSquares {
    public static void matching(double[] x, double[] y, double[] input) {
        double k = getK(x, y);
        double b = getB(x, y);
        System.out.println("线性回归系数k值:\t" + k + "\n" + "线性回归系数b值:\t" + b);
        double maxy = 0; //用来记录最大偏差
        //寻找最大偏差
        for (int i = 0; i < input.length; i++) {
            if (Math.abs(k * input[i] + b - y[i]) > maxy){
                maxy = Math.abs(k * input[i] + b - y[i]);
            }
        }
        System.out.println("最大偏差为:" + maxy);
        //求灵敏度
        double s = 0;
        double sum = 0;
        for (int i = 1; i < y.length; i++) {
            sum += y[i] - y[i-1];
        }
        s = sum / (y.length - 1) / 20;
        System.out.println("灵敏度为:" + s);
        //求线性误差
        System.out.println("非线性误差为:" + maxy/y[10]);
    }

    /*
     * 返回x的系数k 公式:k = ( n sum( xy ) - sum( x ) sum( y ) ) / ( n sum( x^2 ) - sum(x) ^ 2 )
     */
    public static double getK(double[] x, double[] y) {
        int n = x.length;
        return (double) ((n * pSum(x, y) - sum(x) * sum(y)) / (n * sqSum(x) - Math.pow(sum(x), 2)));
    }

    /*
     * 返回常量系数系数b 公式:b = sum( y ) / n - k * sum( x ) / n
     */
    public static double getB(double[] x, double[] y) {
        int n = x.length;
        double k = getK(x, y);
        return sum(y) / n - k * sum(x) / n;
    }

    /*
     * 求和
     */
    private static double sum(double[] ds) {
        double s = 0;
        for (double d : ds)
        {
            s = s + d;
        }
        return s;
    }

    /*
     * 求平方和
     */
    private static double sqSum(double[] ds) {
        double s = 0;
        for (double d : ds) {
            s = (double) (s + Math.pow(d, 2));
        }
        return s;
    }

    /*
     * 返回对应项相乘后的和
     */
    private static double pSum(double[] x, double[] y) {
        double s = 0;
        for (int i = 0; i < x.length; i++) {
            s = s + x[i] * y[i];
        }
        return s;
    }

    public static void main(String[] args) {
        double[] x = {0,20,40,60,80,100,120,140,160,180,200};
        double[] y1 = {0,4.1,8.1,12.2,17.1,21.7,25.8,29.9,34.1,37.8,43.3};
        double[] y2 = {0,7.5,15.1,22.9,30.7,38.6,46.3,54.2,62.0,69.9,77.8};
        double[] y3 = {0,15.0,31.3,47.0,62.6,78.3,93.8,109.6,125.5,141.2,157.3};
        double[] inputs = x;
        System.out.println("===单臂电桥:");
        matching(x, y1,inputs);
        System.out.println("======半桥:");
        matching(x, y2,inputs);
        System.out.println("======全桥:");
        matching(x, y3,inputs);
    }
}

标签:Java,double,sum,System,static,println,半桥,out,全桥
来源: https://blog.csdn.net/m0_46653805/article/details/121058403