Advertisement
Jgug

Der_Int_Gayss2

May 24th, 2012
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. #include <iostream.h>
  2. #include <iomanip.h>
  3. #include <math.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. double h, acc, H, Y[100], dY1[100], dY2[100], dA1Y[100], dA2Y[100], pog1[100], pog2[100], inY1, inY2, Int, pogInt;
  8. int i, k, shag, otrezki;
  9. int m;
  10. int a=5;
  11. int b=8;
  12. //======================================================|
  13.  
  14. double Fun(double x)
  15. {
  16.     double F=0;
  17.     F=pow(x,0.5)-pow(cos(x),2);
  18.     return F;
  19. }
  20.  
  21. double D1(double x)
  22. {
  23.     double k=(b-a)/h;
  24.     double Diff1;
  25.     Diff1=1/(2*pow(x,0.5))+sin(2*x);
  26.     return Diff1;
  27. }
  28.  
  29. double D2(double x)
  30. {
  31.     double k=(b-a)/h;
  32.     double Diff2;
  33.     Diff2=2*cos(2*x)-1/(4*pow(x,1.5));
  34.     return Diff2;
  35. }
  36.  
  37. //======================================================|
  38. void main(void)
  39. {
  40.     cout<<"Viberite shag differencsirovaniya: \n"<<"1) h=0.2\n"<<"2) h=0.1\n"<<"3) h=0.05\n";
  41.     cin>>shag;
  42.     switch (shag)
  43.     {
  44.     case 1:
  45.         h=0.2; break;
  46.     case 2:
  47.         h=0.1; break;
  48.     case 3:
  49.         h=0.05; break;
  50.     default:
  51.         cout<<"Vibrano nevernoe znachenie!\n";
  52.     }
  53.  
  54.     cout<<"Viberite chislo shagov integrirovaniya: \n"<<"1) m=10\n"<<"2) m=20\n"<<"3) m=40\n";
  55.     cin>>otrezki;
  56.     switch (otrezki)
  57.     {
  58.     case 1:
  59.         m=10; break;
  60.     case 2:
  61.         m=20; break;
  62.     case 3:
  63.         m=40; break;
  64.     default:
  65.         cout<<"Vibrano nevernoe znachenie!\n";
  66.     }
  67.  
  68. //==Differencsirovanie--fynkcsii========================|
  69.     k=(b-a)/h;
  70.     for (i=0; i<=k; i++)
  71.     {
  72.         double x=a+h*i;
  73.         Y[i]=Fun(x);
  74.     }
  75.  
  76.     //__1-ya__proizvodnaya__|
  77.     for (i=0; i<=k; i++)
  78.     {
  79.         double x=a+h*i;
  80.         dY1[i]=D1(x);
  81.     }
  82.  
  83.     dA1Y[0]=-(3*Y[0]-4*Y[1]+Y[2])/(2*h); //nachalo intervala
  84.     dA1Y[k]=(Y[k-2]-4*Y[k-1]+3*Y[k])/(2*h); //konecs intervala
  85.  
  86.     for (i=1; i<k; i++)
  87.     {
  88.         dA1Y[i]=(Y[i+1]-Y[i-1])/(2*h);
  89.     }
  90.  
  91.     //__2-ya__proizvodnaya__|
  92.     for (i=0; i<=k; i++)
  93.     {
  94.         double x=a+h*i;
  95.         dY2[i]=D2(x);
  96.     }
  97.    
  98.     for (i=1; i<k; i++)
  99.     {
  100.         dA2Y[i]=(Y[i-1]-2*Y[i]+Y[i+1])/pow(h,2);
  101.     }
  102.  
  103.     //__Pogreshnost'__|
  104.     for (i=0; i<=k; i++)
  105.     {
  106.         pog1[i]=fabs(dY1[i]-dA1Y[i]);
  107.         pog2[i]=fabs(dY2[i]-dA2Y[i]);
  108.     }
  109.  
  110.     //__Vivod__Tablicsi__|
  111.     cout<<"=============================================================="<<endl;
  112.     printf("X\tY\td1Y\tdA1Y\tpog1\td2Y\tdA2Y\tpog2\n");
  113.     cout<<"______________________________________________________________"<<endl;
  114.     for (i=0; i<=k; i++)
  115.     {
  116.         printf("%2.2f\t%4.3f\t%4.3f\t%4.3f\t%5.4f\t%4.3f\t%4.3f\t%5.4f\n",a+i*h,Y[i],dY1[i],dA1Y[i],pog1[i],dY2[i],dA2Y[i],pog2[i]);
  117.     }
  118.  
  119. //=Integrirovanie--fynkcsii===============================|
  120.  
  121.     inY1=inY2=0;
  122.     H=double(b-a)/m;
  123.     double x1, x2;
  124.     for (i=1; i<=m; i++)
  125.     {
  126.         x1=a+H*(i-0.5)+(h/2)*0.5773502692;
  127.         inY1+=(H/2)*Fun(x1);
  128.     }
  129.     for (i=1; i<=m; i++)
  130.     {
  131.         x2=a+H*(i-0.5)-(h/2)*0.5773502692;
  132.         inY1+=(H/2)*Fun(x2);
  133.     }
  134.     Int=inY1+inY2;
  135.     pogInt=fabs(6.067-Int);
  136.     cout<<"=============================================================="<<endl;
  137.     cout<<"Znachenie integrala="<<Int<<endl;
  138.     cout<<"Tochnoe znachenie integrala=6.067"<<endl;
  139.     cout<<"=============================================================="<<endl;
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement