Added euler's method for solving diffrential equations. Single and double variable functions.

This commit is contained in:
novak_99 2021-11-27 22:55:44 -08:00
parent f48ef24006
commit 074de5b24b
3 changed files with 39 additions and 1 deletions

View File

@ -141,6 +141,28 @@ namespace MLPP{
}
return x;
}
double NumericalAnalysis::eulerianMethod(double(*derivative)(double), std::vector<double> q_0, double p, double h){
double max_epoch = (p - q_0[0])/h;
double x = q_0[0];
double y = q_0[1];
for(int i = 0; i < max_epoch; i++){
y = y + h * derivative(x);
x += h;
}
return y;
}
double NumericalAnalysis::eulerianMethod(double(*derivative)(std::vector<double>), std::vector<double> q_0, double p, double h){
double max_epoch = (p - q_0[0])/h;
double x = q_0[0];
double y = q_0[1];
for(int i = 0; i < max_epoch; i++){
y = y + h * derivative({x, y});
x += h;
}
return y;
}
std::vector<double> NumericalAnalysis::jacobian(double(*function)(std::vector<double>), std::vector<double> x){
std::vector<double> jacobian;

View File

@ -32,6 +32,9 @@ namespace MLPP{
double halleyMethod(double(*function)(double), double x_0, double epoch_num);
double invQuadraticInterpolation(double (*function)(double), std::vector<double> x_0, double epoch_num);
double eulerianMethod(double(*derivative)(double), std::vector<double> q_0, double p, double h); // Euler's method for solving diffrential equations.
double eulerianMethod(double(*derivative)(std::vector<double>), std::vector<double> q_0, double p, double h); // Euler's method for solving diffrential equations.
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);
std::vector<std::vector<std::vector<double>>> thirdOrderTensor(double(*function)(std::vector<double>), std::vector<double> x);
@ -42,6 +45,7 @@ namespace MLPP{
double cubicApproximation(double(*function)(std::vector<double>), std::vector<double> c, std::vector<double> x);
double laplacian(double(*function)(std::vector<double>), std::vector<double> x); // laplacian
};
}

View File

@ -58,6 +58,14 @@ using namespace MLPP;
double f(double x){
return sin(x);
}
double f_prime(double x){
return 2 * x;
}
double f_prime_2var(std::vector<double> x){
return 2 * x[0] + x[1];
}
/*
y = x^3 + 2x - 2
y' = 3x^2 + 2
@ -587,7 +595,11 @@ int main() {
// alg.printMatrix(alg.tensor_vec_mult(tensor, {1,2}));
std::cout << numAn.cubicApproximation(f_mv, {0, 0, 0}, {1, 1, 1}) << std::endl;
// std::cout << numAn.cubicApproximation(f_mv, {0, 0, 0}, {1, 1, 1}) << std::endl;
//std::cout << numAn.eulerianMethod(f_prime, {1, 1}, 1.5, 0.000001) << std::endl;
std::cout << numAn.eulerianMethod(f_prime_2var, {2, 3}, 2.5, 0.00000001) << std::endl;
return 0;
}