## You are here

Homecode for Simpson's rule

## Primary tabs

# code for Simpson’s rule

Python code for Simpson’s rule

\PMlinkescapetext{ 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...$.

Related:

NewtonAndCotesFormulas

Type of Math Object:

Algorithm

Major Section:

Reference

Parent:

Groups audience:

## Mathematics Subject Classification

65D32*no label found*

- Forums
- Planetary Bugs
- HS/Secondary
- University/Tertiary
- Graduate/Advanced
- Industry/Practice
- Research Topics
- LaTeX help
- Math Comptetitions
- Math History
- Math Humor
- PlanetMath Comments
- PlanetMath System Updates and News
- PlanetMath help
- PlanetMath.ORG
- Strategic Communications Development
- The Math Pub
- Testing messages (ignore)

- Other useful stuff

## Recent Activity

Apr 22

new question: Prove that for any sets A, B, and C, An(BUC)=(AnB)U(AnC) by St_Louis

Apr 20

new image: information-theoretic-distributed-measurement-dds.png by rspuzio

new image: information-theoretic-distributed-measurement-4.2 by rspuzio

new image: information-theoretic-distributed-measurement-4.1 by rspuzio

new image: information-theoretic-distributed-measurement-3.2 by rspuzio

new image: information-theoretic-distributed-measurement-3.1 by rspuzio

new image: information-theoretic-distributed-measurement-2.1 by rspuzio

Apr 19

new collection: On the Information-Theoretic Structure of Distributed Measurements by rspuzio

Apr 15

new question: Prove a formula is part of the Gentzen System by LadyAnne

Mar 30

new question: A problem about Euler's totient function by mbhatia

new question: Prove that for any sets A, B, and C, An(BUC)=(AnB)U(AnC) by St_Louis

Apr 20

new image: information-theoretic-distributed-measurement-dds.png by rspuzio

new image: information-theoretic-distributed-measurement-4.2 by rspuzio

new image: information-theoretic-distributed-measurement-4.1 by rspuzio

new image: information-theoretic-distributed-measurement-3.2 by rspuzio

new image: information-theoretic-distributed-measurement-3.1 by rspuzio

new image: information-theoretic-distributed-measurement-2.1 by rspuzio

Apr 19

new collection: On the Information-Theoretic Structure of Distributed Measurements by rspuzio

Apr 15

new question: Prove a formula is part of the Gentzen System by LadyAnne

Mar 30

new question: A problem about Euler's totient function by mbhatia

## Comments

## codes for algorithms

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

## Re: codes for algorithms

Not a bad idea to add code.

I'm a little skeptical about your "actual value" for the integral;

the integral can be done algebraically to get the "actual" actual value.

## Re: codes for algorithms

I agree the actual value is not the actual vallue I should correct that

f

G -----> H G

p \ /_ ----- ~ f(G)

\ / f ker f

G/ker f

## disabling autolinks on an entry?

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

## Re: disabling autolinks on an entry?

Perhaps \PMlinkescapetext the whole entry, just take care with formatting instructions (formatting commands render only when set outside of \PMlinkescapetext.

--

"Do not meddle in the affairs of wizards for they are subtle and quick to anger."

## Re: disabling autolinks on an entry?

yes, that's the problem

I need to use verbatim environments, which stop the rendering of autolinks

so when viewwing the rendering, not only a link is NOT created, but also the commands that would create the link are inserted and displayed mixed with the code

and I can't use verbatim INSIDE the pm-pseudo latex commands as stated in the documentation

f

G -----> H G

p \ /_ ----- ~ f(G)

\ / f ker f

G/ker f

## Re: disabling autolinks on an entry?

Can you use link controlling within verbatim? Like when you want something in italics not to be linked you write

\textit{foo \PMlinkescapetext{bar}}

and it works.

--

"Do not meddle in the affairs of wizards for they are subtle and quick to anger."

## Re: disabling autolinks on an entry?

Yes, I just tried it. It should work by just writing

\begin{verbatim}

\PMlinkescapetext{foo

bar

}

\end{verbatim}

--

"Do not meddle in the affairs of wizards for they are subtle and quick to anger."

## Re: disabling autolinks on an entry?

it ALMOST works, except by showing the PMlinkescape command as if it were part of the code (i.e. not hidden during rendering)

check the entry on which this thread is attached to see

f

G -----> H G

p \ /_ ----- ~ f(G)

\ / f ker f

G/ker f

## Re: disabling autolinks on an entry?

I tried your source and it seems to work if you replace \PMlinkescape by \PMlinkescapetext.

--

"Do not meddle in the affairs of wizards for they are subtle and quick to anger."

## Re: disabling autolinks on an entry?

my god, I didn't notice the mistake, thanks

f

G -----> H G

p \ /_ ----- ~ f(G)

\ / f ker f

G/ker f