Kostil_Uranio

spline_1

May 12th, 2021
407
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //19142, Аношин Сергей, Интерполирование сплайном
  2. #include <iostream>
  3. #include <fstream>
  4. #include <cmath>
  5. #include <iomanip>
  6. #define A 0
  7. #define B 1
  8. #include "Прогонка.h"
  9.  
  10. using namespace std;
  11.  
  12. double func(double x) {
  13.     return exp(x);
  14. }
  15.  
  16. void bound_cond(char c, double* arr_spl, int N, double step) {
  17.     if (c == 'a') {
  18.         arr_spl[0] = func(A);
  19.         arr_spl[N - 1] = func(B);
  20.     }
  21.  
  22.     if (c == 'b') {
  23.         arr_spl[0] = (1 - 2 * func(step) + func(2 * step)) / (pow(step, 2));
  24.         arr_spl[N - 1] = (func(1) - 2 * func(1 - step) + func(1 - 2 * step)) / (pow(step, 2));
  25.     }
  26.  
  27.     if (c == 'c') {
  28.         arr_spl[0] = 0;
  29.         arr_spl[N - 1] = 0;
  30.     }
  31. }
  32.  
  33. double* ind_h_spl(int N) {
  34.     double step = (double(B - A) / (double(N) - 1));
  35.  
  36.     double A_sweep;
  37.     double B_sweep;
  38.     double C_sweep;
  39.  
  40.     A_sweep = B_sweep = (step / 6);
  41.     C_sweep = double(2 * step) / 3;
  42.     double* h_spline = new double[N];
  43.  
  44.  
  45.     h_spline[1] = (func(A + 2 * step) - 2 * func(A + step) + func(A)) / (pow(step, 2));
  46.     h_spline[N - 2] = (func(B) - 2 * func(B - step) + func(B - 2 * step)) / pow(step, 2);
  47.  
  48.     double* F_sweep_1 = new double[N - 4];
  49.  
  50.     F_sweep_1[0] = (func(A + 3 * step) - 2 * func(A + 2 * step) + func(A + step)) / step - ((step * h_spline[1]) / 6);
  51.     F_sweep_1[N - 5] = (func(B - step) - 2 * func(B - 2 * step) + func(B - 3 * step)) / step - (step * h_spline[N - 2]) / 6;
  52.     for (int i = 3; i < N - 3; i++)
  53.         F_sweep_1[i - 2] = (func(A + (double(i) + 1) * step) - 2 * func(A + i * step) + func(A + (double(i) - 1) * step)) / step;
  54.  
  55.     double* ans = new double[N - 4];
  56.     ans = sweep_method(N - 4, A_sweep, B_sweep, C_sweep, F_sweep_1);
  57.  
  58.     for (int i = 2; i < N - 2; i++)
  59.         h_spline[i] = ans[i - 2];
  60.  
  61.     h_spline[0] = 2 * h_spline[1] - h_spline[2];
  62.     h_spline[N - 1] = 2 * h_spline[N - 2] - h_spline[N - 3];
  63.  
  64.     delete[] ans;
  65.     delete[] F_sweep_1;
  66.  
  67.     return h_spline;
  68. }
  69.  
  70. double* help_spline(int N, char c) {
  71.     double step = (double(B - A) / (double(N) - 1));
  72.  
  73.     double A_sweep;
  74.     double B_sweep;
  75.     double C_sweep;
  76.  
  77.     A_sweep = B_sweep = (step / 6);
  78.     C_sweep = double(2 * step) / 3;
  79.     double* h_spline = new double[N];
  80.  
  81.     double* F_sweep = new double[N - 2];
  82.     bound_cond(c, h_spline, N, step);
  83.  
  84.     F_sweep[0] = (func(A + 2 * step) - 2 * func(A + step) + func(A)) / step - (step * h_spline[0]) / 6;
  85.     F_sweep[N - 3] = (func(B) - 2 * func(B - step) + func(B - 2 * step)) / step - (step * h_spline[N - 1]) / 6;
  86.  
  87.     for (int i = 2; i < N - 2; i++)
  88.         F_sweep[i - 1] = (func(A + (double(i) + 1) * step) - 2 * func(A + i * step) + func(A + (double(i) - 1) * step)) / step;
  89.  
  90.     double* ans = new double[N - 2];
  91.     ans = sweep_method(N - 2, A_sweep, B_sweep, C_sweep, F_sweep);
  92.  
  93.     for (int i = 1; i < N - 1; i++)
  94.         h_spline[i] = ans[i - 1];
  95.  
  96.     delete[] ans;
  97.     delete[] F_sweep;
  98.  
  99.  
  100.     return h_spline;
  101. }
  102.  
  103. double spline(double* h_spl, int N, double x) {
  104.     double step = (double(B - A) / double(double(N) - 1));
  105.     int count;
  106.     for (count = 1; count < N; count++)
  107.         if (x < step * count) break;
  108.     count = count - 1;
  109.     return (((exp(count * step) * ((double(count) + 1) * step - x)) / step) + ((exp((double(count) + 1) * step) * (x - count * step)) / step) + h_spl[count] * (pow(((double(count) + 1) * step - x), 3) - pow(step, 2) * ((double(count) + 1) * step - x)) / (6 * step) + h_spl[count + 1] * (pow((x - count * step), 3) - pow(step, 2) * (x - count * step)) / (6 * step));
  110. }
  111.  
  112. void error(char cas, ofstream& fin, double eps, int flag) {
  113.     int count = 0;
  114.     int N = 10;
  115.     double step = (double(B - A) / ((double(N) - 1) * 10));
  116.     double err = 1;
  117.     double h_err;
  118.     double p;
  119.  
  120.  
  121.     while (err > eps) {
  122.         err = 0;
  123.         double* spl = new double[N];
  124.  
  125.         if (cas != 'i')
  126.             spl = help_spline(N, cas);
  127.         else {
  128.             spl = ind_h_spl(N);
  129.         }
  130.  
  131.         for (double i = A; i <= B; i = i + step) {
  132.             if (abs(spline(spl, N, i) - func(i)) >= err)
  133.                 err = abs(spline(spl, N, i) - func(i));
  134.         }
  135.  
  136.         if (count != 0) {
  137.             p = log(h_err / err) / log(2);
  138.         }
  139.  
  140.         h_err = err;
  141.  
  142.         if (flag == 1) {
  143.             if (count != 0)
  144.                 fin << "number of nodes: " << N << "    " << "error: " << setprecision(30) << err << "  error's order: " << p << endl;
  145.             else fin << "number of nodes: " << N << "    " << "error: " << setprecision(30) << err << " error's order: isn't defined " << endl;
  146.             N = 2 * N;
  147.         }
  148.  
  149.         if (flag == 0) {
  150.             fin << N << "   " << setprecision(20) << log10(err) << endl;
  151.             N = 1 + N;
  152.         }
  153.  
  154.         step = (double(B - A) / ((double(N) - 1) * 10));
  155.  
  156.         count++;
  157.         delete[] spl;
  158.     }
  159.  
  160. }
  161.  
  162. int main() {
  163.     double eps = 0.0000000001;
  164.  
  165.     ofstream error_1("C:\\Users\\Сергей\\Desktop\\C++ ВМЛА\\Сплайн\\error_spline_'a'.txt");
  166.     error('a', error_1, eps, 1);
  167.  
  168.     ofstream error_2("C:\\Users\\Сергей\\Desktop\\C++ ВМЛА\\Сплайн\\error_spline_'b'.txt");
  169.     error('b', error_2, eps, 1);
  170.  
  171.     ofstream error_3("C:\\Users\\Сергей\\Desktop\\C++ ВМЛА\\Сплайн\\error_spline_'c'.txt");
  172.     error('c', error_3, eps, 1);
  173.  
  174.     ofstream error_4("C:\\Users\\Сергей\\Desktop\\C++ ВМЛА\\Сплайн\\error_spline_'i'.txt");
  175.     error('i', error_4, eps, 1);
  176.  
  177.     ofstream error_5("C:\\Users\\Сергей\\Desktop\\C++ ВМЛА\\Сплайн\\diagram_error_spline_'i'.txt");
  178.     error('i', error_5, eps, 0);
  179.  
  180.     ofstream error_6("C:\\Users\\Сергей\\Desktop\\C++ ВМЛА\\Сплайн\\diagram_error_spline_'a'.txt");
  181.     error('a', error_6, eps, 0);
  182.  
  183.     return 0;
  184. }
RAW Paste Data