Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void BuildCauchyProblem()
- {
- List<double> y = new List<double>();
- double x = 1.0;
- double h = 0.05;
- y.Add(CountYPrecise(x));
- Console.WriteLine("Euler's method: ");
- for(int i = 0; i < 10; i++)
- {
- Console.WriteLine("{0} \t {1} \t {2} \t {3}", x, y[i], CountYPrecise(x), Math.Abs(y[i] - CountYPrecise(x)));
- x += h;
- y.Add(y[i] + h *CountEulersFormula(x, y[i]));
- }
- x = 1.0;
- List<double> y2 = new List<double>();
- y2.Add(CountYPrecise(x));
- Console.WriteLine("\nImproved Euler's method: ");
- for (int i = 0; i < 10; i++)
- {
- Console.WriteLine("{0} \t {1} \t {2} \t {3}", x, y2[i], CountYPrecise(x), Math.Abs(y2[i] - CountYPrecise(x)));
- x += h;
- y2.Add(CountImproveEulersFormula(x, x - h, h, y2[i]));
- }
- x = 1.0;
- List<double> y3 = new List<double>();
- y3.Add(CountYPrecise(x));
- Console.WriteLine("\nPredictor-corrector method: ");
- for (int i = 0; i < 10; i++)
- {
- Console.WriteLine("{0} \t {1} \t {2} \t {3}", x, y3[i], CountYPrecise(x), Math.Abs(y3[i] - CountYPrecise(x)));
- double Fx1 = CountEulersFormula(x, y[i]);
- double fx2 = y3[i] + h * CountEulersFormula(x, y3[i]);
- double tmp = CountEulersFormula(x + h, fx2);
- y3.Add(y3[i] + h * (Fx1 + tmp) / 2);
- x += h;
- }
- }
- static double CountYPrecise(double x)
- {
- return 7 * x * x * x + x * x + x;
- }
- static double CountEulersFormula(double x, double fx)
- {
- return 7 * x * x * x + 22 * x * x + 3 * x + 1 - fx;
- }
- static double CountImproveEulersFormula(double x1, double x2, double h, double y)
- {
- double newy = y + (h / 2) * CountEulersFormula(x1,y);
- double x = (x1 + x2) / 2;
- return y + h * CountEulersFormula(x, newy);
- }
- static void Main(string[] args)
- {
- BuildCauchyProblem();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement