上一篇文章我們介紹了Apache Commons Math3學習之數值積分實例代碼,這里給大家分享math3多項式曲線擬合的相關內容,具體如下。
多項式曲線擬合:org.apache.commons.math3.fitting.PolynomialCurveFitter類。
用法示例代碼:
// ... 創建并初始化輸入數據: double[] x = new double[...]; double[] y = new double[...]; 將原始的x-y數據序列合成帶權重的觀察點數據序列: WeightedObservedPoints points = new WeightedObservedPoints(); // 將x-y數據元素調用points.add(x[i], y[i])加入到觀察點序列中 // ... PolynomialCurveFitter fitter = PolynomialCurveFitter.create(degree); // degree 指定多項式階數 double[] result = fitter.fit(points.toList()); // 曲線擬合,結果保存于雙精度數組中,由常數項至最高次冪系數排列
首先要準備好待擬合的曲線數據x和y,這是兩個double數組,然后把這兩個數組合并到WeightedObservedPoints對象實例中,可以調用WeightedObservedPoints.add(x[i], y[i])將x和y序列中的數據逐個添加到觀察點序列對象中。隨后創建PolynomialCurveFitter對象,創建時要指定擬合多項式的階數,注意階數要選擇適當,不是越高越好,否則擬合誤差會很大。最后調用PolynomialCurveFitter的fit方法即可完成多項式曲線擬合,fit方法的參數通過WeightedObservedPoints.toList()獲得。擬合結果通過一個double數組返回,按元素順序依次是常數項、一次項、二次項、……。
完整的演示代碼如下:
interface TestCase { public Object run(List<Object> params) throws Exception; public List<Object> getParams(); public void printResult(Object result); } class CalcCurveFitting implements TestCase { public CalcCurveFitting() { System.out.print("本算例用于計算多項式曲線擬合。正在初始化 計算數據(" + arrayLength + "點, " + degree + "階)... ..."); inputDataX = new double[arrayLength]; // inputDataX = new double[] {1, 2, 3, 4, 5, 6, 7}; inputDataY = new double[inputDataX.length]; double[] factor = new double[degree + 1]; // N階多項式會有N+1個系數,其中之一為常數項 for(int index = 0; index < factor.length; index ++) { factor[index] = index + 1; } for(int index = 0; index < inputDataY.length; index ++) { inputDataX[index] = index * 0.00001; inputDataY[index] = calcPoly(inputDataX[index], factor); // y = sum(x[n) * fact[n]) // System.out.print(inputDataY[index] + ", "); } points = new WeightedObservedPoints(); for(int index = 0; index < inputDataX.length; index ++) { points.add(inputDataX[index], inputDataY[index]); } System.out.println("初始化完成"); } @Override public List<Object> getParams() { List<Object> params = new ArrayList<Object>(); params.add(points); return params; } @Override public Object run(List<Object> params) throws Exception { PolynomialCurveFitter fitter = PolynomialCurveFitter.create(degree); WeightedObservedPoints points = (WeightedObservedPoints)params.get(0); double[] result = fitter.fit(points.toList()); return result; } @Override public void printResult(Object result) { for(double data : (double[])result) { System.out.println(data); } } private double calcPoly(double x, double[] factor) { double y = 0; for(int deg = 0; deg < factor.length; deg ++) { y += Math.pow(x, deg) * factor[deg]; } return y; } private double[] inputDataX = null; private double[] inputDataY = null; private WeightedObservedPoints points = null; private final int arrayLength = 200000; private final int degree = 5; // 階數 } public class TimeCostCalculator { public TimeCostCalculator() { } /** * 計算指定對象的運行時間開銷。 * * @param testCase 指定被測對象。 * @return 返回sub.run的時間開銷,單位為s。 * @throws Exception */ public double calcTimeCost(TestCase testCase) throws Exception { List<Object> params = testCase.getParams(); long startTime = System.nanoTime(); Object result = testCase.run(params); long stopTime = System.nanoTime(); testCase.printResult(result); System.out.println("start: " + startTime + " / stop: " + stopTime); double timeCost = (stopTime - startTime) * 1.0e-9; return timeCost; } public static void main(String[] args) throws Exception { TimeCostCalculator tcc = new TimeCostCalculator(); double timeCost; System.out.println("--------------------------------------------------------------------------"); timeCost = tcc.calcTimeCost(new CalcCurveFitting()); System.out.println("time cost is: " + timeCost + "s"); System.out.println("--------------------------------------------------------------------------"); } }
總結
以上就是本文關于Apache Commons Math3探索之多項式曲線擬合實現代碼的全部內容,希望對大家有所幫助。有什么問題可以隨時留言,小編會及時回復大家。下面推薦幾本Java方面的書籍,供大家學習,免費的哦。
新聞熱點
疑難解答
圖片精選