# Thread: Trapezoidal Rule with a slight twist

1. ## Trapezoidal Rule with a slight twist

Hi all! Here's the link the the prompt for the question i'm working on:

Imgur: The most awesome images on the Internet

and here is the code I have so far

// Chapter 5, #17
// This program will approximate the area
// under the curve/

Code:
```#include <stdio.h>
#include <math.h>

double Trap (double A, double B, int N);

double G(double x);

double H (double a, double b, double n);

int main(void)

{

double A, B, N;
double Integral;

printf("Please Enter the Values for A:"); scanf("%lf", &A);
printf("\n");

printf("Please Enter the Values for B:"); scanf("%lf", &B);
printf("\n");

printf("Please Enter the Values for N:"); scanf("%lf", &N);
printf("\n");

Integral = (H(A, B, N)/2.0) * Trap(G(A), G(B), N);

printf("The approximate value of the area under this curve is:  " "%5.2f", Integral);
printf("\n");
printf("\n");

return 0;
}

double Trap (double A, double B, int N)
{

double i;
double sum = 0.0;

i = 1;
while (i <= (N - 1.0))
{
sum = pow(i, 2) * sin(i)  ;

i ++ ;

}

return (A + B + (2 * sum));

}

double G(double x)
{

return (pow(x, 2) * sin(x));

}

double H (double A, double B, double N)
{

return (B-A)/N ;

}```

So i'm having some trouble setting it up as well as with the prompt.

The prompt asks me to create a function Trap with a, b, n and F as inputs

However if I create a function F and call it for Trap then input a as F(a) and b as F(b) then why do I need F as an input for F?

Furthermore if I write F as a a parameter in F i assume I would write:

(A, B, N, (pow(a, 2)* sin(a)) + pow(b, 2)* sin(b)) so then why do I need inputs A and B in This case?

Also I'm sure there's something wrong with the way I actually wrote the sum of F(x), but can't figure it out yet. Will continue working on it for now,

Can anyone offer any advice? I would greatly appreciate it!! 2. I don't think your teacher wants or expects an answer where each mathematical function is it's own C function. These other functions besides Trap appear to be simple calculations that you should just do under trap. Especially h: a and b are the terminal x-coordinates on your x-axis. If you have these then you know how long h is.

Wait - now I got it - f(x) is supposed to be y so eventually you have the two b segments in the trapezoid.
b1 = y0 - x0
b2 = y1 - x1
then the area of the trapezoid is (b1 + b2) * (x1 - x0) * 0.5 3. Yeah H definitely doesn't need to be a function, but I saw no harm in it. I'll get rid of it for aesthetics.

What really confuses me is getting rid of the function G I made - If I remove, I think you're saying build under the code for Trap

so it would look like:
Code:
```double Trap (double A, double B, int N)
{

double i;
double sum = 0.0;

i = 1;
while (i <= (N - 1.0))
{
sum = sum + pow(i, 2) * sin(i)  ;

i = i +1 ;

}

G =

return (A + B + (2 * sum));

}``` 4. If it were me, I would write:
Code:
```typedef double (*CurveFunction)(double);
double trap(double a, double b, int n, CurveFunction f);```
Then you can call:
Code:
```for (n = 2; n <= 128; n *= 2)
printf("Approximate area under G curve (%d strips): %.03f \n", n, trap(0, 3.14159, n, &G));

for (n = 2; n <= 128; n *= 2)
printf("Approximate area under H curve (%d strips): %.03f \n", n, trap(-2.0, 2.0, n, &H));```
This allows you more flexibility. Call f(x) under trap to get the y part of the graph no matter which curve it is.

Other than that I'm not sure how to integrate, I think you just sum the area of each strip which is by definition a trapezoid. 5. Huh, wow thanks for opening my eyes.

I think this will be really helpful.

Thank you very much! Popular pages Recent additions c_programming, double, homework help, printfn;, tutoring 