Apache Commons Math3探索之多項式曲線擬合實現(xiàn)代碼
上一篇文章我們介紹了Apache Commons Math3學(xué)習(xí)之?dāng)?shù)值積分實例代碼,這里給大家分享math3多項式曲線擬合的相關(guān)內(nèi)容,具體如下。
多項式曲線擬合:org.apache.commons.math3.fitting.PolynomialCurveFitter類。
用法示例代碼:
// ... 創(chuàng)建并初始化輸入數(shù)據(jù): double[] x = new double[...]; double[] y = new double[...]; 將原始的x-y數(shù)據(jù)序列合成帶權(quán)重的觀察點數(shù)據(jù)序列: WeightedObservedPoints points = new WeightedObservedPoints(); // 將x-y數(shù)據(jù)元素調(diào)用points.add(x[i], y[i])加入到觀察點序列中 // ... PolynomialCurveFitter fitter = PolynomialCurveFitter.create(degree); // degree 指定多項式階數(shù) double[] result = fitter.fit(points.toList()); // 曲線擬合,結(jié)果保存于雙精度數(shù)組中,由常數(shù)項至最高次冪系數(shù)排列
首先要準(zhǔn)備好待擬合的曲線數(shù)據(jù)x和y,這是兩個double數(shù)組,然后把這兩個數(shù)組合并到WeightedObservedPoints對象實例中,可以調(diào)用WeightedObservedPoints.add(x[i], y[i])將x和y序列中的數(shù)據(jù)逐個添加到觀察點序列對象中。隨后創(chuàng)建PolynomialCurveFitter對象,創(chuàng)建時要指定擬合多項式的階數(shù),注意階數(shù)要選擇適當(dāng),不是越高越好,否則擬合誤差會很大。最后調(diào)用PolynomialCurveFitter的fit方法即可完成多項式曲線擬合,fit方法的參數(shù)通過WeightedObservedPoints.toList()獲得。擬合結(jié)果通過一個double數(shù)組返回,按元素順序依次是常數(shù)項、一次項、二次項、……。
完整的演示代碼如下:
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("本算例用于計算多項式曲線擬合。正在初始化 計算數(shù)據(jù)(" + 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個系數(shù),其中之一為常數(shù)項
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()
{
}
/**
* 計算指定對象的運行時間開銷。
*
* @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("--------------------------------------------------------------------------");
}
}
總結(jié)
以上就是本文關(guān)于Apache Commons Math3探索之多項式曲線擬合實現(xiàn)代碼的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:Apache Commons Math3學(xué)習(xí)之?dāng)?shù)值積分實例代碼、apache zookeeper使用方法實例詳解等,有什么問題可以隨時留言,小編會及時回復(fù)大家。下面推薦幾本Java方面的書籍,供大家學(xué)習(xí),免費的哦。
7天學(xué)會spring+cloud教程 pdf格式
Java核心技術(shù):基礎(chǔ)知識 卷I(原書第10版) (凱S.霍斯特曼) 中文pdf完整版
http://chabaoo.cn/books/569792.html
更多精彩內(nèi)容,盡在http://chabaoo.cn/
相關(guān)文章
詳解Java使用JMH進(jìn)行基準(zhǔn)性能測試
本文主要介紹了Java使用JMH進(jìn)行基準(zhǔn)性能測試,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11
Mybatis-Plus實現(xiàn)只更新部分字段的數(shù)據(jù)
這篇文章主要介紹了Mybatis-Plus實現(xiàn)只更新部分字段的數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
Springboot如何連接遠(yuǎn)程服務(wù)器上的數(shù)據(jù)庫實踐
本文主要介紹了Springboot如何連接遠(yuǎn)程服務(wù)器上的數(shù)據(jù)庫實踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
RocketMq同組消費者如何自動設(shè)置InstanceName
這篇文章主要介紹了RocketMq同組消費者如何自動設(shè)置InstanceName問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06

