Like Tree1Likes
  • 1 Post By stahta01

Need help with C++ integration problem

This is a discussion on Need help with C++ integration problem within the C++ Programming forums, part of the General Programming Boards category; I have an assignment where I have to use the trapezoidal and simpson's rules to estimate the integral of a ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    Need help with C++ integration problem

    I have an assignment where I have to use the trapezoidal and simpson's rules to estimate the integral of a function. Everything works as far as I can tell, except for the final results. If someone could help me out, it would be much appreciated.

    Code:
    #include<stdio.h>
    #include<math.h>
    #include<stdlib.h>
    #include<conio.h>
    
    //Declare global variables
    double A, B, x=0, n1, n2;
    char ch;
    
    //Function to evaluate both of the functions
    double evalf(double x){
           if(ch == 'a')
              return(sin((2*x)/5) - x + 1);
           else
              return(cos((2*x)/5) + (3*x) + 1);
           }
    
    //Function to input the number of intervals wanted for the Trapezoidal rule,
    //and see if their accuracy is acceptable.
    double trap_intervals(){
       double accuracy=0, error=1, h, n;
       while(error > accuracy){
          printf("\nPlease enter the number of subintervals you would like: ");
          scanf("%lf", &n);
          printf("Please enter the accuracy you would like: ");
          scanf("%lf", &accuracy);
          h = (B-A)/n;
          error = ((B-A)*(h*h)*sin(2*B/5))/75;
          if(error < 0)
             error = -error;
          if(error > accuracy)
             printf("\nSorry, you do not have enough subintervals for the level of accuracy.");
          printf("\nYour error is %lf.\n", error);
       }
       return(n);
    }
    
    //Function to estimate the integral of the function using the Trapezoidal Rule
    double trapezoid(){
           double X, Y, p, h, x, sum;
           h = (B-A)/n1;
           X = evalf(A);
           Y = evalf(B);
           for(x=(A+h); x<B; x+=h){
              p = evalf(x);
              sum+=p;
           }
           return(h*((1/2)*X + sum + (1/2)*Y));
    }
    
    //Function to input the number of intervals wanted for Simpson's rule, and see
    //if their accuracy is acceptable.       
    double simp_intervals(){
       double accuracy = 0, error = 1, h, n;
       while(error > accuracy){
          printf("\nPlease enter the number of subintervals you would like: ");
          scanf("%lf", &n);
          printf("Please enter the accuracy you would like: ");
          scanf("%lf", &accuracy);      
          h = (B-A)/n;
          error = (-4*(B-A)*(h*h*h*h)*sin(2*B/5))/28125;
          if(error<0)
             error= -error;
          if(error > accuracy)
             printf("\nSorry, you do not have enough subintervals for the level of accuracy.");
          printf("\nYour error is %lf.\n", error);
       }
       return(n);
    }     
    
    //Function to estimate the integral of the function using Simpson's Rule
    double simpson(){
           double X, Y, x, p, q, h, sum1, sum2;
           int i;
           h = (B-A)/n2;
           X = evalf(A);
           Y = evalf(B);
           for(x=A+h; x<B; x+=h){
            i = 1;
            if(i%2 == 0){
               p = 2*evalf(x);
               sum1+=p;
               }
            else{
               q = 4*evalf(x);
               sum2+=q;
               }
            i++;
            }
            return((h/3)*(X + sum1 + sum2 + Y));
    }
               
    //Main function starts here.
    int main(){
        
       double temp;
    
    //Decide which function you wish to integrate
       printf("If you want to integrate the function f(x) = sin(2x/5) - x + 1, press 'a'.");
       printf("\nIf you want to integrate the function f(x) = cos(2x/5) + 3x + 1, press 'b'.\n");
       scanf("%c", &ch);
       
    //Ensure the user only picks a or b
       while(ch != 'a' && ch != 'b'){
          printf("\nSorry, invalid input. Please try again:\n");
          scanf("%c", &ch);
       }
    
    //Tell the user which function will be integrated
       if(ch == 'a')
          printf("\nThe function we are integrating is f(x) = sin(2x/5) - x + 1.\n");
       else
          printf("\nThe function we are integrating is f(x) = cos(2x/5) + 3x + 1.\n");
    
    //Choose the intervals, swap A and B if A is greater than B
       printf("Please enter the values of the interval [A,B].\nA: ");
       scanf("%lf", &A);
       printf("\nB: ");
       scanf("%lf", &B);
       if(A>B){
          printf("\nA is larger than B. Swapping values now...");
          B = temp;
          A = B;
          temp = A;
          temp = 0;
       }
    
    //Pick intervals for Trapezoidal Rule
       printf("\nWe must now enter the number of intervals for the trapezoidal rule.");
       n1 = trap_intervals();
    
    //Pick intervals for Simpson's Rule
       printf("\nWe must now enter the number of intervals for Simpsons rule.");
       n2 = simp_intervals();
    
    //Estimate the value of the integration using the Trapezoidal Rule
       temp = trapezoid();
       printf("\nThe value of integration using the trapezoidal rule is %lf.", temp);
       temp = 0;
    
    //Estimate the value of the integration using Simpson's Rule
       temp = simpson();
       printf("\nThe value of integration using Simpson's rule is %lf.", temp);
    
    //End program
       printf("\n\nPress any key to finish...");
       getch();
    }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    Basically the problem is in the trapezoid and simpson functions, sorry for pasting the whole code.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    What C++? All I see is C!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,521
    First, learn to write code without unnecessary Global Variables.
    Second, learn to swap two value correctly.
    Third, learn to post C code in the C sub-forum.

    Code:
       if(A>B){
          printf("\nA is larger than B. Swapping values now...");
          B = temp;
          A = B;
          temp = A;
          temp = 0;
       }
    Tim S.
    Elysia likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Integration with Web?
    By dluthcke in forum C++ Programming
    Replies: 25
    Last Post: 09-30-2009, 12:02 AM
  2. Integration Problem
    By Tarento in forum C Programming
    Replies: 5
    Last Post: 05-09-2006, 12:48 AM
  3. Integration problem
    By HAssan in forum C Programming
    Replies: 4
    Last Post: 01-23-2006, 02:07 AM
  4. Problem with Monte Carlo(integration by rejection)
    By dionys in forum C Programming
    Replies: 9
    Last Post: 04-07-2004, 09:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21