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.