diff --git a/others/Predictor-corrector-method.cpp b/others/Predictor-corrector-method.cpp new file mode 100644 index 00000000000..ecba9e598a4 --- /dev/null +++ b/others/Predictor-corrector-method.cpp @@ -0,0 +1,38 @@ +#include +#include + +// Define the ODE function f(x, y) +double f(double x, double y) { + return x - y; // Replace this with your desired ODE function +} + +// Predictor-Corrector method to approximate the solution +double predictorCorrector(double x0, double y0, double h, double x_target) { + double x = x0; + double y = y0; + + while (x < x_target) { + // Predictor Step (Euler's Method) + double y_pred = y + h * f(x, y); + + // Corrector Step (Improved Euler's Method) + y = y + 0.5 * h * (f(x, y) + f(x + h, y_pred)); + + x += h; + } + + return y; // Approximate solution at x_target +} + +int main() { + double x0 = 0.0; // Initial value of x + double y0 = 1.0; // Initial value of y + double h = 0.1; // Step size (adjust as needed) + double x_target = 1.0; // Target value of x for the solution + + double result = predictorCorrector(x0, y0, h, x_target); + + std::cout << "Approximate solution at x = " << x_target << ": y = " << result << std::endl; + + return 0; +}