Advertisement
Misipuk

NM_Lab8

Oct 1st, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. #include <iostream>
  2. #include "math.h"
  3. #include <fstream>
  4. #define H 0.001
  5. using namespace std;
  6.  
  7. double a0=1.3;
  8. double b0=1.6;
  9. double a1y,a2y;
  10. double c_;
  11. double k_;
  12. ofstream fout;
  13.  
  14. double y(double x){
  15.     return -0.538*x*x+0.598*x+0.531+1/(-2.723*x+0.876);
  16. }
  17.  
  18. double dy(double x){
  19.     return -1.076*x+0.367242/((0.321704-x)*(0.321704-x))+0.598;
  20. }
  21.  
  22. double ddy(double x){
  23.     return 14.829458/pow((-2.723*x+0.876),3)-1.076;
  24. }
  25.  
  26. double p(double x){
  27.     return 1.5;
  28. }
  29. double q(double x){
  30.     return -x;
  31. }
  32.  
  33. double f(double x){
  34.     return ddy(x)+p(x)*dy(x)+q(x)*y(x);
  35. }
  36.  
  37. double getAij(int i, int j){
  38.     int n=(int)((b0-a0)/H)+1;
  39.     if(i==0){
  40.         if(j==0)
  41.             return c_;
  42.  
  43.         if(j==1)
  44.             return 1;
  45.         else
  46.             return 0;
  47.     }
  48.     if(i==n-1){
  49.         if(j==n-2)
  50.             return 1;
  51.         if(j==n-1)
  52.             return k_;
  53.         else
  54.             return 0;
  55.     }
  56.     else{
  57.         if(j==(i-1))
  58.             return 1/(H*H)-p(a0+i*H)/(2*H);
  59.         if(j==i)
  60.             return q(a0+i*H)-2/(H*H);
  61.         if(j==(i+1))
  62.             return 1/(H*H)+p(a0+i*H)/(2*H);
  63.         else
  64.             return 0;
  65.     }
  66. }
  67.  
  68. void progonka(){
  69.     double e=0;
  70.     int n=(int)((b0-a0)/H)+1;
  71.     cout << n << endl;
  72.     double a[n];// Ay=b
  73.     double b[n];
  74.     double y_[n];
  75.     a[0]=-getAij(0,1)/getAij(0,0);
  76.     b[0]=f(a0)/getAij(0,0);
  77.     for(int i=1; i<n-1; i++){
  78.         a[i]=-getAij(i,i+1)/(getAij(i,i)+getAij(i,i-1)*a[i-1]);
  79.         b[i]=(-getAij(i,i-1)*b[i-1]+f(a0+H*i))/(getAij(i,i)+getAij(i,i-1)*a[i-1]);
  80.     }
  81.     b[n-1]=(-getAij(n-1,n-1-1)*b[n-1-1]+f(b0))/(getAij(n-1,n-1)+getAij(n-1,n-1-1)*a[n-1-1]);
  82.     y_[n-1]=b[n-1];//y
  83.     for(int i=n-2; i>=0; i--){
  84.         y_[i]=a[i]*y_[i+1]+b[i];
  85.     }
  86.     cout << " X    Ysr      Yi    |Ysr-Yi|" << endl;
  87.     for( int i=0; i<n; i++){
  88.         if(e<fabs(y_[i]-y(a0+i*H)))
  89.             e=fabs(y_[i]-y(a0+i*H));
  90.         fout << a0+i*H << " " << y_[i] << " " << y(a0+i*H) << " " << fabs(y_[i]-y(a0+i*H)) << endl;
  91.         cout << a0+i*H << " " << y_[i] << " " << y(a0+i*H) << " " << fabs(y_[i]-y(a0+i*H)) << endl;
  92.     }
  93.     cout << "max|ei|: " << e << endl;
  94.     fout << "max|ei|: " << e << endl;
  95. }
  96.  
  97. int main()
  98. {
  99.     cout << f(a0) << " " << y(a0) << " " << y(a0+H) << endl;
  100.     c_=(f(a0)-y(a0+H))/y(a0);
  101.     k_=(f(b0)-y(b0-H))/y(b0);
  102.     int n=(int)ceil((b0-a0)/H)+1;
  103.     a1y=y(a0)-0.5*dy(a0);
  104.     a2y=dy(b0);
  105.     fout.open("result.txt");
  106.     cout << "H: " << H << endl;
  107.     progonka();
  108.     fout.close();
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement