当前位置: 移动技术网 > IT编程>开发语言>Java > Apache Commons Math3探索之多项式曲线拟合实现代码

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

上一篇文章我们介绍了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学习之数值积分实例代码、等,有什么问题可以随时留言,小编会及时回复大家。下面推荐几本java方面的书籍,供大家学习,免费的哦。

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

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

更多精彩内容,尽在

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网