Ladies_Man

#ARCH_Archi Original

Jan 23rd, 2014
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2.  
  3. #include <math.h>
  4. #include <locale.h>
  5. #include <stdio.h>
  6.  
  7. double f( double x ) {    
  8.     return //Function to integrate
  9. }
  10.  
  11. double F( double x ) {    
  12.     return //AntiDerivative
  13. }
  14.  
  15.  
  16.  
  17. double Integral( double Left, double Right, long N, double (*func)(double) )
  18. {
  19.     double x, dx, res = 0.0;
  20.     dx = (Right - Left) / N;
  21.  
  22.     for ( x = Left; x < Right; x += dx) {
  23.        
  24.     res += (func(x) + func(x + dx)) / 2;     //Trapezoidal rule
  25.     }
  26.     res *= dx;
  27.     return res;
  28. }
  29.  
  30.  
  31.  
  32. int main ()
  33. {
  34.     long n;
  35.     double L = , R = ;            //Area
  36.     double V, V0 = F(R) – F(L);       //Precise result (Newton - Leibniz)
  37.     setlocale ( LC_ALL, "");
  38.     fprintf (file, "Num of steps;Relative Mistake;Evaluation of Mistake\n");
  39.  
  40.     for (n = 1; n < 100; n += n/50 + 1) {    //Num of steps
  41.         V = Integral( L, R, n, f );              
  42.        
  43.     fprintf (file, "%ld;=%.15G;=%.15G\n", n, (V-V0)/V0,               //Relative Error (15 digits for double)
  44.         //        (((R-L)*(R-L)*(R-L)*(pow(10, 10)))/(12.*n*n))/V0 );     //Error of trapezoidal rule
  45.     }
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment