MLS Extrapolation Procedural Method
Afficher commentaires plus anciens
I have to create a script of MLS Extrapolation the easiest way. Could someone lay down the step-by-step procedure?
3 commentaires
Jamil Kasan
le 3 Oct 2016
Modifié(e) : Walter Roberson
le 3 Oct 2016
I have a c++ ready code here
// Example program
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <map>
typedef std::map<int, std::vector<double> > matr_t;
std::vector<double> generateRowVector(std::vector<double> absci_, int row_no, int size_of_matr)
{
std::vector<double> x;
int size_of_vec = absci_.size();
for (int i = 0; i < size_of_matr; ++i)
{
{
double sum = 0;
for (int j = 0; j < size_of_vec; ++j)
{
sum += pow(absci_[j], i + row_no - 1);
}
x.push_back(sum);
}
}
return x;
}
std::vector<double> generateColumnVector(std::vector<double> absci_, std::vector<double> ordi_, int size_of_col)
{
unsigned int size_of_vec = absci_.size();
std::vector<double> y;
if (size_of_vec == ordi_.size())
{
for (int col_no = 1; col_no <= size_of_col; ++col_no)
{
double sum = 0;
for (unsigned int i = 0; i < size_of_vec; ++i)
{
sum += pow(absci_[i], col_no - 1) * ordi_[i];
}
y.push_back(sum);
}
return y;
}
return (std::vector<double>){0,0,0};
}
double getDeterminant(std::map<int, std::vector<double> > matrix, int size_of_matr)
{
double minuend= 0, subtra = 0;
for (int i = 0; i < size_of_matr; ++i)
{
double sum = 1;
for (int row = 1; row <= size_of_matr; ++row)
{
std::cout << matrix[row][(row+i-1) % size_of_matr] << " ";
sum *= matrix[row][(row+i-1) % size_of_matr];
}
minuend += sum;
std::cout << "\n";
}
for (int i = size_of_matr; i > 0; --i)
{
double sum = 1;
for (int row = 1; row <= size_of_matr; ++row)
{
std::cout << "place" << (i-row + size_of_matr)% size_of_matr << ":" << matrix[row][(i-row + size_of_matr) % size_of_matr] << " ";
sum *= matrix[row][(i-row + size_of_matr) % size_of_matr];
}
subtra += sum;
std::cout << "\n";
}
std::cout << "\n";
return minuend - subtra;
}
void EliminateNthElementMthRow(matr_t& matrix_, int element_no, int row_no)
{
double eliminator = matrix_[row_no][element_no-1]/ matrix_[element_no][element_no-1];
int size_of_matrix = matrix_[1].size();
for (int i = 0; i < size_of_matrix; ++i)
{
matrix_[row_no][i] = matrix_[row_no][i] - (eliminator * matrix_[element_no][i]);
}
}
double getDeterminantViaUpperMatrix(matr_t matrix, int size_of_matr)
{
matr_t copyMatr = getUpperMatrix(matrix, size_of_matr);
double det = 1;
for (int i = 1; i <= size_of_matr; ++i)
{
det *= copyMatr[i][i-1];
}
return det;
}
matr_t getUpperMatrix(matr_t matrix_, int size_of_matr)
{
matr_t copyMatr = matrix_;
for (int i = 1; i < size_of_matr; ++i)
{
for (int j = i+1; j <= size_of_matr; ++j)
{
EliminateNthElementMthRow(copyMatr, i, j);
}
}
return copyMatr;
}
matr_t getLowerMatrix(matr_t matrix_)
{
}
matr_t generateIdentityMatr(int size_of_matr)
{
matr_t identity_mat;
for (int i = 1; i <= size_of_matr; ++i )
{
for (int j = 1; j <= size_of_matr; ++j)
{
if (i == j) identity_mat[i].push_back(1);
else identity_mat[i].push_back(0);
}
}
return identity_mat;
}
matr_t getInverseMatrix(matr_t matrix_, int size_of_matr)
{
matr_t identity_mat = generateIdentityMatr(int size_of_matr);
matr_t copyMatr = matrix_;
for (int i = 1; i<= size_of_matr; ++i)
{
copyMatr[i].insert(copyMatr[i].end(), identity_mat[i].begin(), identity_mat[i].end());
}
return copyMatr;
}
int main()
{
int size_of_matr = 3;
std::vector<double> absci({0,0.5,1,1.5,2,2.5});
std::vector<double> ordi({0, 0.25, 1, 2.25, 4, 6.25});
std::vector<double> y = generateColumnVector(absci, ordi, size_of_matr);
std::map<int, std::vector<double> > coeffMat;
for (int row_no = 1; row_no <= size_of_matr; ++row_no)
{
coeffMat[row_no] = generateRowVector(absci, row_no, size_of_matr);
for (auto const& data : coeffMat[row_no] )
{
std::cout << data << " ";
}
std::cout<<"\n";
}
std::cout<<"\n";
for (auto const& data : y)
{
std::cout << data << "\n";
}
std::cout<<"\n";
std::cout << getDeterminant(coeffMat, size_of_matr);
}
This is the procedure
Step 1: Calculate the mean of the xx-values and the mean of the yy-values.
X¯¯¯=∑i=1nxinY¯¯¯=∑i=1nyinX¯=∑i=1nxinY¯=∑i=1nyin
Step 2: The following formula gives the slope of the line of best fit:
m=∑i=1n(xi−X¯¯¯)(yi−Y¯¯¯)∑i=1n(xi−X¯¯¯)2m=∑i=1n(xi−X¯)(yi−Y¯)∑i=1n(xi−X¯)2
Step 3: Compute the yy-intercept of the line by using the formula:
b=Y¯¯¯−mX¯¯¯b=Y¯−mX¯
Step 4: Use the slope mm and the yy-intercept bb to form the equation of the line.
Kurt Masvidal
le 3 Oct 2016
Jamil Kasan
le 3 Oct 2016
Pm Sent for the C++ file :) Will code it in .m and will post here
Réponses (0)
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!