PlanetMath (more info)
 Math for the people, by the people. Sponsor PlanetMath
Encyclopedia | Requests | Forums | Docs | Wiki | Random | RSS  
Login
create new user
name:
pass:
forget your password?
Main Menu
Owner confidence rating: High Entry average rating: No information on entry rating
[parent] 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)

View style:

See Also: Newton and Cotes formulas


This object's parent.
Log in to rate this entry.
(view current ratings)

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 MSC65D32 (Numerical analysis :: Numerical approximation and computational geometry :: Quadrature and cubature formulas)

Pending Errata and Addenda
None.
Discussion
Style: Expand: Order:
forum policy
disabling autolinks on an entry? by drini on 2004-12-25 19:52:20

I guess I could fi x this entry by adding manual linkstops, but it will break again whenever a new link (say a new entry mentioning a term) is created, and I cannto be watching over this entry (and other code-related ones I intend to create) all the time.

so, any idea how to make autolinks not wok at all?
 f
G -----> H G
p \ /_ ----- ~ f(G)
 \ / f ker f
 G/ker f 
[ reply | up ]
codes for algorithms by drini on 2004-11-23 17:40:13

I thought it would be a nice idea having examples of coding some of algorithms in here, but instead of creating 20 pages for each major language, it'd be better to post all versions into a single page

I've written this one in python, but if anyone else wants to add some other languages, could be meerged with this entry. Just remember
*CLARITY and SIMPLICITY* over optimizations and language-specific tricks.

It's much better to have code which can be read and understood, even if it's not the most efficient way.

Ah, and about the bug of autolinking happeningg inside verbatim, I'm about to fill a bug report on that (I couldn't find a getaround)
 f
G -----> H G
p \ /_ ----- ~ f(G)
 \ / f ker f
 G/ker f 
[ reply | up ]

Interact
post | correct | update request | add example | add (any)