Advertisement
Kostil_Uranio

spline

May 11th, 2021
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4. #include <iomanip>
  5. #define A 0
  6. #define B 1
  7. #include "sweep_method.h"
  8.  
  9. using namespace std;
  10.  
  11. double func(double x) {
  12.     return exp(x);
  13. }
  14.  
  15. void bound_cond(char c, double* arr_spl, int N, double step) {
  16.     if (c == 'a') {
  17.         arr_spl[0] = func(A);
  18.         arr_spl[N - 1] = func(B);
  19.     }
  20.    
  21.     if (c == 'b') {
  22.         arr_spl[0] = (1 - 2 * func(step) + func(2 * double (step))) / (pow(step, 2));
  23.         arr_spl[N - 1] = (func(1) - 2 * func(1 - double (step)) + func(1 - 2 * double (step))) / (pow(step, 2));
  24.     }
  25.    
  26.     if (c == 'c') {
  27.         arr_spl[0] = 0;
  28.         arr_spl[N - 1] = 0;
  29.     }
  30. }
  31.  
  32. double* help_spline(int N, char c) {
  33.     double step = (double(B - A) / (double(N) - 1));
  34.  
  35.     double A_sweep;
  36.     double B_sweep;
  37.     double C_sweep;
  38.  
  39.     A_sweep = B_sweep = (step / 6);
  40.     C_sweep = double(2 * step) / 3;
  41.     double* h_spline = new double[N];
  42.  
  43.     double* F_sweep = new double[N - 2];
  44.     bound_cond(c, h_spline, N, step);
  45.  
  46.     F_sweep[0] = (func(A + 2 * step) - 2 * func(A + step) + func(A)) / step - (step * h_spline[0]) / 6;
  47.     F_sweep[N - 3] = (func(B) - 2 * func(B - step) + func(B - 2 * step)) / step - (step * h_spline[N - 1]) / 6;
  48.  
  49.     for (int i = 2; i < N - 2; i++)
  50.         F_sweep[i - 1] = (func(A + (double(i) + 1) * step) - 2 * func(A + i * step) + func(A + (double(i) - 1) * step)) / step;
  51.  
  52.     double* ans = new double[N - 2];
  53.     ans = sweep_method(N - 2, A_sweep, B_sweep, C_sweep, F_sweep);
  54.  
  55.     for (int i = 1; i < N - 1; i++)
  56.         h_spline[i] = ans[i - 1];
  57.    
  58.     delete[] ans;
  59.     delete[] F_sweep;
  60.  
  61.  
  62.     return h_spline;
  63. }
  64.  
  65. double spline(double* h_spl, int N, double x) {
  66.     double step = (double (B - A) / double (double (N) - 1));
  67.     int count;
  68.     for (count = 1; count < N; count++)
  69.         if (x < step * count) break;
  70.     count = count - 1;
  71.     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));
  72. }
  73.  
  74.  
  75. void error(char cas, ofstream &fin, double eps) {
  76.     int count = 0;
  77.     int N = 10;
  78.     //double step = (double(B - A) / ((double(N) - 1) * 10));
  79.     //double step = (double(B - A) / (double (N)));
  80.     double step = (double(B - A) / ((double(N) - 1) * 10));
  81.     double err = 1;
  82.     double h_err;
  83.     double p;
  84.  
  85.     while (err > eps) {
  86.         err = 0;
  87.        
  88.         double* spl = new double[N];
  89.    
  90.         if (cas != 'i')
  91.             spl = help_spline(N, cas);
  92.         else {
  93.             spl = ind_h_spl(N);
  94.         }
  95.        
  96.  
  97.         for (double i = A; i <= B; i = i + step) {
  98.             if (abs(spline(spl, N, i) - func(i)) >= err)
  99.                 err = abs(spline(spl, N, i) - func(i));
  100.         }
  101.  
  102.         if (count != 0) {
  103.             p = log(h_err / err) / log(2);
  104.         }
  105.  
  106.         h_err = err;
  107.        
  108.         if (count != 0)
  109.             fin << "number of nodes: " << N << "    " << "error: " << setprecision(30) << err << "  error's order: " << p << endl;
  110.         else fin << "number of nodes: " << N << "    " << "error: " << setprecision(30) << err << " error's order: isn't defined " << endl;
  111.        
  112.        
  113.         N = 2 * N;
  114.         step = (double(B - A) / ((double(N) - 1) * 10));
  115.        
  116.         count++;
  117.         delete[] spl;
  118.     }
  119.  
  120. }
  121.  
  122. int main() {
  123.     double eps = 0.0000001;
  124.  
  125.     ofstream error_1("C:\\Users\\Сергей\\Desktop\\C++ ВМЛА\\comp_meth_spline\\error_spline_'a'.txt");
  126.     //error('a', error_1, eps);
  127.  
  128.     ofstream error_2("C:\\Users\\Сергей\\Desktop\\C++ ВМЛА\\comp_meth_spline\\error_spline_'b'.txt");
  129.     //error('b', error_2, eps);
  130.    
  131.     ofstream error_3("C:\\Users\\Сергей\\Desktop\\C++ ВМЛА\\comp_meth_spline\\error_spline_'c'.txt");
  132.     //error('c', error_3, eps);
  133.  
  134.     ofstream error_4("C:\\Users\\Сергей\\Desktop\\C++ ВМЛА\\comp_meth_spline\\error_spline_'i'.txt");
  135.     //error('i', error_4, eps);
  136.    
  137.     double* test = ind_h_spl(10);
  138.    
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement