Thread: Numerical Integration

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    slovenia
    Posts
    25

    Question Numerical Integration

    CODE:

    Code:
    #define Pi 3.14159265  // PI equivalent
    #define DIF 0.001      //differencial size
    
    double IntegralCharge(double Time);
    
    int main()
    {
    	IntegralCharge(0.3164);
    	
    	return 0;
    }
    
    double IntegralCharge(double Time)
    {
    	double Q1=0.0, Q2=0.0, Q=0.0, mid=0.0;
    	double temp=0.0;
    
    	for ( temp = 0.0 ; temp < Time ; temp += DIF); //this is the "integration" part
    	{                                                                        //it's just some stupid equation for
    		Q1  = (10 * sin(100*Pi*temp));                 //charge in electrodynamics
    		Q2  = (10 * sin(100*Pi*(temp+DIF)));       //but doesn't even matter.
    		mid = ((Q1+Q2) / 2);                                 //The final SUM ( Q ) is completely
    		Q  += (mid * DIF);                                     //wrong, when I check the result 
    	}                                                                       //in analytic integration. Any idea?
    /*
    	for (temp = 0.0 ; temp < 23 ; temp += DIF)   //this is just some stupid easy test
    	{                                                                     // equation f(x)=x to see if the method
    		Q1  = temp;                                            // is at least somewhat correct, and it is
    		Q2  = temp + DIF;                                  // as long as DIF isn't "too" big
    		mid = ((Q1 + Q2) / 2);                            // I'm guessing that in most cases                   
    		Q   += (mid * DIF);                                 // the problem is that the computer 
    	}                                                                    // makes bigger and bigger mistake in 
    		*/                                                           //calculations as the numbers grow larger
    	printf("%lf\n",Q);                                          //just a guess thow..
    }
    So as you see most of my problem is in the comments, if you know what the cause of the problem is, let me know, if there are any parts that aren't clear, again let me know.

    Thanks

  2. #2
    Registered User
    Join Date
    Mar 2008
    Location
    slovenia
    Posts
    25
    woww the comments aren't sopose to look like that, I lined them up when I made the thread, upss

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    slovenia
    Posts
    25
    Again I just keep on seeing mistakes I made, the DIF part was meant to say " DIF isn't too SMALL".

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Is this supposed to be the midpoint method? Because that means you want to evaluate your function at temp+0.5*dif, not find (f(x)+f(x+dif))/2.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do not mix tabs and spaces, unless you know what you are doing (and even then...).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Mar 2008
    Location
    slovenia
    Posts
    25
    I do not totaly understand what you mean, but my plan is to take 2 values main fuction is Q(temp) so the first value: Q1(temp) and second: Q2(temp + dif) the dif is their "X axis" difference I sum them up and divide them by 2 to make the middle point and then I multiply it by their X axis difference the DIF, so that gives me the size of the area constrained by two sides (mid and DIF) and that's basically the whole process, then sum them up all together an viola WRONG NUMBER :P, I probaly said a bunch of things you already know, but I don't know what you know so... you know :P

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well: first you really do need to know what numerical integration method you want to use. You should pick one: left-hand Riemann sum, right-hand Riemann sum, midpoint method, Simpson's rule, or something more complicated. Once you've chosen one, then we can talk about implementing it.

    Further on the guess that you think you want the midpoint method:
    If Q is your function, and dx is your delta-x, then the midpoint method sums up things like
    Q(x_k + dx/2). You are trying to add up (Q(x_k)+Q(x_k+dx))/2. The midpoint is x, not Q.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Location
    slovenia
    Posts
    25
    Lets simplify it... I sure do agree that I'm not very clear on the N.I. part, but that's not really my point, that little algorithm of mine, is very simple as you see, I really don't see the problem in the calculation that you see, my confusion is the result part, how can it be so waay of.

    I'm trying to add up ((Q(x_k)+Q(x_k+dx))/2)--middle of both values * dx--their distance. ( I don't see a problem here, besides that it's aproximation is based on the size of dx).

  9. #9
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Code:
    	printf("&#37;lf\n",Q);                                          //just a guess thow..
    Try using the %f specifier not %lf, it might mangle up the Q parameter and show you way off results.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by xuftugulus View Post
    Code:
    	printf("%lf\n",Q);                                          //just a guess thow..
    Try using the %f specifier not %lf, it might mangle up the Q parameter and show you way off results.
    Also, make sure to #include <stdio.h> -- forgetting it is a great way to get garbage output from printf().

  11. #11
    Registered User
    Join Date
    Mar 2008
    Location
    slovenia
    Posts
    25
    did both of those, same result, my guess would be that there may be some concealed detail to the naked eye, or the more probable that the computer gets carried away while doing so many calculations, cuz I tried doing a for loop

    This is some wierd stuff:
    Code:
    for (i=0;i<1000000;i++)
    result+=1000000;
    the result is normal

    Now:
    Code:
    for (i=0;i<100000000;i++)
    {
    	Q+=1000000000.0;
    	if (i&#37;10000000==0)
    	printf("%lf\n",Q);    //it seems that cuz of this funct
    }
    the result is somenumbers.216421 (it has a decimal point), how can this be, and if you take away the printf funct the result is back to its normal result.

    This is something I ran along the way while trying a bunch of things.

  12. #12
    Registered User
    Join Date
    Mar 2008
    Location
    slovenia
    Posts
    25
    Code:
    	for (x=0.0 ; x < 5.0 ; x += DIF)
    	{
    		y1  = (pow(x,2)+2);
    		y2  = (pow((x+DIF),2)+2);
    		mid = (y1+y2)/2;
    		result += mid*DIF;
    	}
    this on the other hand works like a charm, 99.99999&#37; accurate result

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Ooh, now I remember, the trapezoidal rule. That matches what you're doing (although not very efficiently). And yes, the trapezoidal rule is exact on low-degree polynomials. I forget how many decimal digits you have with a double -- once you get above that you can't necessarily represent all integers.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by tabstop View Post
    I forget how many decimal digits you have with a double -- once you get above that you can't necessarily represent all integers.
    You get about 16-17 digits in a IEEE-754 64-bit float (which is what double is most on most machines). Which doesn't take that long to fill when you start with 10 digits in the first place.

    Floating point math is [NEARLY] ALWAYS an approximation.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-04-2009, 09:18 AM
  2. Numerical Integration by Trap Rule.
    By hexagons in forum C Programming
    Replies: 3
    Last Post: 03-25-2009, 10:12 AM
  3. Numerical System Transformations - Lecture
    By vurdlak in forum C++ Programming
    Replies: 2
    Last Post: 03-16-2006, 08:16 AM
  4. Integration
    By Sang-drax in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 01-03-2003, 04:08 AM
  5. Using fscanf for reading numerical data from text file
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-15-2002, 05:18 PM