OLW: #Integral #Calculus Approximation

This handy #Python #One-liner can help you impress your high school cousin who is just starting to learn the calculus that you have forgotten years ago! This uses the mid-point rectangle approximation method. You can specify the number of rectangles to use in order to improve accuracy.

def integrate(function, n=1000, start=0, stop=100): return (float(stop-start)/n) * sum([eval(function) for x in [start+((i+0.5)*float(stop-start)/n) for i in range(n)]])

Use it as follows:

integrate(‘sin(x)’,1000,0,1) is equivalent to 

“Never under any circumstances take a sleeping pill and a laxative on the same night.”


One thought on “OLW: #Integral #Calculus Approximation

  1. […] OLW: #Integral #Calculus Approximation […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s