added halley's method + invquadinter method

This commit is contained in:
novak_99 2021-11-24 14:44:08 -08:00
parent e2f5d20fbc
commit 985a8f7f78
3 changed files with 30 additions and 3 deletions

View File

@ -64,10 +64,34 @@ namespace MLPP{
double NumericalAnalysis::newtonRaphsonMethod(double(*function)(double), double x_0, double epoch_num){
double x = x_0;
for(int i = 0; i < epoch_num; i++){
x = x - function(x)/numDiff(function, x);
x -= function(x)/numDiff(function, x);
}
return x;
}
double NumericalAnalysis::halleyMethod(double (*function)(double), double x_0, double epoch_num){
double x = x_0;
for(int i = 0; i < epoch_num; i++){
x -= ((2 * function(x) * numDiff(function, x))/(2 * numDiff(function, x) * numDiff(function, x) - function(x) * numDiff_2(function, x)));
}
return x;
}
double NumericalAnalysis::invQuadraticInterpolation(double (*function)(double), std::vector<double> x_0, double epoch_num){
double x = 0;
std::vector<double> currentThree = x_0;
for(int i = 0; i < epoch_num; i++){
double t1 = ((function(currentThree[1]) * function(currentThree[2]))/( (function(currentThree[0]) - function(currentThree[1])) * (function(currentThree[0]) - function(currentThree[2])) ) ) * currentThree[0];
double t2 = ((function(currentThree[0]) * function(currentThree[2]))/( (function(currentThree[1]) - function(currentThree[0])) * (function(currentThree[1]) - function(currentThree[2])) ) ) * currentThree[1];
double t3 = ((function(currentThree[0]) * function(currentThree[1]))/( (function(currentThree[2]) - function(currentThree[0])) * (function(currentThree[2]) - function(currentThree[1])) ) ) * currentThree[2];
x = t1 + t2 + t3;
currentThree.erase(currentThree.begin());
currentThree.push_back(x);
}
return x;
}
std::vector<double> NumericalAnalysis::jacobian(double(*function)(std::vector<double>), std::vector<double> x){
std::vector<double> jacobian;

View File

@ -26,6 +26,8 @@ namespace MLPP{
double numDiff_2(double(*function)(std::vector<double>), std::vector<double> x, int axis1, int axis2);
double newtonRaphsonMethod(double(*function)(double), double x_0, double epoch_num);
double halleyMethod(double(*function)(double), double x_0, double epoch_num);
double invQuadraticInterpolation(double (*function)(double), std::vector<double> x_0, double epoch_num);
std::vector<double> jacobian(double(*function)(std::vector<double>), std::vector<double> x); // Indeed, for functions with scalar outputs the Jacobians will be vectors.
std::vector<std::vector<double>> hessian(double(*function)(std::vector<double>), std::vector<double> x);

View File

@ -158,9 +158,9 @@ int main() {
// std::cout << "Absolute Average Deviation: " << stat.absAvgDeviation(x) << std::endl;
// LINEAR ALGEBRA
std::vector<std::vector<double>> square = {{1, 1}, {-1, 1}, {1, -1}, {-1, -1}};
// std::vector<std::vector<double>> square = {{1, 1}, {-1, 1}, {1, -1}, {-1, -1}};
alg.printMatrix(alg.rotate(square, M_PI/4));
// alg.printMatrix(alg.rotate(square, M_PI/4));
// std::vector<std::vector<double>> A = {
// {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
@ -540,6 +540,7 @@ int main() {
// std::cout << numAn.numDiff(&f, 1) << std::endl;
// std::cout << numAn.newtonRaphsonMethod(&f, 1, 1000) << std::endl;
std::cout << numAn.invQuadraticInterpolation(&f, {100, 2,1.5}, 10) << std::endl;
// std::cout << numAn.numDiff(&f_mv, {1, 1}, 1) << std::endl; // Derivative w.r.t. x.