############ ############ This program uses simple integration methods to integrate a function. ############ ############ Written by Paul Batzing as an example for FYS2140 students. ############ ############ ############ DO NOT CHANGE ############ from math import * ############ ############ END OF DO NOT CHANGE ############ ############ ############ DO NOT CHANGE ############ Simple integrator that uses a sum of rectangles. ############ def simple(xmin, xmax, steps, function): x = xmin dx =(xmax-xmin)/( 1.0*steps) I = 0 for i in range(1,steps+1): I += dx*function(x) x += dx return I ############ ############ END OF DO NOT CHANGE. ############ ############ ############ DO NOT CHANGE ############ Integrator that uses Simpsons method. ############ def simpson(xmin, xmax, steps, function): x = xmin dx =(xmax-xmin)/( 1.0*steps) I = 0 for i in range(0,steps): I += dx/6.0 *(function(x) + 4*function(x+dx*0.5) + function(x+dx)) x += dx return I ############ ############ END OF DO NOT CHANGE. ############ ############ ############ Function to integrate, replace with the function of choice. ############ def f(x): return sin(x) ############ ############ End of function. ############ ############ ############ Write the results to the terminal. ############ print "Simpsons method with 10 steps gives: ",simpson(0,pi,10,f) print "While a simple riemann sum with 1000 steps gives: ",simple(0,pi,1000,f) ############ ############ End of write to terminal. ############