Skip to content

Instantly share code, notes, and snippets.

@edib
Created January 11, 2015 16:37
Show Gist options
  • Save edib/013f81a7d504938170a2 to your computer and use it in GitHub Desktop.
Save edib/013f81a7d504938170a2 to your computer and use it in GitHub Desktop.
'''
calculate definite integral for given intervals and slices
+startPoint+:: float
+endPoint+:: float
+numberofSlices+:: integer
'''
def integral(startPoint,endPoint,numberofSlices)
# change for each iteration
deltaX = (endPoint-startPoint)/numberofSlices
# total area and iteration area, must be defined here before used
area, sliceArea = 0.0
# simple loop
numberofSlices.times do |i|
x = startPoint + i * deltaX.to_f
#the function
y = 3*x**2
# area of each slice
sliceArea = y * deltaX.to_f
area = area + sliceArea
i += 1
end
return area.round
end
puts integral(1.0,3.0,10000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment