Advertisement
Misipuk

NM_Lab7

Oct 4th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <math.h>
  5.  
  6. using namespace std;
  7.  
  8. using std::setw;
  9. ofstream out;
  10. double H;
  11. double n;
  12.  
  13. double f(double x)
  14. {
  15.     return -sqrt(pow(x,2)*sqrt(12*pow(x,2)+9)-3*x*x)/sqrt(2);
  16. }
  17.  
  18. double df(double x, double y)
  19. {
  20.     return (3*pow(y,3)+6*y*x*x)/(2*x*y*y+3*pow(x,3));
  21. }
  22.  
  23. double RungeKuttaStep(double x0, double y0)
  24. {
  25.     double f1,f2,f3,f4;
  26.     f1=df(x0,y0);
  27.     f2=df(x0+H/2,y0+H/2*f1);
  28.     f3=df(x0+H/2,y0+H/2*f2);
  29.     f4=df(x0+H,y0+H*f3);
  30.     y0+=H/6*(f1+2*f2+2*f3+f4);
  31.     return y0;
  32. }
  33.  
  34. void RungeKutta(double x0, double y0)
  35. {
  36.     double e=0;
  37.     printf("x0 \t y0 \t f(x0) \t |y0-f(x0)|\n");
  38.     for(int i=0; i<1/H; i++){
  39.         y0 = RungeKuttaStep(x0,y0);
  40.         x0 += H;
  41.         out << x0 << " " << y0 << " " << f(x0) << " " << fabs(y0-f(x0)) << endl;
  42.         cout << setw(4) << x0 << " " << setw(8) << y0 << " " << setw(8) << f(x0) << " " << setw(13) << fabs(y0-f(x0)) << endl;
  43.         if(e<fabs(y0-f(x0)))
  44.            e=fabs(y0-f(x0));
  45.     }
  46.     out << "max|ei| " << e << endl;
  47.     cout << "max|ei| " << e << endl;
  48. }
  49.  
  50. void Adams(double x0, double y0)
  51. {
  52.     double k[4];
  53.     double e=0;
  54.     k[0]=df(x0,y0);
  55.     printf("x0 \t y0 \t f(x0) \t |y0-f(x0)|\n");
  56.     for(int i=1; i<4; i++){
  57.         y0=RungeKuttaStep(x0,y0);
  58.         x0+=H;
  59.         k[i]=df(x0,y0);
  60.         out << x0 << " " << y0 << " " << f(x0) << " " << fabs(y0-f(x0)) << endl;
  61.         cout << setw(4) << x0 << " " << setw(8) << y0 << " " << setw(8) << f(x0) << " " << setw(13) << fabs(y0-f(x0)) << endl;
  62.     }
  63.     for(int i=3; i<1/H; i++)
  64.     {
  65.         y0+=H/24*(55*k[3]-59*k[2]+37*k[1]-9*k[0]);
  66.         x0+=H;
  67.         for(int j=0; j<3; j++)
  68.         {
  69.             k[j]=k[j+1];
  70.         }
  71.         k[3]=df(x0,y0);
  72.         if(e<fabs(y0-f(x0)))
  73.            e=fabs(y0-f(x0));
  74.         out << x0 << " " << y0 << " " << f(x0) << " " << fabs(y0-f(x0)) << endl;
  75.         cout << setw(4) << x0 << " " << setw(8) << y0 << " " << setw(8) << f(x0) << " " << setw(13) << fabs(y0-f(x0)) << endl;
  76.     }
  77.     out << "max|ei| " << e << endl;
  78.     cout << "max|ei| " << e << endl;
  79. }
  80.  
  81. int main()
  82. {
  83.     n=1024;
  84.     out.open("Output.txt");
  85.     for(int i=1; i<11; i++){
  86.         H=1/n;
  87.         n/=2;
  88.         out << "H: " << H << endl;
  89.         cout << "R-K:" << endl;
  90.         RungeKutta(6,-18);
  91.         cout << "Adams:" << endl;
  92.         Adams(6,-18);
  93.         cout << "***************************************" << endl;
  94.     }
  95.     out.close();
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement