|
|
|
|
code for Simpson's rule
|
(Algorithm)
|
|
|
Python code for Simpson's rule
from math import *
def f(x):
#function to integrate
return sin(x)
def simpson_rule(a,b):
#Approximation by Simpson's rule
c=(a+b)/2.0
h=abs(b-a)/2.0
return h*(f(a)+4.0*f(c)+f(b))/3.0
# Calculates integral of f(x) from 0 to 1
print simpson_rule(0,1)
Integrating $\sin x$ from $0$ to $1$ with the previous code gives $0.45986218971...$ whereas the true value is $1-\cos 1 = 0.459697694131860282599063392557...$ .
|
"code for Simpson's rule" is owned by drini. [ owner history (1) ]
|
|
(view preamble | get metadata)
Cross-references: code
This is version 4 of code for Simpson's rule, born on 2004-11-23, modified 2004-12-27.
Object id is 6518, canonical name is CodeForSimpsonsRule.
Accessed 8342 times total.
Classification:
| AMS MSC: | 65D32 (Numerical analysis :: Numerical approximation and computational geometry :: Quadrature and cubature formulas) |
|
|
|
|
|
|
Pending Errata and Addenda
|
|
|
|
|
|
|
|
|
|
|