Integrating function using trapezoidal rule

This is a discussion on Integrating function using trapezoidal rule within the C Programming forums, part of the General Programming Boards category; Hi guys, can some one please help me with this program. I need to be able to integrade a function ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    15

    Integrating function using trapezoidal rule

    Hi guys, can some one please help me with this program. I need to be able to integrade a function but i seem to be having some trouble getting consistant result using the rule. more specifically im not getting consistant result for the sub intervals i am implementing. Im thinking that my for loop needs to be modified to make this work, but i dont know how... that is.. the intergration of y=x from 0 to 10 is 50, but as you can see from the output i only get that for the first interval mainly 10.

    Code:
    #include<stdio.h>
    #include<math.h>
    #define F(X) X
    int main(){
    int i,k1,k2;
    double a,b,h1,h2,sum=0,sum2=0,U1,U2,y;
    printf("Enter the lower limit:");
    scanf ("%lf",&a);
    printf("Enter the upper limit:");
    scanf("%lf",&b);
    
    k1=10;
    k2=20;
    printf("%5s %5s %12s %12s %12s\n","k1","k2","U1","U2","y");
    while(k1<=1250 && k2<=2500){
    h1=(b-a)/k1;
    for (i=1;i<=k1-1;i++){sum=sum+F(a+(i*h1)); }
    U1=(h1/2)*(F(a)+2*sum+F(b));
    
    h2=(b-a)/k2;
    for (i=1;i<=k2-1;i++){sum2=sum2+F(a+(i*h2)); }
    U2=(h2/2)*(F(a)+2*sum2+F(b));
    
    y=( (2*2*U2)/((2*2)-1) )-( U1/((2*2)-1) );
    
    printf("%5d %5d %12.6lf %12.6lf %12.6lf\n",k1,k2,U1,U2,y);
    
    k1+=(5*k1)-k1;
    k2+=(5*k2)-k2;
    
    }
    
    
    
    
    
    
    return 0;
    }
    can someone please help me.

    and sorry to the modes for not knowing the proper tags

  2. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    sorry just to clarify.. the out put shoudl be 50 for all sub intervals

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    never mind guys.. i needed to reinitialized sum and sum to back to 0 in the while.. thanks though

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trapezoidal Rule for Integration
    By lolcats22 in forum C Programming
    Replies: 2
    Last Post: 11-20-2011, 02:09 AM
  2. The trapezoidal rule question
    By paranoidgnu in forum C Programming
    Replies: 9
    Last Post: 04-24-2011, 10:00 AM
  3. Simpson's rule and Trapezoidal rule from fixed array
    By timwonderer in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2010, 03:14 PM
  4. composite trapezoidal rule
    By Sam Robinson in forum C Programming
    Replies: 0
    Last Post: 05-19-2003, 11:01 AM
  5. Arrays and the trapezoidal rule
    By ChemistryStar4 in forum C++ Programming
    Replies: 1
    Last Post: 04-05-2003, 09:16 PM

Tags for this Thread


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