# The trapezoidal rule question

This is a discussion on The trapezoidal rule question within the C Programming forums, part of the General Programming Boards category; Hi, I can't figure out where is the problem.Please help me to correct my code Code: #include <stdio.h> #include <math.h> ...

1. ## The trapezoidal rule question

Hi,
Code:
```#include <stdio.h>
#include <math.h>
void trap(double a,double b, int n, double *areag, double *areah);
double g(double x);
double h(double x);
int main(void)
{
double  areag = 0.0, areah = 0.0;
double a1 = 0, b1 = 10;
int n;
for(n=2;n<=128;n*=2){
trap(a1, b1, n, &areag, &areah);
printf("%f %f\n", areag, areah);
}
return(0);
}

double g(double x){
return(pow(x,2)*sin(x));
}
double h(double x){
return(sqrt(4-pow(x,2)));
}
void trap(double a,double b, int n, double *areag, double *areah){
int i, l;
*areag = (b-a)/2*n * ( g(a) + g(b));
for(i = 1; i<=n-1;i++)
*areag += 2*g(i);
*areah = (b-a)/2*n * ( h(a) + h(b));
for(l=1;l<=n-1;l++)
*areah += 2*h(i);
}```

2. How about you tell us what it's doing wrong and what you expect it to do for you...

You need to read this --> http://www.cplusplus.com/forum/articles/1295/

A couple of quick observations...
g and h are really lousy names for functions... choose something that describes the function's behaviour, not then next letter in the alphabet.
You don't actually need prototypes for your g and h functions at the top of the program since they are both "seen before called".

3. Thanks for your advices.If you build and run the code, you will see it doesn't work properly.This question from a book(Problem solving and program design in C).It gives 2 equation and wants find approximating area under a curve.And adds call traps with values for n of 2, 4,8, 16, 32, 64, 128.
Output of my code is negative and -nan.
Equations are:
g(x) = x^2sinx (a = 0, b = 3.14159)
h(x) = sqrt(4-pow(x.2)) ( a =-2, b=2);

4. For future references, rarely someone will have the time to build the code and debug it for you. To obtain better help, you should post up the errors and output of the program.

Code:
```for(l=1;l<=n-1;l++)
*areah += 2*h(i);```
why is it h(i) when it's 'l' in the for loop?

the trouble is g(x) = sqrt(4-pow(x,2))
Code:
```for(n=2;n<=128;n*=2){
trap(a1, b1, n, &areag, &areah);
///  as n increase, pow(n,2) is getting large
// inside trap.
for(i = 1; i<=n-1;i++)
*areag += 2*g(i);```
when n is large enough 4-pow(x,2) is a negative number.

5. I see.I will do what you said.
Solved the problem;
The problem was in trap function :
Code:
```void trap(double a,double b, int n, double *areag, double *areah){
int i, l;
*areag = (b-a)/2*n * ( g(a) + g(b));
for(i = 1; i<=n-1;i++)
*areag += 2*g( (double)i / n);
*areah = (b-a)/2*n * ( h(a) + h(b));
for(l=1;l<=n-1;l++)
*areah += 2*h( (double) l / n);
}```

6. OK you still have several errors there. Here are my suggestions and what I would do:

1; Don't pass the areas just call the function and return the area.

2; evaluate the stepping size first then pass it to evaluation function because it's easier if you want to do simples 1/3, 3/8 or bools rule if you decided to do them.

3; turn (b-a)/2*n * ( g(a) + g(b)) to stepping size * (evaluate left interval + evaluateright interval) / 2.0L to get the first and last pieces evaluated.

4; oh yeah I suggest you use long doubles because it gives a greater precision.

5; before the for loop get the first stepping size for your g() or h().evaluation function. [left interval + stepping size]

6; in the for loop you need to pass the next stepping into g() or h() evaluation function not left and right interval and add the return area to the total sum, and then increase the stepping size.

7; return the sum.

7. you misunderstood the interior points of traperzoid rule.

The for loop should be:
Code:
```for( something with i)
*areag += (b-a)/n*g( a+ i*(b-a)/n)```
or define the step-size like tripleA suggested
Code:
```double h = (b-a)/n;
for( something in here)
*area += h*g(a+i*h);```

8. Originally Posted by nimitzhunter
you misunderstood the interior points of traperzoid rule.

The for loop should be:
Code:
```for( something with i)
*areag += (b-a)/n*g( a+ i*(b-a)/n)```
or define the step-size like tripleA suggested
Code:
```double h = (b-a)/n;
for( something in here)
*area += h*g(a+i*h);```
To keep it efficient you don't want to do all those multiplication over and over again in the for loop. The h multiplication in my suggestion can be taken out and do it after the loop.

And my previous point #2 is wrong, the multiplication of the stepping size should be taken out from the equation and applies to to the total area before returning it.

9. Yeah, i'ts more efficient that way, but he already calculated the end point. I just want to give him something that work without altering his program( that is up to him.)

Code:
```#include <stdio.h>
#include <math.h>
void trap(double a,double b, int n, double *areag, double *areah);
double g(double x);
double h(double x);
int main(void)
{
double  areag = 0.0, areah = 0.0;
double a1 = 0, b1 = 3;
int n;
for(n=2;n<128;n*=2){
trap(a1, b1, n, &areag, &areah);
printf("%f %f\n", areag, areah);
}
return(0);
}

double g(double x){
return(pow(x,2)*sin(x));
}
double h(double x){
return(sqrt(fabs(4-pow(x,2))));
}
void trap(double a,double b, int n, double *areag, double *areah){
int i, l;
*areag = ((b-a)/2)*n * ( g(a) + g(b));
for(i = 1; i<=n-1;i++)
*areag += (b-a)/n*g( a+ i*(b-a)/n);
*areah = (b-a)/2*n * ( h(a) + h(b));
for(l=1;l<=n-1;l++)
*areah += (b-a)/n*h(( a+ l*(b-a)/n));
}```