Advertisement
myname0

CauchyProblem

Dec 6th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.29 KB | None | 0 0
  1.  static void BuildCauchyProblem()
  2.         {
  3.             List<double> y = new List<double>();
  4.             double x = 1.0;
  5.             double h = 0.05;
  6.             y.Add(CountYPrecise(x));
  7.  
  8.             Console.WriteLine("Euler's method: ");
  9.             for(int i = 0; i < 10; i++)
  10.             {
  11.                 Console.WriteLine("{0} \t {1} \t {2} \t {3}", x, y[i], CountYPrecise(x), Math.Abs(y[i] - CountYPrecise(x)));
  12.                 x += h;
  13.                 y.Add(y[i] + h *CountEulersFormula(x, y[i]));
  14.             }
  15.  
  16.             x = 1.0;
  17.             List<double> y2 = new List<double>();
  18.             y2.Add(CountYPrecise(x));
  19.  
  20.             Console.WriteLine("\nImproved Euler's method: ");
  21.             for (int i = 0; i < 10; i++)
  22.             {
  23.                 Console.WriteLine("{0} \t {1} \t {2} \t {3}", x, y2[i], CountYPrecise(x), Math.Abs(y2[i] - CountYPrecise(x)));
  24.                 x += h;
  25.  
  26.                 y2.Add(CountImproveEulersFormula(x, x - h, h, y2[i]));
  27.             }
  28.  
  29.             x = 1.0;
  30.             List<double> y3 = new List<double>();
  31.             y3.Add(CountYPrecise(x));
  32.  
  33.             Console.WriteLine("\nPredictor-corrector method: ");
  34.             for (int i = 0; i < 10; i++)
  35.             {
  36.                 Console.WriteLine("{0} \t {1} \t {2} \t {3}", x,  y3[i], CountYPrecise(x), Math.Abs(y3[i] - CountYPrecise(x)));
  37.                 double Fx1 = CountEulersFormula(x, y[i]);
  38.                 double fx2 = y3[i] + h * CountEulersFormula(x, y3[i]);
  39.                 double tmp = CountEulersFormula(x + h, fx2);
  40.                 y3.Add(y3[i] + h * (Fx1 + tmp) / 2);
  41.  
  42.                 x += h;
  43.                
  44.             }
  45.  
  46.         }
  47.  
  48.         static double CountYPrecise(double x)
  49.         {
  50.             return 7 * x * x * x + x * x + x;
  51.         }
  52.  
  53.         static double CountEulersFormula(double x, double fx)
  54.         {
  55.             return 7 * x * x * x + 22 * x * x + 3 * x + 1 - fx;
  56.         }
  57.  
  58.         static double CountImproveEulersFormula(double x1, double x2, double h, double y)
  59.         {
  60.             double newy = y + (h / 2) * CountEulersFormula(x1,y);
  61.             double x = (x1 + x2) / 2;
  62.             return y + h * CountEulersFormula(x, newy);
  63.         }
  64.  
  65.  
  66.  
  67.         static void Main(string[] args)
  68.         {
  69.             BuildCauchyProblem();
  70.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement