java拟合_Java拟合算法

1 importorg.apache.commons.math3.fitting.PolynomialCurveFitter;2 importorg.apache.commons.math3.fitting.WeightedObservedPoints;3 importorg.apache.commons.math3.linear.ArrayRealVector;4 importorg.apache.commons.math3.linear.DecompositionSolver;5 importorg.apache.commons.math3.linear.LUDecomposition;6 importorg.apache.commons.math3.linear.MatrixUtils;7 importorg.apache.commons.math3.linear.RealMatrix;8 importorg.apache.commons.math3.linear.RealVector;9

10 public classMathUtil {11 /**

12 * 一元线性拟合 y = a + b*x13 *14 *@paramx15 *@paramy16 * reuslt[0] = a result[1] = b result[2] 相关系数 result[3] 决定系数17 * result[4] 点数量(长度) result[5] 自由度18 */

19 public static double[] lineFitting(double x[], doubley[]) {20 int size =x.length;21 double xmean = 0.0;22 double ymean = 0.0;23 double xNum = 0.0;24 double yNum = 0.0;25 double xyNum = 0;26 double xNum2 = 0;27 double yNum2 = 0;28 double rss = 0;29 double tss = 0;30 double result[] = new double[6];31

32 for (int i = 0; i < size; i++) {33 xmean +=x[i];34 ymean +=y[i];35 xNum2 += x[i] *x[i];36 yNum2 += y[i] *y[i];37 xyNum += x[i] *y[i];38 }39 xNum =xmean;40 yNum =ymean;41 xmean /=size;42 ymean /=size;43

44 double sumx2 = 0.0f;45 double sumxy = 0.0f;46 for (int i = 0; i < size; i++) {47 sumx2 += (x[i] - xmean) * (x[i] -xmean);48 sumxy += (y[i] - ymean) * (x[i] -xmean);49 }50

51 double b = sumxy /sumx2;52 double a = ymean - b *xmean;53

54 result[0] =a;55 result[1] =b;56 System.out.println("a = " + a + ", b=" +b);57

58 double correlation = (xyNum - xNum * yNum /size)59 / Math.sqrt((xNum2 - xNum * xNum / size) * (yNum2 - yNum * yNum /size));60

61 System.out.println("相关系数:" +correlation);62 result[2] =correlation;63

64 for (int i = 0; i < size; i++) {65 rss += (y[i] - (a + b * x[i])) * (y[i] - (a + b *x[i]));66 tss += (y[i] - ymean) * (y[i] -ymean);67 }68

69 double r2 = 1 - (rss / (size - 1 - 1)) / (tss / (size - 1));70

71 result[3] =r2;72 System.out.println("决定系数" +r2);73

74 result[4] =x.length;75 result[5] = x.length - 1 - 1;76

77 returnresult;78 }79

80 /**

81 * 多元线性拟合 y = a + b*x1 + c*x282 *83 *@paramx84 *@paramy85 * result[0] = a result[b] = b . . . result[len - 4] 点数 result[len -86 * 3] 自由度 result[len - 2] 残差平方和 result[len - 1] 确定系数87 */

88 public static double[] lineFitting2(double x[][], doubley[]) {89 double[] a = new double[x.length + 1];90 double[] v = new double[2]; //这里的2为m

91 double[] dt = new double[4];92

93 line2sqt(x, y, 2, 11, a, dt, v);94

95 inti;96

97 System.out.println("残差平方和:" + dt[0]);98

99 double temp = a[a.length - 1];100 //更换输出位置,把常数放到第一位

101 for (i = a.length - 1; i > 0; i--) {102 a[i] = a[i - 1];103 }104

105 a[0] =temp;106

107 double[] result = new double[x.length + 5];108

109 for (i = 0; i <= x.length; i++) {110 result[i] =a[i];111 }112

113 result[x.length + 1] =y.length;114 result[x.length + 2] = y.length -x.length;115 result[x.length + 3] = dt[0];116 result[x.length + 4] =getLine2R(x, y, a, x.length);117

118 System.out.println("校正确定系数:" + result[x.length + 4]);119

120 returnresult;121 }122

123 /**

124 * 多项式拟合 y = a + b*x1 + c*x1^2…… result[0] = a result[1] = b . . . result[n + 1]125 * 点数 result[n + 2] 自由度 result[n + 3] 确定系数126 *127 *@paramn128 * 几级129 *@return

130 */

131 public static double[] dxsFitting(double x[], double y[], intn) {132 double result[] = new double[n + 4];133

134 WeightedObservedPoints obs = newWeightedObservedPoints();135

136 for (int i = 0; i < x.length; i++) {137 obs.add(x[i], y[i]);138 }139

140 //Instantiate a third-degree polynomial fitte

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值