Created
January 11, 2015 16:37
-
-
Save edib/013f81a7d504938170a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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