Advertisement
Misipuk

Untitled

Oct 4th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <math.h>
  5. #include <windows.h>
  6. using namespace std;
  7.  
  8.  
  9. using std::setw;
  10. ofstream out;
  11. double step=0.01;
  12. double n;
  13.  
  14.  
  15. double f(double x)
  16. {
  17.     return sqrt(pow(x,2)*sqrt(4*pow(x,2)+9)-3*x*x)/sqrt(2);
  18. }
  19.  
  20. double df(double x, double y)
  21. {
  22.     return (3*pow(y,3)+6*y*x*x)/(2*x*y*y+3*pow(x,3));
  23. }
  24.  
  25. double RK_Step(double x, double y)
  26. {
  27.     double f1,f2,f3,f4;
  28.     f1=df(x,y);
  29.     f2=df(x+step/2,y+step/2*f1);
  30.     f3=df(x+step/2,y+step/2*f2);
  31.     f4=df(x+step,y+step*f3);
  32.     y=y+ step/6*(f1+2*f2+2*f3+f4);
  33.     return y;
  34. }
  35.  
  36. void A_Method(double x, double y)
  37. {
  38.     double num_a[4];
  39.     double dlt=0;
  40.     num_a[0]=df(x,y);//y'
  41.     printf("x \t y \t f(x) \t |y-f(x)|\n");
  42.     for(int i=1; i<4; i++){//
  43.         y=RK_Step(x,y);//
  44.         x+=step;
  45.         num_a[i]=df(x,y);
  46.         out << x << " " << y << " " << f(x) << " " << fabs(y-f(x)) << endl;
  47.         cout << setw(4) << x << " " << setw(8) << y << " " << setw(8) << f(x) << " " << setw(13) << fabs(y-f(x)) << endl;
  48.     }// get 4 y'
  49.  
  50.     for(int i=3; i<1/step; i++)
  51.     {
  52.         y+=step/24*(55*num_a[3]-59*num_a[2]+37*num_a[1]-9*num_a[0]);
  53.         x+=step;
  54.         for(int j=0; j<3; j++)
  55.         {
  56.             num_a[j]=num_a[j+1];
  57.         }
  58.  
  59.         num_a[3]=df(x,y);//сдвигаем влево и обновляем последний
  60.         if(dlt<fabs(y-f(x))){
  61.             dlt=fabs(y-f(x));
  62.         }
  63.  
  64.         out << x << " " << y << " " << f(x) << " " << fabs(y-f(x)) << endl;
  65.         cout << setw(4) << x << " " << setw(8) << y << " " << setw(8) << f(x) << " " << setw(13) << fabs(y-f(x)) << endl;
  66.     }
  67.     out << "maximum delta " << dlt << endl;
  68.     cout << "maximum delta " << dlt << endl;
  69. }
  70.  
  71.  
  72. void RK(double x, double y)
  73. {
  74.     double dlt=0;
  75.     printf("x \t y \t f(x) \t |y-f(x)|\n");
  76.     for(int i=0; i<1/step; i++){
  77.         y = RK_Step(x,y);
  78.         x += step;
  79.         out << x << " " << y << " " << f(x) << " " << fabs(y-f(x)) << endl;
  80.         cout << setw(4) << x << " " << setw(8) << y << " " << setw(8) << f(x) << " " << setw(13) << fabs(y-f(x)) << endl;
  81.  
  82.         if(dlt<fabs(y-f(x))){
  83.              dlt=fabs(y-f(x));
  84.         }
  85.     }
  86.     out << "maximum delta " << dlt << endl;
  87.     cout << "maximum delta " << dlt << endl;
  88. }
  89.  
  90.  
  91.  
  92.  
  93.  
  94. int main()
  95. {
  96.     n=1024;
  97.     out.open("Output.txt");
  98.     for(int i=1; i<11; i++){
  99.         step=1/n;
  100.         n/=2;
  101.         out << "Step: " << step << endl;
  102.         cout << "Method R-K:" << endl;
  103.         RK(2,2);
  104.         cout << "*************************************" << endl;
  105.         cout << "A_Method:" << endl;
  106.         A_Method(2,2);
  107.     }
  108.     out.close();
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement