Created
January 4, 2017 02:17
-
-
Save ostapkonst/03c3663a090fac60d0b551e14924b8af to your computer and use it in GitHub Desktop.
Задача. Решить дифференциальное уравнение методом Рунге-Кутта первого и второго порядка точности.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace SummerPractise | |
{ | |
class Program | |
{ | |
// Производная функции (точное решение y(1) = 0.350 при y(0) = 0 на [0, 1]) | |
static double dFunc(double x, double y) | |
{ | |
return (Math.Pow(x, 2) + Math.Pow(y, 2)); | |
} | |
// Метод Рунге-Кутта первого порядка (метод Эйлера) | |
static double Eyler(double a, double b, double dot, double N, Func<double, double, double> f) | |
{ | |
double y = dot; | |
double h = (b - a) / N; | |
for (uint i = 0; i < N; i++) | |
y += h * f(a + i * h, y); | |
return y; | |
} | |
// Метод Рунге-Кутта второго порядка точности | |
static double Runge(double a, double b, double dot, double N, Func<double, double, double> f) | |
{ | |
double y = dot; | |
double h = (b - a) / N; | |
for (uint i = 0; i < N; i++) | |
{ | |
double x = a + i * h; | |
y += h * f(x + h / 2, y + h * f(x, y) / 2); | |
} | |
return y; | |
} | |
static void Main(string[] args) | |
{ | |
Console.WriteLine(" ---------------------------------------------- "); | |
Console.WriteLine("| Приближенное решение задачи Коши |"); | |
Console.WriteLine("| Требуется: шаг, нач. точка, промежуток |"); | |
Console.WriteLine(" ---------------------------------------------- "); | |
double a; | |
do | |
Console.Write("Начало п.: "); | |
while (!double.TryParse(Console.ReadLine(), out a)); | |
double b; | |
do | |
Console.Write("Конец п.: "); | |
while (!double.TryParse(Console.ReadLine(), out b) || a >= b); | |
double dot; | |
do | |
Console.Write("y({0}) = ", a); | |
while (!double.TryParse(Console.ReadLine(), out dot)); | |
uint N; | |
do | |
Console.Write("Количество шагов: "); | |
while (!uint.TryParse(Console.ReadLine(), out N) || N == 0); | |
Console.WriteLine("Результат Эйлера = {0}", Eyler(a, b, dot, N, dFunc)); | |
Console.WriteLine("Результат Рунге-Кутта = {0}", Runge(a, b, dot, N, dFunc)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment