#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 make_spline(int n, double h, double A, double B, double C){ y[0] = 1; y[n] = exp(1); boost::scoped_array alpha (new double[n]); boost::scoped_array 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; }