рабочая контра

 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
90
91
92
93
#include <iostream>
#include <cmath>
#include <boost/scoped_array.hpp>
#include <boost/optional.hpp>
#include <iomanip>
double f(double x){
return 2 * exp(x);
}
double abs(double x) {return(x < 0) ? -x : x;}
int n = 10;
boost::scoped_array<double> x(new double [n + 1]);
boost::scoped_array<double> y(new double [n + 1]);
void solve(int n, double h, double A, double B, double C, double p, double q){
boost::scoped_array<double> alpha (new double[n]);
boost::scoped_array<double> beta (new double[n]);
boost::scoped_array<double> D (new double[n + 1]);
double Z;
for (int i = 0; i < n; i++){
D[i] = f(x[i])*h*h;
}
D[1] -= (1 - p * h / 2)*y[0];
D[n] -= (1 + p * h / 2)*y[n];
alpha[1] = -C / B;
beta[1] = D[1] / B;
//====================== МЕТОД ПРОГОНКИ =======================================
for (int i = 1; i < n; i++){
Z = (A * alpha[i - 1] + B);
alpha[i] = -C / Z;
beta[i] = (D[i] - A * beta[i - 1]) / Z;
}
y[n - 1] = beta[n - 1];
for (int i = n - 1; i > 0; i--)
y[i] = alpha[i] * y[i + 1]+ beta[i];
//=============================================================================
}
void make_table(int n, double h, double a, double b){
x[0] = a;
x[n] = b;
for (int i = 1; i < n; i++){
x[i] = a + (b - a) / n * i;
y[i] = 0;
}
}
int main(){
double A, B, C;
double begin = 0, end = 1;
double h = (end - begin)/n;
double p, q;
y[0] = 1.5;
y[n] = exp(1);
std::cout << std::setprecision(4);
std::cout << "P = ";
std::cin >> p;
std::cout << "Q = ";
std::cin >> q;
A = 1.0 - p*h/2.0;
B = q*h*h - 2.0;
C = 1.0 + p*h/2.0;
std::cout << std::endl << "На отрезке " << "[" << begin << "," << end << "]" << std::endl;
std::cout << "Количество разбиений n = " << n << std::endl;
std::cout << "Уравнение: y\"(x) + " << p << "y\'(x) + " << q << "y(x) = 2*exp(x)" << std::endl << std::endl;
make_table(n, h, begin, end);
solve(n, h, A, B, C, p, q);
for(int i = 0; i < n + 1; i++){
std::cout << "y(" << x[i] << ") = " << y[i] << "\t" << abs(y[i] - exp(x[i])) << std::endl;
}
return 0;
}