#include #include #include #include #include double f(double x){ return 2 * exp(x); } double abs(double x) {return(x < 0) ? -x : x;} int n = 10; boost::scoped_array x(new double [n + 1]); boost::scoped_array y(new double [n + 1]); void solve(int n, double h, double A, double B, double C, double p, double q){ boost::scoped_array alpha (new double[n]); boost::scoped_array beta (new double[n]); boost::scoped_array 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; }