Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 KB | None | 0 0
  1. using System;
  2.  
  3. namespace PredictorCorrectorSolver
  4. {
  5.     class PredictorCorrector
  6.     {
  7.         public static double[] x;
  8.         public static double[] y;
  9.         public static double[] f;
  10.  
  11.         public static void PredCorrSolve(double x0, double xn, double y0, double h)
  12.         {
  13.             int n = (int)(((xn - x0) / h) + 1.0);
  14.             x = new double[n]; x[0] = x0;
  15.             y = new double[n]; y[0] = y0;
  16.             f = new double[n];
  17.             for (int i = 1; i < n; i++)
  18.             {
  19.                 x[i] = x[0] + i * h;
  20.             }
  21.  
  22.             for (int i = 0; i <= 2; i++)
  23.             {
  24.                 y[i + 1] = RungeKutta(x[i], y[i], h);
  25.                 f[i] = Function(x[i], y[i]);
  26.             }
  27.  
  28.             for (int i = 3; i < (n - 1); i++)
  29.             {
  30.                 f[i] = Function(x[i], y[i]);
  31.                 y[i + 1] = y[i] + (h / 24) * (55 * f[i] - 59 *
  32.                     f[i - 1] + 37 * f[i - 2] - 9 * f[i - 3]);
  33.                 double tmp;
  34.                 while (true)
  35.                 {
  36.                     f[i + 1] = Function(x[i + 1], y[i + 1]);
  37.                     tmp = y[i] + (h / 24) * (9 * f[i + 1] + 19 *
  38.                         f[i] - 5 * f[i - 1] + f[i - 2]);
  39.                     if (!(Math.Abs(tmp - y[i + 1]) < 0.00001))
  40.                         y[i + 1] = tmp;
  41.                     else break;
  42.                 }
  43.             }
  44.         }
  45.  
  46.         private static double RungeKutta(double xi, double yi, double h)
  47.         {
  48.             double[] k = new double[4];
  49.             k[0] = Function(xi, yi);
  50.             k[1] = Function(xi + h / 2, yi + h * k[0] / 2);
  51.             k[2] = Function(xi + h / 2, yi + h * k[1] / 2);
  52.             k[3] = Function(xi + h, yi + h * k[2]);
  53.             return yi + (h / 6) * (k[0] + 2 * k[1] + 2 * k[2] + k[3]);
  54.         }
  55.  
  56.         private static double Function(double xi, double yi)
  57.         {
  58.             return 2 * (Math.Pow(xi, 2) + yi);
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement