Post Reply 
The most compact algorithm for Simpson's rule??
12-16-2015, 12:57 AM (This post was last modified: 12-16-2015 01:53 PM by Namir.)
Post: #15
RE: The most compact algorithm for Simpson's rule??
Here is a version that iterates one less time to calculate the integral:

Code:
Given function f(x), integration interval [A, B] and N divisions, where N is even.
To calculate the integral of f(x) for X=A to x=B using Simpson's Rule

h=(B-A)/N
Sum=f(A)+f(B)
C=4
Do
  A=A+h
  Sum = Sum + C*f(A)
  C=6-C
  N=N-1
Loop Until N=1
Area=h/3*Sum

The last iteration multiplies f(A) by 4 and leaves C with the value of 2. The initial value of Sum is initialized as f(A)+f(B). By controlling the loop with a counter, we can have a compact version that does not not do extra calculations.

Namir
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: The most compact algorithm for Simpson's rule?? - Namir - 12-16-2015 12:57 AM



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