Calculate Area Under Gaussian Curve w/ Python Reimann Sum

  • Context: Python 
  • Thread starter Thread starter dacruick
  • Start date Start date
  • Tags Tags
    Python Sum
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 2K views
dacruick
Messages
1,038
Reaction score
1
Hi I have a program that is to calculate the area under the curve of a Gaussian distribution.
The integral from -inf to inf is 1, and my Reimann sum program calculates close to 1.5pi = numpy.arccos(-1)
a = 1 / (StandDev * pow(2*pi,0.5))
b = MPCAverage #MPC is the x-axis of my gaussian curve
c = StandDev

step = 0.01
IntervalSize = 1.27 #Irrelevant
x2 = 0
s = 0
while s < IntervalSize/step:
x1 = x2
x2 += step
y1 = a*numpy.exp(-(x1-b)**2/(c*c*2))
y2 = a*numpy.exp(-(x2-b)**2/(c*c*2))
Area += (step)*(y2+y1/2)
s += 1
print Area

any tips would be greatly appreciated. Sorry for the spacing error, it won't let me put them in.
 
Physics news on Phys.org
Nevermind!

Area += (step)*(y2+y1/2) = DumbMove
Area += (step)*((y2+y1)/2) = BetterMove