Simpson's Rule Implementation trick?

03182016, 12:11 AM
(This post was last modified: 03212016 03:32 AM by Namir.)
Post: #1




Simpson's Rule Implementation trick?
I found C code in an article on the Internet that implements Simpson's rule using a single for loop. Here is the pseudocode for the C code:
Code:
Here is a version that I modified to avoid using a+i*h and a+(i+1)*h: Code:
Consolidating the for loop to have a single update to sum: Code: Simpson's Rule for Numerical Integration Ver 3 Enjoy! Namir 

03182016, 01:14 PM
(This post was last modified: 03182016 01:15 PM by Dieter.)
Post: #2




RE: Simpson Rule Implementation trick?
(03182016 12:11 AM)Namir Wrote: I found C code in an article on the Internet that implements Simpson's rule using a single for loop. Fine. But what's the improvement over another solution with a simple for loop as it has been discussed in December in this thread? Dieter 

03182016, 08:47 PM
Post: #3




RE: Simpron Rule Implementation trick?
By initializing the sum as:
Code: sum = f(a) + f(b) + 4*f(a+h) Instead of the usual expression: Code:
Code: for i=2 to n step 2 Or, borrowing from the old threads: Code: c = 2 

03182016, 11:15 PM
(This post was last modified: 03182016 11:16 PM by Vtile.)
Post: #4




RE: Simpron Rule Implementation trick?
Why Simpson rule, why not ie. rungekutta as isn't that the way when calculating numerical solution for integrals with high accuracy? I know must be rather idiotic question (since obviously it wouldn't discussed here if it would not have use), but need to ask to learn.


03182016, 11:53 PM
Post: #5




RE: Simpron Rule Implementation trick?
(03182016 11:15 PM)Vtile Wrote: Why Simpson rule, why not ie. rungekutta as isn't that the way when calculating numerical solution for integrals with high accuracy? I know must be rather idiotic question (since obviously it wouldn't discussed here if it would not have use), but need to ask to learn. The GaussKrnorod quadrature is among the best numerical integration methods. This thread just comments on a little trick that I noticed in an article that discusses basic numerical integration methods. The code that initializes the sum of terms caught my attention .... that's all. BTW, The RungeKutta methods are for solving ordinary differential equations. Namir 

03192016, 01:14 AM
Post: #6




RE: Simpron Rule Implementation trick?
I see, back to the books then.


03192016, 04:59 AM
Post: #7




RE: Simpron Rule Implementation trick?
Romberg's method is pretty good. It can be of arbitrarily high order if the integrand is smooth enough and can be adapted to giving error estimates.


03192016, 06:45 AM
Post: #8




RE: Simpron Rule Implementation trick?
(03192016 04:59 AM)ttw Wrote: Romberg's method is pretty good. It can be of arbitrarily high order if the integrand is smooth enough and can be adapted to giving error estimates. This is also possible with Simpson's method. Two approximations with n and 2n intervals give a new and improved estimate. Take a look a this thread in the HP41 software library. Dieter 

03192016, 06:50 AM
Post: #9




RE: Simpron Rule Implementation trick?
I'm a fan of the GaussKronrod quadratures. The 34S started with one of these for its integrate function. It wasn't adaptive and failed too often and people complained so I went back to a Romberg method.
For the 43s I'd like to switch back to an adaptive GK method. It should have the memory to handle it.  Pauli 

03192016, 02:21 PM
Post: #10




RE: Simpron Rule Implementation trick?
(03192016 06:45 AM)Dieter Wrote:(03192016 04:59 AM)ttw Wrote: Romberg's method is pretty good. It can be of arbitrarily high order if the integrand is smooth enough and can be adapted to giving error estimates. I have implemented a variant of the Romberg method that uses Simpson's rule to start with instead of teh Trapezoidal rule. You can find this variant method on my web site. Namir 

03192016, 02:22 PM
Post: #11




RE: Simpron Rule Implementation trick?
(03192016 06:50 AM)Paul Dale Wrote: I'm a fan of the GaussKronrod quadratures. The 34S started with one of these for its integrate function. It wasn't adaptive and failed too often and people complained so I went back to a Romberg method. I think the Integrate function of the HP34C and HP15C (and later models?) use a method based on Romberg's method. Namir 

03192016, 05:05 PM
(This post was last modified: 03192016 05:09 PM by Dieter.)
Post: #12




RE: Simpson Rule Implementation trick?
(03192016 02:21 PM)Namir Wrote: I have implemented a variant of the Romberg method that uses Simpson's rule to start with instead of teh Trapezoidal rule. You can find this variant method on my web site. Yes, I have seen this some time ago and I tried some of the VBA code (after some modifications for my older Excel version). Interesting results, the RombergSimpson method looks quite promising. However, on a calculator with limited ressources I prefer the Simpson method with additional extrapolation. Now the next step would be an improvement for periodic functions, i.e. something that uses nodes that are not evenly distributed. Such a method is also used in the original 34C Integrate method. BTW... Namir, could you please correct the typo in the subject of your initial post? The way it is written now ("Simpron") noone will be able to find this thread while searching the forum. Dieter 

« Next Oldest  Next Newest »

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