Thread: Integration Problem

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    24

    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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    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.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    24
    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?

  5. #5
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    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.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with for loop calling external function
    By lordbubonicus in forum C Programming
    Replies: 2
    Last Post: 10-13-2007, 10:54 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM