1. ## 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. woww the comments aren't sopose to look like that, I lined them up when I made the thread, upss

3. Again I just keep on seeing mistakes I made, the DIF part was meant to say " DIF isn't too SMALL".

4. 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. Do not mix tabs and spaces, unless you know what you are doing (and even then...).

6. 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. 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. 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. 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.

10. Originally Posted by xuftugulus
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. 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. 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. 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. Originally Posted by tabstop
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