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

創(chuàng)新互聯(lián)專注于九原企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城網(wǎng)站建設(shè)。九原網(wǎng)站建設(shè)公司,為九原等地區(qū)提供建站服務(wù)。全流程按需定制網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
具體如下。
多項(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對象實(shí)例中,可以調(diào)用WeightedObservedPoints.add(x[i], y[i])將x和y序列中的數(shù)據(jù)逐個(gè)添加到觀察點(diǎn)序列對象中。隨后創(chuàng)建PolynomialCurveFitter對象,創(chuàng)建時(shí)要指定擬合多項(xiàng)式的階數(shù),注意階數(shù)要選擇適當(dāng),不是越高越好,否則擬合誤差會(huì)很大。最后調(diào)用PolynomialCurveFitter的fit方法即可完成多項(xiàng)式曲線擬合,fit方法的參數(shù)通過WeightedObservedPoints.toList()獲得。擬合結(jié)果通過一個(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ì)算指定對象的運(yùn)行時(shí)間開銷。
*
* @param testCase 指定被測對象。
* @return 返回sub.run的時(shí)間開銷,單位為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)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。
文章名稱:ApacheCommonsMath3探索之多項(xiàng)式曲線擬合的示例分析
文章位置:http://chinadenli.net/article40/jijjho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站策劃、動(dòng)態(tài)網(wǎng)站、用戶體驗(yàn)、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)