Advertisement
Jgug

Der_Int_v4

May 23rd, 2012
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.65 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, delta, Y[100], derY1[100], derY2[100], derApp1Y[100], derApp2Y[100], pogr1[100], pogr2[100], inSredn, inTrap, inX[100], inY[100], inYsr[100], inXsr[100] ;
  8. int i, j, k, step, tochnost;
  9. long int m;
  10. int a=4;
  11. int b=7;
  12. //======================================================|
  13.  
  14. double Function(double x)
  15. {
  16.     double F=0;
  17.     F=exp(x)/pow(x,3)-pow(sin(x),3);
  18.     return F;
  19. }
  20.  
  21. double Derivative1(double x)
  22. {
  23.     double k=(b-a)/h;
  24.     double FD1;
  25.     FD1=(exp(x)*(x-3)-3*pow(x,4)*pow(sin(x),2)*cos(x))/pow(x,4);
  26.     return FD1;
  27. }
  28.  
  29. double Derivative2(double x)
  30. {
  31.     double k=(b-a)/h;
  32.     double FD2;
  33.     FD2=(3*pow(x,5)*pow(sin(x),3)-6*pow(x,5)*sin(x)*pow(cos(x),2)+exp(x)*(pow(x,2)-6*x+12))/pow(x,5);
  34.     return FD2;
  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>>step;
  42.     switch (step)
  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 tochnost' integrirovaniya: \n"<<"1) delta=0.1\n"<<"2) delta=0.01\n"<<"3) delta=0.001\n";
  55.     cin>>tochnost;
  56.     switch (tochnost)
  57.     {
  58.     case 1:
  59.         delta=0.1; break;
  60.     case 2:
  61.         delta=0.01; break;
  62.     case 3:
  63.         delta=0.001; 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]=Function(x);
  74.     }
  75.  
  76.     //__1-ya__proizvodnaya_______________________________|
  77.     for (i=0; i<=k; i++)
  78.     {
  79.         double x=a+h*i;
  80.         derY1[i]=Derivative1(x);
  81.     }
  82.  
  83.     derApp1Y[0]=-(3*Y[0]-4*Y[1]+Y[2])/(2*h); //nachalo intervala
  84.     derApp1Y[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.         derApp1Y[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.         derY2[i]=Derivative2(x);
  96.     }
  97.    
  98.     for (i=1; i<k; i++)
  99.     {
  100.         derApp2Y[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.         pogr1[i]=fabs(fabs(derY1[i])-fabs(derApp1Y[i]));
  107.         pogr2[i]=fabs(fabs(derY2[i])-fabs(derApp2Y[i]));
  108.     }
  109.  
  110.     //__Vivod__Tablicsi___________________________________|
  111.     cout<<"______________________________________________________________"<<endl;
  112.     printf("X\tY\tder1Y\tderA1Y\tpogr1\tder2Y\tderA2Y\tpogr2\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],derY1[i],derApp1Y[i],pogr1[i],derY2[i],derApp2Y[i],pogr2[i]);
  117.     }
  118.  
  119. //========================================================|
  120. //=Integrirovanie--fynkcsii===============================|
  121. //========================================================|
  122.     m=2;
  123.     do
  124.     {
  125.         inSredn=inTrap=0;
  126.         H=double(b-a)/m;
  127.         double x1, x2;
  128.         for (i=1; i<=m; i++)
  129.         {
  130.             x1=a+H*(i-0.5);
  131.             inSredn+=H*Function(x1);
  132.         }
  133.  
  134.         inTrap=H*((Function(a)+Function(b))/2);
  135.         for (i=1; i<m-1; i++)
  136.         {
  137.             x2=a+H*i;
  138.             inTrap+=H*Function(x2);
  139.         }
  140.         m=2*m;
  141.         acc=fabs(inTrap-inSredn);
  142. //      cout<<"Sredn="<<inSredn<<"  "<<"Trap="<<inTrap<<endl;
  143.     }
  144.     while (acc>=delta);
  145.     double inFinal=(inTrap+2*inSredn)/3;
  146.     double pogr=fabs(6.118-inFinal);
  147. //  cout<<m<<endl;
  148.     cout<<"______________________________________________________________"<<endl;
  149.     cout<<"Znachenie interala= "<<inFinal<<endl;
  150.     cout<<"Tochnoe znachenie integrala ravno 6.118"<<endl;
  151.     cout<<"Pogreshnost'= "<<pogr<<endl;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement