# Integration Problem

• 05-08-2006
Tarento
Integration Problem
Code:

```#include <stdio.h> #include <math.h> main() { double del_x, x, k, error, flag, f, sum, n_end; int n_trap, n1; printf("Part B:\n"); printf("Integration of f(x) = sinh(x) on [-1,2]\n\n"); do { printf("Enter the number of trapezoids:\n"); scanf("%d", &n_trap); printf("n_trap = %d      del_x =%g\n", n_trap, 3./n_trap); n1 = n_trap +1; x = 0.0; f = sinh(x); sum -= 0.5 * f; for(k=0; k<n1; k++) { x = (double)k * (3./n_trap); f = sinh(x); sum += f; } sum -= 0.5 * f; sum *= (3./n_trap); printf("Integral = %g\n", sum); error = 100*((cosh(2)-cosh(1)) - sum); printf("Relative Error = %g percent\n\n", error); printf("Do you want new n_trap: y=1/n=0\n"); scanf("%d", &flag); sum = 0; }while (flag ==1); }```
Sorry this one's so long. What I'm trying to do is integrate sinh(x) over the domain [-1, 2]. I SHOULD get that the Integral is 2.235734 when using TEN trapezoids, but instead I get this output.

Code:

```Part B: Integration of f(x) = sinh(x) on [-1,2] Enter the number of trapezoids: 10 n_trap = 10      del_x =0.3 Integral = -6.73572e+306 Relative Error = Inf percent Do you want new n_trap: y=1/n=0```
Someone PLEASE help me. I've been working on this for hours, and I still can't get it. :(
• 05-08-2006
Salem
Code:

```\$ gcc -W -Wall -ansi -pedantic -O2 foo.c foo.c:5: warning: return type defaults to `int' foo.c: In function `main': foo.c:35: warning: int format, double arg (arg 2) foo.c:7: warning: unused variable `del_x' foo.c:7: warning: unused variable `n_end' foo.c:38: warning: control reaches end of non-void function foo.c:7: warning: 'sum' might be used uninitialized in this function```
Pay attention to the 'sum' and 'printf' related messages.
• 05-08-2006
Richie T
You have a whole bunch of variables, some you don't use, all of
which are uninitialised. Making some basic changes and
improving layout gives the following:

Code:

```#include <stdio.h> #include <math.h> int main(void)        /*main returns int*/ {         /*initialise all of these*/         double del_x = 0.0, x = 0.0, k = 0.0, error = 0.0, flag = 0.0, f = 0.0, sum = 0.0, n_end = 0.0;         int n_trap = 0, n1 = 0;         printf("Part B:\n");         printf("Integration of f(x) = sinh(x) on [-1,2]\n\n");         do         {                 printf("Enter the number of trapezoids:\n");                 scanf("%d", &n_trap);                 printf("n_trap = %d      del_x =%g\n", n_trap, 3.0/n_trap);                 n1 = n_trap +1;                 x = 0.0;                 f = sinh(x);                 sum -= 0.5 * f;                 for(k=0; k<n1; k++)                 {                         x = (double)k * (3./n_trap);                         f = sinh(x);                         sum += f;                 }                 sum -= 0.5 * f;                 sum *= (3./n_trap);                 printf("Integral = %g\n", sum);                 error = 100*((cosh(2)-cosh(1)) - sum);                 printf("Relative Error = %g percent\n\n", error);                 printf("Do you want new n_trap: y=1/n=0\n");                 scanf("%d", &flag);                 sum = 0;         }while (flag == 1);         return 0; }```
This is given much more realistic answers but it is still not correct.
You are implementing the formula incorrectly, and who taught you
how to calculate percentage error? Generally its like this

Percentage error = ((absolute value(Actual - approx))/Actual) * 100

That should set you off in the correct direction. Look at the
trapezoidal formula again.
• 05-08-2006
Tarento
Yeah, it ain't working. My teacher insists the integral is correct, but I don't believe him. :( The integral looks correct, but maybe I'm not seeing something?
• 05-08-2006
Richie T
I could write code that did this correctly and explain it to you,
but you wouldn't benefit as much from that as what I'm about to
suggest - I googled for "trapezoidal rule" and this was the first
link that popped up - it explains each term in the equation.

My advice is to compare the formula to your approach, and if
it is mostly incorrect (i didn't check that it is, but it probably is),
scrap your current implementation and rewrite as close to the
formula as possible - this can be done using a for loop that
iterates for "the number trapezoids" times - a single statement
can evaluate the result for each trapezoid. Use sensible variable
names and the implementation should be easier to write.
• 05-09-2006
Salem
So start learning how to debug your own code.
Code:

```                for(k=0; k<n1; k++)                 {                         x = (double)k * (3./n_trap);                         f = sinh(x);                         sum += f;                         printf( "Loop %f: x=%f, f=%f, sum=%f\n", k, x, f, sum );                 }```
Does this output look OK, compared to what you worked out by hand?
If it does, move on to your next calculations.

Why is 'k' a floating point number, when used as a loop variable?