Skip to content

Commit

Permalink
Create Predictor-corrector-method.cpp
Browse files Browse the repository at this point in the history
Fixes #2529 predictor-corrector method added
  • Loading branch information
utkarshbhola authored Oct 1, 2023
1 parent 6376bf4 commit 16c51d4
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions others/Predictor-corrector-method.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <iostream>
#include <cmath>

// 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;
}

0 comments on commit 16c51d4

Please sign in to comment.