apache commons math3多项式曲线拟合 Apache Commons Math3探索之多项式曲线拟合实现代码

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

apache commons math3多项式曲线拟合 Apache Commons Math3探索之多项式曲线拟合实现代码

狐帝   2021-03-29 我要评论
想了解Apache Commons Math3探索之多项式曲线拟合实现代码的相关内容吗,狐帝在本文为您仔细讲解apache commons math3多项式曲线拟合的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:apache,commons,math3,多项式曲线拟合,math3,多项式曲线拟合,下面大家一起来学习吧。

上一篇文章我们介绍了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探索之多项式曲线拟合实现代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:Apache Commons Math3学习之数值积分实例代码apache zookeeper使用方法实例详解等,有什么问题可以随时留言,小编会及时回复大家。下面推荐几本Java方面的书籍,供大家学习,免费的哦。

7天学会spring+cloud教程 pdf格式

www.qb5200.com/books/575161.html

Java核心技术:基础知识 卷I(原书第10版) (凯S.霍斯特曼) 中文pdf完整版

https://m.qb5200.com/www.qb5200.com/books/569792.html

更多精彩内容,尽在https://m.qb5200.com/www.qb5200.com/

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们