using System using System Collections Generic using System Linq using

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Diffur
{
class Program
{
static int N = 3;
static double y(double x) { return N * Math.Exp(x); }
static double f(double x) { return 21 * Math.Exp(x); }
static double p(double x) { return N; }
static double q(double x) { return N; }
static double[] TridiagonalMatrixAlgorithm(double[] a, double[] b, double[] c, double[] d)
{
int n = b.Length;
double[] alpha = new double[n - 1];
double[] beta = new double[n - 1];
double[] x = new double[n];
alpha[0] = (-1) * c[0] / b[0];
beta[0] = d[0] / b[0];
//прогонка
for (int i = 1; i < n - 1; i++)
{
alpha[i] = ((-1) * c[i]) / (a[i - 1] * alpha[i - 1] + b[i]);
beta[i] = (d[i] - a[i - 1] * beta[i - 1]) / (a[i - 1] * alpha[i - 1] + b[i]);
}
x[n - 1] = (d[n - 1] - beta[n - 2] * a[n - 2]) / (a[n - 2] * alpha[n - 2] + b[n - 1]);
for (int i = n - 2; i >= 0; i--)
{
x[i] = alpha[i] * x[i + 1] + beta[i];
}
//
return x;
}
static void Main(string[] args)
{
int count = 20; // количество шагов
double[] res = new double[count];
double[] x = new double[count];
double l = 0;
double r = 1;
double h = (r - l) / Convert.ToDouble(count-1);
for (int i = 0; i < count; i++)
{
x[i] = h * i;
}
res[0] = y(x[0]);
res[count - 1] = y(x[count - 1]);
double[] a = new double[count - 3];
double[] b = new double[count - 2];
double[] c = new double[count - 3];
double[] d = new double[count - 2];
b[0] = q(x[1]) * Math.Pow(h, 2) - 2;
c[0] = (1 + p(x[1]) * h / 2);
d[0] = f(x[1]) * Math.Pow(h, 2) - (1 - p(x[1]) * h / 2) * res[0];
for (int i = 1; i < count - 3; i++)
{
a[i-1] = 1 - p(x[i + 1]) * h / 2; // нижняя диаг
c[i] = 1 + p(x[i + 1]) * h / 2; // верхняя диаг
b[i] = q(x[i + 1]) * Math.Pow(h, 2) - 2; // главная диаг
d[i] = f(x[i + 1]) * Math.Pow(h, 2); // свободные чл
}
a[count - 4] = 1 - p(x[count-2]) * h / 2;
b[count - 3] = q(x[count - 2]) * Math.Pow(h, 2) - 2;
d[count - 3] = f(x[count - 2]) * Math.Pow(h, 2) - (1 + p(x[count-2]) * h / 2) * res[count-1];
double[] res1 = TridiagonalMatrixAlgorithm(a, b, c, d);
for (int i = 0; i < count-2; i++)
{
res[i+1] = res1[i];
}
for (int i = 0; i < count; i++)
{
Console.WriteLine("{0}\t{1}\t{2}\t{3}", x[i], y(x[i]), res[i], Math.Abs(y(x[i])-res[i]));
}
Console.WriteLine("END");
}
}
}