37
1
using System;
2
3
class Program
4
{
5
static void Main()
6
{
7
// Set the initial conditions and step size
8
double x0 = 0.0;
9
double y0 = 1.0;
10
double y1 = 0.0;
11
double y2 = 0.0;
12
double h = 0.1;
13
double xMax = 1.0;
14
// Solving the third-order linear homogeneous ordinary differential equation:
15
// y'''(x) + 2y''(x) - y'(x) - 2y(x) = 0
16
// Using the Runge-Kutta method for numerical approximation
17
Console.WriteLine("x\t\ty");
18
for (double x = x0; x <= xMax; x += h)
19
{
20
Console.WriteLine($"{x}\t\t{y0}");
21
double k1 = h * F(x, y0, y1, y2);
22
double k2 = h * F(x + h / 2, y0 + k1 / 2, y1, y2);
23
double k3 = h * F(x + h / 2, y0 + k2 / 2, y1, y2);
24
double k4 = h * F(x + h, y0 + k3, y1, y2);
25
y0 += (k1 + 2 * k2 + 2 * k3 + k4) / 6;
26
// Update y1 and y2 based on the derivatives
27
y2 = y1;
28
y1 = k1;
29
}
30
}
31
32
static double F(double x, double y, double yp, double ypp)
33
{
34
// Define the third-order ODE: y''' + 2y'' - y' - 2y = 0
35
return -2 * ypp + yp - 2 * y;
36
}
37
}
Cached Result