Simpson revival
09-28-2016, 10:00 PM (This post was last modified: 09-28-2016 10:46 PM by Dieter.)
Post: #6
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: Simpson revival
(09-28-2016 03:50 PM)Namir Wrote:  Version 1 gave better results. These results seem counter intuitive, since I expected version 2 to give a better final result.

Any or all improvements and corrections are welcome.

I am not sure how your code works in detail, but here is an example to compare with.

Edit: I think I now know where your error is. The improved result is calculated from the previous (W) and current (Z) Simpson approximation. But in your second program W is replaced with the last improved approximation, cf. line 160. So the next improved value is calculated from the the previous improved value and the current Simpson approximation. Here seems to be the problem. You must not overwrite W. Leave it as it is and only print/display the improved value.
Or, even better, calculate the previous and current improved approximations WI and ZI and stop the iteration as soon as these agree within the specified tolerance.

Finally, here is my example:
Let f(x) = 1/x and integrate it from 1 to 2.
The exact result, rounded to 12 digits, is ln(2)=0,693147180560.

Code:
   n      Standard Simpson    Error        Improved            Error  ----------------------------------------------------------------------    2      0,694444444444      1,3 E-03       4      0,693253968254      1,1 E-04     0,693174603175      2,7 E-05    8      0,693154530655      7,4 E-06     0,693147901481      7,2 E-07   16      0,693147652819      4,7 E-07     0,693147194297      1,4 E-08   32      0,693147210290      3,0 E-08     0,693147180788      2,3 E-10   64      0,693147182421      1,9 E-09     0,693147180564      3,6 E-12  128      0,693147180676      1,2 E-10     0,693147180560      5,6 E-14

The "improved" column holds the extrapolated results, i.e. [16*Simpson(a, b, n) – Simpson(a, b, n/2)] / 15.
The "Error" columns show the absolute error of the calculated integrals.
You can see that the "improved" values are roughly as accurate as the standard Simpson results with twice the number of intervals, and sometimes even better.
From step to step the error of the standard method improves by a factor approaching 16, while it's finally factor 64 for the improved method.

Here is the VBA code I used to calculate Simpson(1, 2, n):

Code:
Function Simpson(a, b, n)    h = (b - a) / n    k = 4    s = 0    For i = 1 To n - 1       s = s + k * f(a + i * h)       k = 6 - k    Next    Simpson = (f(a) + s + f(b)) * h / 3 End Function Function f(x)    f = 1 / x End Function

What results do you get?

Dieter
 « Next Oldest | Next Newest »

 Messages In This Thread Simpson revival - Pekis - 09-28-2016, 08:04 AM RE: Simpson revival - Namir - 09-28-2016, 10:40 AM RE: Simpson revival - Dieter - 09-28-2016, 12:25 PM RE: Simpson revival - Namir - 09-28-2016, 02:00 PM RE: Simpson revival - Namir - 09-28-2016, 03:50 PM RE: Simpson revival - Dieter - 09-28-2016 10:00 PM RE: Simpson revival - Albert Chan - 07-31-2018, 02:57 PM RE: Simpson revival - Namir - 09-29-2016, 04:44 PM RE: Simpson revival - Dieter - 09-29-2016, 06:23 PM RE: Simpson revival - Namir - 09-29-2016, 10:27 PM RE: Simpson revival - Dieter - 09-30-2016, 06:00 AM RE: Simpson revival - Dieter - 10-02-2016, 03:29 PM RE: Simpson revival - Namir - 10-02-2016, 04:48 PM RE: Simpson revival - Namir - 10-09-2016, 03:14 AM RE: Simpson revival - Albert Chan - 08-04-2018, 05:29 PM

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