05-30-2018, 05:53 PM (This post was last modified: 05-30-2018 06:51 PM by Dieter.)
Post: #2
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
(05-30-2018 01:39 PM)Namir Wrote:  I tested the above code with f(x)=x and f(x)=1/x and obtained good results.

Namir, I have tried your code in VBA for Excel, and it seems to work fine. But for f(x)=1/x (your example) something happens that I do not understand.

Let's assume a=1 and b=2 so that the true result is ln 2 = 0,693147180559945...

n = 10 => 0,693146401483427
n = 20 => 0,693147131766058
n = 30 => 0,693147170917894
n = 40 => 0,705569697497420 (!!)
n = 50 => 0,693147179310086
n = 60 => 0,701445982770560 (!!)
n = 70 => 0,700264648002835 (!!)
n = 80 => 0,699377730119543 (!!)
n = 90 => 0,698687360816317 (!!)
n=100 => 0,693147180481822

So the results first get more and more accurate as n increases, and the error is about 1/4 of a standard Simpson method.
But then some results are way off. For instance for n=10 and n=16 the results are fine. For n=12 and n=14 they are off.
Likewise n=20 and n=24 are fine, n=22 is not.

What's going on here?
Is there a special restriction for the value of n? You said it can be any positive value.

EDIT: I think I found the problem. It's the exit condition of the loop. Due to roundoff errors the >= test does not test true if "a" is sliiiiightly less than "Blast". Testing floating point values for equality always is a bad idea. Instead you should check if the difference is below a certain threshold. But this is not required here: After replacing the DO-loop with a FOR-loop everything works fine. I think you should adjust your program accordingly:

Replace the DO line with FOR I=1 TO N
Replace the LOOP UNTIL... line with NEXT I

After this adjustment the program seems to work fine.
For the example function the accuracy is comparable to a standard Simpson method with √2 n intervals.

Dieter
 « Next Oldest | Next Newest »

 Messages In This Thread New Quadratic Integration - Namir - 05-30-2018, 01:39 PM RE: New Quadratic Integration - Dieter - 05-30-2018 05:53 PM RE: New Quadratic Integration - Namir - 05-30-2018, 08:17 PM RE: New Quadratic Integration - Dieter - 05-30-2018, 09:24 PM RE: New Quadratic Integration - ttw - 05-31-2018, 02:27 AM RE: New Quadratic Integration - Namir - 05-31-2018, 03:46 AM RE: New Quadratic Integration - Namir - 05-31-2018, 03:51 AM RE: New Quadratic Integration - ttw - 05-31-2018, 03:56 AM

User(s) browsing this thread: 1 Guest(s)