Advertisement
Jgug

der_int_v3

May 23rd, 2012
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.55 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. 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. /*double Srednie (double x, int m)
  38. {
  39. //  double x;
  40.     double inSredn=0;
  41.     for (i=0; i<m; i++)
  42.     {
  43. //      x=a+H*(i-0.5);
  44.         inSredn+=H*Function(x);
  45.     }
  46.     return inSredn;
  47. }
  48.  
  49. double Trapecsii(double x, int m)
  50. {
  51. //  double x;
  52.     double inTrap=0;
  53.     for (i=0; i<m; i++)
  54.     {
  55. //      x=a+H*i;
  56.         inTrap+=H*Function(x);
  57.     }
  58.     return inTrap;
  59. }*/
  60.  
  61.  
  62. //======================================================|
  63. void main(void)
  64. {
  65.     cout<<"Viberite shag differencsirovaniya: \n"<<"1) h=0.2\n"<<"2) h=0.1\n"<<"3) h=0.05\n";
  66.     cin>>step;
  67.     switch (step)
  68.     {
  69.     case 1:
  70.         h=0.2; break;
  71.     case 2:
  72.         h=0.1; break;
  73.     case 3:
  74.         h=0.05; break;
  75.     default:
  76.         cout<<"Vibrano nevernoe znachenie!\n";
  77.     }
  78.  
  79.     cout<<"Viberite tochnost' defferencsirovaniya: \n"<<"1) delta=0.1\n"<<"2) delta=0.01\n"<<"3) delta=0.001\n";
  80.     cin>>tochnost;
  81.     switch (tochnost)
  82.     {
  83.     case 1:
  84.         delta=0.1; break;
  85.     case 2:
  86.         delta=0.01; break;
  87.     case 3:
  88.         delta=0.001; break;
  89.     default:
  90.         cout<<"Vibrano nevernoe znachenie!\n";
  91.     }
  92.  
  93. //--Differencsirovanie--fynkcsii------------------------|
  94.     k=(b-a)/h;
  95.     for (i=0; i<=k; i++)
  96.     {
  97.         double x=a+h*i;
  98.         Y[i]=Function(x);
  99.     }
  100.  
  101.     //__1-ya__proizvodnaya_______________________________|
  102.     for (i=0; i<=k; i++)
  103.     {
  104.         double x=a+h*i;
  105.         derY1[i]=Derivative1(x);
  106.     }
  107.  
  108.     derApp1Y[0]=-(3*Y[0]-4*Y[1]+Y[2])/(2*h); //nachalo intervala
  109.     derApp1Y[k]=(Y[k-2]-4*Y[k-1]+3*Y[k])/(2*h); //konecs intervala
  110.  
  111.     for (i=1; i<k; i++)
  112.     {
  113.         derApp1Y[i]=(Y[i+1]-Y[i-1])/(2*h);
  114.     }
  115.  
  116.     //__2-ya__proizvodnaya________________________________|
  117.     for (i=0; i<=k; i++)
  118.     {
  119.         double x=a+h*i;
  120.         derY2[i]=Derivative2(x);
  121.     }
  122.    
  123.     for (i=1; i<k; i++)
  124.     {
  125.         derApp2Y[i]=(Y[i-1]-2*Y[i]+Y[i+1])/pow(h,2);
  126.     }
  127.  
  128.     //__Pogreshnost'______________________________________|
  129.     for (i=0; i<=k; i++)
  130.     {
  131.         pogr1[i]=fabs(fabs(derY1[i])-fabs(derApp1Y[i]));
  132.         pogr2[i]=fabs(fabs(derY2[i])-fabs(derApp2Y[i]));
  133.     }
  134.  
  135.     //__Vivod__Tablicsi___________________________________|
  136.     printf("X\tY\tder1Y\tderA1Y\tpogr1\tder2Y\tderA2Y\tpogr2\n");
  137.     for (i=0; i<=k; i++)
  138.     {
  139.         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]);
  140.     }
  141.  
  142. //========================================================|
  143. //=Integrirovanie--fynkcsii===============================|
  144. //========================================================|
  145.     m=2;
  146.     do
  147.     {
  148.         inSredn=inTrap=0;
  149.         H=double(b-a)/m;
  150.         double x1, x2;
  151.         for (i=1; i<=m; i++)
  152.         {
  153.             x1=a+H*(i-0.5);
  154.             inSredn+=H*Function(x1);
  155.         }
  156.  
  157.         inTrap=H*((Function(a)+Function(b))/2);
  158.         for (i=1; i<m-1; i++)
  159.         {
  160.             x2=a+H*i;
  161.             inTrap+=H*Function(x2);
  162.         }
  163.         m=2*m;
  164.         acc=fabs(fabs(inTrap)-fabs(inSredn));
  165.         cout<<"Sredn="<<inSredn<<"  "<<"Trap="<<inTrap<<endl;
  166.     }
  167.     while (acc>=delta);
  168.     cout<<m<<endl;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement