多项式曲线拟合:org.apache.commons.math3.fitting.PolynomialCurveFitter类。

用法示例代码:

  1. // ... 创建并初始化输入数据:
  2. double[] x = new double[...];
  3. double[] y = new double[...];
  4. 将原始的x-y数据序列合成带权重的观察点数据序列:
  5. WeightedObservedPoints points = new WeightedObservedPoints();
  6. // 将x-y数据元素调用points.add(x[i], y[i])加入到观察点序列中
  7. // ...
  8. PolynomialCurveFitter fitter = PolynomialCurveFitter.create(degree);   // degree 指定多项式阶数
  9. 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数组返回,按元素顺序依次是常数项、一次项、二次项、……。

完整的演示代码如下:

package fitting;

import org.apache.commons.math3.fitting.PolynomialCurveFitter;
import org.apache.commons.math3.fitting.WeightedObservedPoints; import java.util.ArrayList;
import java.util.List; public class TimeCostCalculator {
public static void main(String[] args) throws Exception {
TimeCostCalculator tcc = new TimeCostCalculator();
double timeCost = tcc.calcTimeCost(new CalcCurveFitting());
System.out.println("--------------------------------------------------------------------------");
System.out.println("time cost is: " + timeCost + "s");
System.out.println("--------------------------------------------------------------------------");
} /**
* 计算指定对象的运行时间开销。
*
* @param curveFitting 指定被测对象。
* @return 返回sub.run的时间开销,单位为s。
* @throws Exception
*/
public double calcTimeCost(CurveFitting curveFitting) throws Exception {
List<Object> params = curveFitting.getParams();
long startTime = System.nanoTime();
Object result = curveFitting.run(params);
long stopTime = System.nanoTime();
curveFitting.printResult(result);
System.out.println("start: " + startTime + " / stop: " + stopTime);
return 1.0e-9 * (stopTime - startTime);
} } interface CurveFitting {
public List<Object> getParams(); public Object run(List<Object> params) throws Exception; public void printResult(Object result);
} class CalcCurveFitting implements CurveFitting { private WeightedObservedPoints points; private final int degree = 5; // 阶数 public CalcCurveFitting() {
int arrayLength = 200000;
System.out.println(String.format("本算例用于计算多项式曲线拟合。正在初始化计算数据(%s点,%s阶......", arrayLength, degree));
double[] inputDataX = new double[arrayLength];
// inputDataX = new double[] {1, 2, 3, 4, 5, 6, 7};
double[] 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("init completely");
} @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;
} }

http://blog.csdn.net/kingfox/article/details/44118319

Apache Commons Math3学习笔记(2) - 多项式曲线拟合(转)的更多相关文章

  1. CloudSim4.0报错NoClassDefFoundError,Caused by: java.lang.ClassNotFoundException: org.apache.commons.math3.distribution.UniformRealDistribution

    今天下载了CloudSim 4.0的代码,运行其中自带的示例程序,结果有一部分运行错误: 原因是找不到org.apache.commons.math3.distribution.UniformReal ...

  2. Apache Commons Lang 学习栏目

    Apache Commons Lang 学习栏目 Apache Commons Lang 3.8.1 API https://mvnrepository.com/artifact/org.apache ...

  3. Apache Calcite 论文学习笔记

    特别声明:本文来源于掘金,"预留"发表的[Apache Calcite 论文学习笔记](https://juejin.im/post/5d2ed6a96fb9a07eea32a6f ...

  4. Maven+Spring Batch+Apache Commons VF学习

    Apache Commons VFS资料:例子:http://www.zihou.me/html/2011/04/12/3377.html详细例子:http://p7engqingyang.iteye ...

  5. 快速沃尔什变换 FWT 学习笔记【多项式】

    〇.前言 之前看到异或就担心是 FWT,然后才开始想别的. 这次学了 FWT 以后,以后判断应该就很快了吧? 参考资料 FWT 详解 知识点 by neither_nor 集训队论文 2015 集合幂 ...

  6. java apache commons HttpClient发送get和post请求的学习整理(转)

    文章转自:http://blog.csdn.net/ambitiontan/archive/2006/01/06/572171.aspx HttpClient 是我最近想研究的东西,以前想过的一些应用 ...

  7. apache commons类库的学习

    原文地址http://www.tuicool.com/articles/iyEbquE 1.1. 开篇 在Java的世界,有很多(成千上万)开源的框架,有成功的,也有不那么成功的,有声名显赫的,也有默 ...

  8. 一篇关于apache commons类库的详解

    1.1. 开篇 在Java的世界,有很多(成千上万)开源的框架,有成功的,也有不那么成功的,有声名显赫的,也有默默无闻的.在我看来,成功而默默无闻的那些框架值得我们格外的尊敬和关注,Jakarta C ...

  9. Solr学习笔记---部署Solr到Tomcat上,可视化界面的介绍和使用,Solr的基本内容介绍,SolrJ的使用

    学习Solr前需要有Lucene的基础 Lucene的一些简单用法:https://www.cnblogs.com/dddyyy/p/9842760.html 1.部署Solr到Tomcat(Wind ...

随机推荐

  1. Spring集成XFire开发WebService

    Spring是眼下最流行的JavaEE Framework,可是使用Spring的Spring-WS开发WebService却十分繁琐.XFire是一个简化WebService开发的开源项目.通过Sp ...

  2. poj3268(最短路)

    题目连接:http://poj.org/problem?id=3268 题意:给出n个点和m条单向边,现在所有牛要到牛x那里去参加聚会,并且所有牛参加聚会后还要回来,给你牛x,除了牛x之外的牛,他们都 ...

  3. hdu3001(状压dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 题意:n 个城市已经 m 条路 以及对应路费 c,要求遍历所有城市最少的路费,每个城市不能超过2 ...

  4. [poj 1127]Jack Straws[线段相交][并查集]

    题意: 给出一系列线段,判断某两个线段是否连通. 思路: 根据线段相交情况建立并查集, 在同一并查集中则连通. (第一反应是强连通分量...实际上只要判断共存即可, 具体的方向啊是没有关系的..) 并 ...

  5. Codeforces Round #277.5 (Div. 2)B——BerSU Ball

    B. BerSU Ball time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  6. BGP的状态机制

    Idle 状态:即空闲状态,不接受任何BGP的连接,等待Start事件的产生,如果有start事件产生,若有start事件产生,系统开启ConnectRetry定时器,向邻居发起TCP连接,并将状态变 ...

  7. MVC数据验证使用小结

    原文:MVC数据验证使用小结 描述:MVC数据验证使用小结 内容:display,Required,stringLength,Remote,compare,RegularExpression 本人最近 ...

  8. MooTools,jQuery库的一些比对

    jQuery与MooTools库的一些比对   今天就我自己的一些认识比对下这两个JS框架,更多的是希望大家能够对MooTools这个JS框架有更多的认识.毕竟,大多数从事web前端的人对上手容易的j ...

  9. phantomjs,selenium,pyv8,pythonwebkit,,,,,,,,,,,,,

    Pyv8,PythonWebKit,Selenium,PhantomJS,Ghost.py 等等.... 快速构建实时抓取集群[searchtb] 定义:http://i.cnblogs.com/Ed ...

  10. 利用SVNKit进行版本库的树的导出

    public List searchByTree(String userName,String passwd,String SVNServerUrl,String dirUrl){ //这里有点像 s ...