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 rulec=(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 1print simpson_rule(0,1)}
Integrating from to with the previous code gives whereas the true value is .