riemann_sum.py

Created by ferr0fluidmann

Created on November 04, 2019

442 Bytes

A program for the rough integration of functions.


import math

def f(x):
  return x

def leftEndpoint(fx=f,a=0,b=1,n=5):
  out = 0
  x_step = (b-a)/n
  for i in range(n):
    out += x_step*fx(a+i*x_step)
  return out

def rightEndpoint(fx=f,a=0,b=1,n=5):
  out = 0
  x_step = (b-a)/n
  for i in range(n):
    out += x_step*fx(b-i*x_step)
  return out

def riemannSum(fx=f,a=0,b=1,n=5):
  le = leftEndpoint(fx, a, b, n)
  re = rightEndpoint(fx, a, b, n)
  return (le+re)/2.0