Advertisement
Jgug

Der-Int

May 22nd, 2012
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.89 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, H, acc,  delta, X[100], Y[100], derY1[100], derY2[100], derAppY1[100], derAppY2[100], pogr1[100], pogr2[100], inSredn, inTrap, inX[100], inY[100];
  8. int i, j, k, m, step, tochnost;
  9.  
  10. void main(void)
  11. {
  12.     const a=4;
  13.     const b=7;
  14.    
  15.     cout<<"Viberite shag differencsirovaniya: \n"<<"1) h=0.2\n"<<"2) h=0.1\n"<<"3) h=0.05\n";
  16.     cin>>step;
  17.     switch (step)
  18.     {
  19.     case 1:
  20.         h=0.2; break;
  21.     case 2:
  22.         h=0.1; break;
  23.     case 3:
  24.         h=0.05; break;
  25.     default:
  26.         cout<<"Vibrano nevernoe znachenie!\n";
  27.     }
  28.     cout<<"Viberite tochnost' defferencsirovaniya: \n"<<"1) delta=0.1\n"<<"2) delta=0.01\n"<<"3) delta=0.001\n";
  29.     cin>>tochnost;
  30.     switch (tochnost)
  31.     {
  32.     case 1:
  33.         delta=0.1; break;
  34.     case 2:
  35.         delta=0.01; break;
  36.     case 3:
  37.         delta=0.001; break;
  38.     default:
  39.         cout<<"Vibrano nevernoe znachenie!\n";
  40.     }
  41. //=========================================================|
  42. //--Differencsirovanie--fynkcsii---------------------------|
  43. //=========================================================|
  44.     k=(b-a)/h; //kol-vo otrezkov
  45.     for (i=0; i<=k; i++)
  46.     {
  47.         X[i]=a+i*h;
  48.         Y[i]=exp(X[i])/pow(X[i],3)-pow(sin(X[i]),3);
  49.     }
  50.  
  51.     //nachojdenie pervoi proizvodnoi
  52.     for (i=0; i<=k; i++)
  53.     {
  54.         derY1[i]=(exp(X[i])*(X[i]-3)-3*pow(X[i],4)*pow(sin(X[i]),2)*cos(X[i]))/pow(X[i],4);
  55.     }
  56.     derAppY1[0]=-(3*Y[0]-4*Y[1]+Y[2])/(2*h); //nachalo intervala
  57.     derAppY1[k]=(Y[k-2]-4*Y[k-1]+3*Y[k])/(2*h); //konecs intervala
  58.     for (i=1; i<=k; i++)
  59.     {
  60.         derAppY1[i]=(Y[i+1]-Y[i-2])/(2*h);
  61.     }
  62.  
  63.     //nachojdenie vtoroi proizvodnoi
  64.     for (i=0; i<=k; i++)
  65.     {
  66.         derY2[i]=(3*pow(X[i],5)*pow(sin(X[i]),3)-6*pow(X[i],5)*sin(X[i])*pow(cos(X[i]),2)+exp(X[i])*(pow(X[i],2)-6*X[i]+12))/pow(X[i],5);
  67.     }
  68.     for (i=1; i<k; i++)
  69.     {
  70.         derAppY2[i]=(Y[i-1]-2*Y[i]+Y[i+1])/(h*h);
  71.     }
  72.     //pogreshnost'
  73.     for (i=0; i<=k; i++)
  74.     {
  75.         pogr1[i]=fabs(fabs(derY1[i])-fabs(derAppY1[i]));
  76.         pogr2[i]=fabs(fabs(derY2[i])-fabs(derAppY2[i]));
  77.     }
  78.     //vivod tablicsi
  79.     printf("X\tY\tdY\tDY\tpogr\td2Y\tD2Y\tpogr\n");
  80.     for (i=0; i<=k; i++)
  81.     {
  82.         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",X[i],Y[i],derY1[i],derAppY1[i],pogr1[i],derY2[i],derAppY2[i],pogr2[i]);
  83.     }
  84. //=========================================================|
  85. //==Integrirovanie--fynkcsii===============================|
  86. //=========================================================|
  87.     //formyla_srednich
  88.     m=20;
  89.     inSredn=inTrap=0;
  90.     do
  91.     {
  92.         H=double(b-a)/m;
  93.         for (i=0; i<m; i++)
  94.         {
  95.             inX[i]=a+i*H;
  96.             inY[i]=exp(inX[i])/pow(inX[i],3)-pow(sin(inX[i]),3);
  97.         }
  98.         for (i=1; i<=m; i++)
  99.         {
  100.             inSredn+=H*((inY[i]+inY[i-1])/2);
  101.         }
  102.         inTrap=H*(inY[0]+inY[m])/2;
  103.         for (i=1; i<m-1; i++)
  104.         {
  105.             inTrap+=H*inY[i];
  106.         }
  107.         //cout<<inTrap<<"  "<<inSredn;
  108.         m=2*m;
  109.         cout<<inSredn<<" "<<inTrap<<endl;
  110.         acc=fabs(fabs(inSredn)-fabs(inTrap));
  111.     }
  112.     while(acc>=delta);
  113.     cout<<m<<"  "<<H<<endl;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement