Уравнение методом Рунге-Кутты - C#

Узнай цену своей работы

Формулировка задачи:

Нужно решить ДУ методом Рунге-Кутта где Значения , v у меня есть. Что-то не нашел знак бесконечности, извиняюсь

Решение задачи: «Уравнение методом Рунге-Кутты»

textual
Листинг программы
  1. namespace RungeKutta
  2. {
  3.     class Program
  4.     {
  5.         public delegate double Function(double x, double y);
  6.  
  7.         #region Ordinary Differential Equations - Methods
  8.  
  9.         public static double ODE_RungeKutta2(Function f, double x0, double y0, double h, double x)
  10.         {
  11.             double xnew, ynew, k1, k2, result = double.NaN;
  12.             if (x == x0)
  13.                 result = y0;
  14.             else if (x > x0)
  15.             {
  16.                 do
  17.                 {
  18.                     if (h > x - x0) h = x - x0;
  19.                     k1 = h * f(x0, y0);
  20.                     k2 = h * f(x0 + 0.5 * h, y0 + 0.5 * k1);
  21.                     ynew = y0 + k2;
  22.                     xnew = x0 + h;
  23.                     x0 = xnew;
  24.                     y0 = ynew;
  25.                 } while (x0 < x);
  26.                 result = ynew;
  27.             }
  28.             return result;
  29.         }
  30.  
  31.         public static double ODE_RungeKutta4(Function f, double x0, double y0, double h, double x)
  32.         {
  33.             double xnew, ynew, k1, k2, k3, k4, result = double.NaN;
  34.             if (x == x0)
  35.                 result = y0;
  36.             else if (x > x0)
  37.             {
  38.                 do
  39.                 {
  40.                     if (h > x - x0) h = x - x0;
  41.                     k1 = h * f(x0, y0);
  42.                     k2 = h * f(x0 + 0.5 * h, y0 + 0.5 * k1);
  43.                     k3 = h * f(x0 + 0.5 * h, y0 + 0.5 * k2);
  44.                     k4 = h * f(x0 + h, y0 + k3);
  45.                     ynew = y0 + (k1 + 2 * k2 + 2 * k3 + k4) / 6;
  46.                     xnew = x0 + h;
  47.                     x0 = xnew;
  48.                     y0 = ynew;
  49.                 } while (x0 < x);
  50.                 result = ynew;
  51.             }
  52.             return result;
  53.         }
  54.  
  55.         public static double ODE_RungeKuttaFehlberg(Function f, double x0, double y0, double x, double h, double tolerance)
  56.         {
  57.             double xnew, ynew, hnew, k1, k2, k3, k4, k5, k6;
  58.             double hmin = 0.0001;
  59.             double hmax = 0.5;
  60.             if (h > hmax) h = hmax;
  61.             if (h < hmin) h = hmin;
  62.  
  63.             while (x0 < x)
  64.             {
  65.                 k1 = h * f(x0, y0);
  66.                 k2 = h * f(x0 + 0.25 * h, y0 + 0.25 * k1);
  67.                 k3 = h * f(x0 + 3 * h / 8, y0 + 3 * k1 / 32 + 9 * k2 / 32);
  68.                 k4 = h * f(x0 + 12 * h / 13, y0 + 1932 * k1 / 2197 - 7200 * k2 / 2197 + 7296 * k3 / 2197);
  69.                 k5 = h * f(x0 + h, y0 + 439 * k1 / 216 - 8 * k2 + 3680 * k3 / 513 - 845 * k4 / 4104);
  70.                 k6 = h * f(x0 + 0.5 * h, y0 - 8 * k1 / 27 + 2 * k2 - 3544 * k3 / 2565 + 1859 * k4 / 4104 - 11 * k5 / 40);
  71.                 double error = Math.Abs(k1 / 360 - 128 * k3 / 4275 - 2197 * k4 / 75240 + k5 / 50 + 2 * k6 / 55) / h;
  72.                 double s = Math.Pow(0.5 * tolerance / error, 0.25);
  73.                 if (error < tolerance)
  74.                 {
  75.                     ynew = y0 + 25 * k1 / 216 + 1408 * k3 / 2565 + 2197 * k4 / 4104 - 0.2 * k5;
  76.                     xnew = x0 + h;
  77.                     x0 = xnew;
  78.                     y0 = ynew;
  79.                 }
  80.                 if (s < 0.1) s = 0.1;
  81.                 if (s > 4) s = 4;
  82.                 hnew = h * s;
  83.                 h = hnew;
  84.                 if (h > hmax) h = hmax;
  85.                 if (h < hmin) h = hmin;
  86.                 if (h > x - x0) h = x - x0;
  87.             } return y0;
  88.         }
  89.         #endregion
  90.  
  91.         #region Ordinary Differential Equations - Test functions
  92.  
  93.         static double f(double x, double y)
  94.         {
  95.             return y * Math.Cos(x);
  96.         }
  97.         #endregion  
  98.  
  99.         #region Ordinary Differential Equations - Test Methodes
  100.  
  101.         static void TestRungeKutta2()
  102.         {
  103.             double h = 0.001;
  104.             double x0 = 0.0;
  105.             double y0 = 1.0;
  106.             Console.WriteLine("\n Results from the 2nd-order Runge-Kutta method with h = {0}\n", h);
  107.             double result = y0;
  108.             for (int i = 0; i < 11; i++)
  109.             {
  110.                 double x = 0.1 * i;
  111.                 result = ODE_RungeKutta2(f, x0, result, h, x);
  112.                 double exact = Math.Exp(Math.Sin(x));
  113.                 if (i % 5 == 0)
  114.                     Console.WriteLine(" x = {0:n1}, y = {1:e12}, exact = {2:e12}", x, result, exact);
  115.                 x0 = x;
  116.             }
  117.         }
  118.  
  119.         static void TestRungeKutta4()
  120.         {
  121.             double h = 0.001;
  122.             double x0 = 0.0;
  123.             double y0 = 1.0;
  124.             Console.WriteLine("\n Results from the 4th-order Runge-Kutta method with h = {0}\n", h);
  125.             double result = y0;
  126.             for (int i = 0; i < 11; i++)
  127.             {
  128.                 double x = 0.1 * i;
  129.                 result = ODE_RungeKutta4(f, x0, result, h, x);
  130.                 double exact = Math.Exp(Math.Sin(x));
  131.                 if (i % 5 == 0)
  132.                     Console.WriteLine(" x = {0:n1}, y = {1:e12}, exact = {2:e12}", x, result, exact);
  133.                 x0 = x;
  134.             }
  135.         }
  136.  
  137.         static void TestRungeKuttaFehlberg()
  138.         {
  139.             double h = 0.2;
  140.             double x0 = 0.0;
  141.             double y0 = 1.0;
  142.             Console.WriteLine("\n Results from the fourth-order Runge-Kutta-Fehlberg method with h = {0}\n", h);
  143.             double result = y0;
  144.             for (int i = 0; i < 11; i++)
  145.             {
  146.                 double x = 0.1 * i;
  147.                 result = ODE_RungeKuttaFehlberg(f, x0, result, x, h, 1e-8);
  148.                 double exact = Math.Exp(Math.Sin(x));
  149.                 if (i % 5 == 0)
  150.                     Console.WriteLine(" x = {0:n1}, y = {1:e12}, exact = {2:e12}", x, result, exact);
  151.                 x0 = x;
  152.             }
  153.         }
  154.  
  155.         #endregion
  156.  
  157.         static void Main(string[] args)
  158.         {
  159.             TestRungeKutta2();
  160.             TestRungeKutta4();
  161.             TestRungeKuttaFehlberg();
  162.             Console.ReadLine();
  163.         }
  164.     }
  165. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

6   голосов , оценка 4.5 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы