-
-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Predictor-corrector-method.cpp
Fixes #2529 predictor-corrector method added
- Loading branch information
1 parent
6376bf4
commit 16c51d4
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |