最小二乘法擬合曲線:4次函式
{
double a, b, c, d, e;
//Mat A = Mat_<double>(5, 5);
//Mat B = Mat_<double>(5, 1);
//Mat C = Mat_<double>(5, 1);
Mat A(5, 5, CV_64F);
Mat B(5, 1, CV_64F);
Mat X(5, 1, CV_64F);
double sum = 0;
for (size_t i = 0; i < size; ++i)
{
sum += pow(src_x[i], 8);
}
A.at<double>(0, 0) = sum;
sum = 0;
for (size_t i = 0; i < size; ++i)
{
sum += pow(src_x[i], 7);
}
A.at<double>(0, 1) = A.at<double>(1, 0) = sum;
sum = 0;
for (size_t i = 0; i < size; ++i)
{
sum += pow(src_x[i], 6);
}
A.at<double>(0, 2) = A.at<double>(1, 1) = A.at<double>(2, 0) = sum;
sum = 0;
for (size_t i = 0; i < size; ++i)
{
sum += pow(src_x[i], 5);
}
A.at<double>(0, 3) = A.at<double>(1, 2) = A.at<double>(2, 1) = A.at<double>(3, 0) = sum;
sum = 0;
for (size_t i = 0; i < size; ++i)
{
sum += pow(src_x[i], 4);
}
A.at<double>(0, 4) = A.at<double>(1, 3) = A.at<double>(2, 2) = A.at<double>(3, 1) = A.at<double>(4, 0) = sum;
sum = 0;
for (size_t i = 0; i < size; ++i)
{
sum += pow(src_x[i], 3);
}
A.at<double>(1, 4) = A.at<double>(2, 3) = A.at<double>(3, 2) = A.at<double>(4, 1) = sum;
sum = 0;
for (size_t i = 0; i < size; ++i)
{
sum += pow(src_x[i], 2);
}
A.at<double>(2, 4) = A.at<double>(3, 3) = A.at<double>(4, 2) = sum;
sum = 0;
for (size_t i = 0; i < size; ++i)
{
sum += src_x[i];
}
A.at<double>(3, 4) = A.at<double>(4, 3) = sum;
sum = 0;
for (size_t i = 0; i < size; ++i)
{
sum += 1;
}
A.at<double>(4, 4) = sum;
sum = 0;
for (size_t i = 0; i < size; ++i)
{
sum += pow(src_x[i], 4) * src_y[i];
}
B.at<double>(0, 0) = sum;
sum = 0;
for (size_t i = 0; i < size; ++i)
{
sum += pow(src_x[i], 3) * src_y[i];
}
B.at<double>(1, 0) = sum;
sum = 0;
for (size_t i = 0; i < size; ++i)
{
sum += pow(src_x[i], 2) * src_y[i];
}
B.at<double>(2, 0) = sum;
sum = 0;
for (size_t i = 0; i < size; ++i)
{
sum += src_x[i] * src_y[i];
}
B.at<double>(3, 0) = sum;
sum = 0;
for (size_t i = 0; i < size; ++i)
{
sum += src_y[i];
}
B.at<double>(4, 0) = sum;
//X = A.inv() * B;
solve(A, B, X);// A * X = B
a = X.at<double>(0, 0);
b = X.at<double>(1, 0);
c = X.at<double>(2, 0);
d = X.at<double>(3, 0);
e = X.at<double>(4, 0);
printf("\n%f, %f, %f, %f, %f\n", a, b, c, d, e);
for (size_t i = 0; i < size; ++i)
{
double ret = a * pow(src_x[i], 4) + b * pow(src_x[i], 3) + c * pow(src_x[i], 2) + d * src_x[i] + e;
dst_y.push_back(ret);
}
相關文章
- 最小二乘法擬合非線性函式及其Matlab/Excel 實現函式MatlabExcel
- Python擬合曲線Python
- excel曲線擬合怎麼弄Excel
- 如何使用Python曲線擬合Python
- 【Python】keras使用LSTM擬合曲線PythonKeras
- 總體最小二乘法(Total Least Squares)擬合直線AST
- 4擬合線性函式的k和b函式
- 最小二乘法擬合圓心
- .NET 白板書寫加速-曲線擬合預測
- [Open3d系列]--點雲曲線擬合3D
- JavaScript繪製sin正弦函式曲線JavaScript函式
- JavaScript 繪製sin正弦函式曲線JavaScript函式
- pytorch擬合sin函式PyTorch函式
- R語言之視覺化①③散點圖+擬合曲線R語言視覺化
- excel線性迴歸擬合線Trend函式是這樣來使用的Excel函式
- 函式的遞迴及科赫曲線繪製函式遞迴
- 虛擬函式,虛擬函式表函式
- 虛擬函式 純虛擬函式函式
- dwg解析-樣條曲線擬合點解析(中望CAD匯出)
- 介面、虛擬函式、純虛擬函式、抽象類函式抽象
- 虛擬函式函式
- 基於移動最小二乘法的點雲曲面擬合(python)Python
- 【python資料探勘課程】十四.Scipy呼叫curve_fit實現曲線擬合Python
- 自定義View合輯(6)-波浪(貝塞爾曲線)View
- [Lang] 虛擬函式函式
- C++中抽象類、虛擬函式和純虛擬函式C++抽象函式
- C++中的虛擬函式與虛擬函式表 (轉)C++函式
- 機器學習中的PR曲線和ROC曲線機器學習
- 【C++筆記】虛擬函式(從虛擬函式表來解析)C++筆記函式
- 【C++筆記】虛擬函式(從虛擬函式概念來解析)C++筆記函式
- 損失函式:最小二乘法與極大似然估計法函式
- 線性迴歸,邏輯迴歸的學習(包含最小二乘法及極大似然函式等)邏輯迴歸函式
- C++ 虛擬函式C++函式
- C++虛擬函式C++函式
- 虛擬函式釋義函式
- JS 函式式概念: 管道 和 組合JS函式
- 平凡的函式 線性篩積性函式函式
- 深入C++成員函式及虛擬函式表C++函式