Advertisement
cd62131

Ordinary Differential Equation

Feb 3rd, 2014
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.43 KB | None | 0 0
  1. #include <stdio.h>
  2. double f(double x, double y) {
  3.   return - x * y;
  4. }
  5. int main(void) {
  6.   double a = 0.;
  7.   double b = 3.;
  8.   int n = 1000;
  9.   double h = (b - a) / n;
  10.   double x = 0., ye = 1., yh = 1.;
  11.   for (int i = 0; i < n; i++) {
  12.     x += h;
  13.     ye += h * f(x, ye);
  14.     double k1 = f(x, yh);
  15.     double k2 = f(x + h, yh + h * k1);
  16.     yh += h * (k1 + k2) / 2.;
  17.     printf("%lf %lf %lf\n", x, ye, yh);
  18.   }
  19.   return 0;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement