溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Apache Commons Math3探索之多項(xiàng)式曲線擬合的示例分析

發(fā)布時(shí)間:2021-07-21 09:14:54 來(lái)源:億速云 閱讀:139 作者:小新 欄目:編程語(yǔ)言

這篇文章將為大家詳細(xì)講解有關(guān)Apache Commons Math3探索之多項(xiàng)式曲線擬合的示例分析,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

具體如下。

多項(xiàng)式曲線擬合:org.apache.commons.math4.fitting.PolynomialCurveFitter類。

用法示例代碼:

// ... 創(chuàng)建并初始化輸入數(shù)據(jù): 
double[] x = new double[...]; 
double[] y = new double[...]; 
將原始的x-y數(shù)據(jù)序列合成帶權(quán)重的觀察點(diǎn)數(shù)據(jù)序列: 
WeightedObservedPoints points = new WeightedObservedPoints(); 
// 將x-y數(shù)據(jù)元素調(diào)用points.add(x[i], y[i])加入到觀察點(diǎn)序列中 
// ... 
PolynomialCurveFitter fitter = PolynomialCurveFitter.create(degree);  // degree 指定多項(xiàng)式階數(shù) 
double[] result = fitter.fit(points.toList());  // 曲線擬合,結(jié)果保存于雙精度數(shù)組中,由常數(shù)項(xiàng)至最高次冪系數(shù)排列

首先要準(zhǔn)備好待擬合的曲線數(shù)據(jù)x和y,這是兩個(gè)double數(shù)組,然后把這兩個(gè)數(shù)組合并到WeightedObservedPoints對(duì)象實(shí)例中,可以調(diào)用WeightedObservedPoints.add(x[i], y[i])將x和y序列中的數(shù)據(jù)逐個(gè)添加到觀察點(diǎn)序列對(duì)象中。隨后創(chuàng)建PolynomialCurveFitter對(duì)象,創(chuàng)建時(shí)要指定擬合多項(xiàng)式的階數(shù),注意階數(shù)要選擇適當(dāng),不是越高越好,否則擬合誤差會(huì)很大。最后調(diào)用PolynomialCurveFitter的fit方法即可完成多項(xiàng)式曲線擬合,fit方法的參數(shù)通過(guò)WeightedObservedPoints.toList()獲得。擬合結(jié)果通過(guò)一個(gè)double數(shù)組返回,按元素順序依次是常數(shù)項(xiàng)、一次項(xiàng)、二次項(xiàng)、……。

完整的演示代碼如下:

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("本算例用于計(jì)算多項(xiàng)式曲線擬合。正在初始化 計(jì)算數(shù)據(jù)(" + arrayLength + "點(diǎn), " + 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階多項(xiàng)式會(huì)有N+1個(gè)系數(shù),其中之一為常數(shù)項(xiàng) 
   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;  // 階數(shù) 
} 
public class TimeCostCalculator 
{ 
  public TimeCostCalculator() 
  { 
  } 
  /** 
  * 計(jì)算指定對(duì)象的運(yùn)行時(shí)間開(kāi)銷(xiāo)。 
  * 
  * @param testCase 指定被測(cè)對(duì)象。 
  * @return 返回sub.run的時(shí)間開(kāi)銷(xiāo),單位為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("--------------------------------------------------------------------------"); 
  } 
}

關(guān)于“Apache Commons Math3探索之多項(xiàng)式曲線擬合的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI