Контра - норм погрешности

 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
#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 make_spline(int n, double h, double A, double B, double C){
y[0] = 1;
y[n] = exp(1);
boost::scoped_array<double> alpha (new double[n]);
boost::scoped_array<double> beta (new double[n]);
double D, Z;
for (int i = 0; i < n; i++){
alpha[i] = 0;
beta[i] = 0;
}
//====================== МЕТОД ПРОГОНКИ =======================================
for (int i = 1; i < n; i++){
//D = (3.0/h)*(f(x[i + 1]) - 2*f(x[i]) + f(x[i - 1]))*h*h;
D = h*h*f(x[i+1]);
Z = (A * alpha[i - 1] + B);
alpha[i] = -C / Z;
beta[i] = (D - A * beta[i - 1]) / Z;
}
D = (6.0/h)*(f(x[n]) - 2*f(x[n - 1]) + f(x[n - 2]));
y[n - 1] = (- D + A * beta[n - 2]) / (B + A * alpha[n - 2]);
for (int i = n - 2; 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;
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);
make_spline(n, h, A, B, C);
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;
}