Advertisement
Jgug

der_int_v2

May 23rd, 2012
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 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.     cout<<"k-1 der1y:";
  57.     printf("%10.10f\n",derY1[k-1]);
  58.     derAppY1[0]=-(3*Y[0]-4*Y[1]+Y[2])/(2*h); //nachalo intervala
  59.     derAppY1[k]=(Y[k-2]-4*Y[k-1]+3*Y[k])/(2*h); //konecs intervala
  60.     for (i=1; i<k; i++)
  61.     {
  62.         derAppY1[i]=(Y[i+1]-Y[i-1])/(2*h);
  63.     }
  64.  
  65.     //nachojdenie vtoroi proizvodnoi
  66.     for (i=0; i<=k; i++)
  67.     {
  68.         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);
  69.     }
  70.     for (i=1; i<k; i++)
  71.     {
  72.         derAppY2[i]=(Y[i-1]-2*Y[i]+Y[i+1])/pow(h,2);
  73.     }
  74.     //pogreshnost'
  75.     for (i=0; i<=k; i++)
  76.     {
  77.         pogr1[i]=fabs(fabs(derY1[i])-fabs(derAppY1[i]));
  78.         pogr2[i]=fabs(fabs(derY2[i])-fabs(derAppY2[i]));
  79.     }
  80.     //vivod tablicsi
  81.     printf("X\tY\tder1Y\tderA1Y\tpogr1\tder2Y\tderA2Y\tpogr2\n");
  82.     for (i=0; i<=k; i++)
  83.     {
  84.         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]);
  85.     }
  86. //=========================================================|
  87. //==Integrirovanie--fynkcsii===============================|
  88. //=========================================================|
  89.     //formyla_srednich
  90.     m=20;
  91.     do
  92.     {
  93.         inSredn=inTrap=0;
  94.         H=double(b-a)/m;
  95.         for (i=0; i<m; i++)
  96.         {
  97.             inX[i]=a+i*H;
  98.             inY[i]=exp(inX[i])/pow(inX[i],3)-pow(sin(inX[i]),3);
  99.         }
  100.         for (i=1; i<=m; i++)
  101.         {
  102.             inSredn+=H*((inY[i]+inY[i-1])/2);
  103.         }
  104.         inTrap=H*(inY[0]+inY[m])/2;
  105.         for (i=1; i<m-1; i++)
  106.         {
  107.             inTrap+=H*inY[i];
  108.         }
  109.         //cout<<inTrap<<"  "<<inSredn;
  110.         m=2*m;
  111.         cout<<inSredn<<" "<<inTrap<<endl;
  112.         acc=fabs(fabs(inSredn)-fabs(inTrap));
  113.     }
  114.     while(acc>=delta);
  115.     cout<<"================================="<<endl;
  116.     cout<<m<<"  "<<H<<endl;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement