Skip to content

Instantly share code, notes, and snippets.

@chand1012
Created October 15, 2019 00:17
Show Gist options
  • Save chand1012/18e6f44489d5c914208524e3729daa44 to your computer and use it in GitHub Desktop.
Save chand1012/18e6f44489d5c914208524e3729daa44 to your computer and use it in GitHub Desktop.
import math
def f(x): # change this to the function of your choice
squared = math.pow(x, 2)
return math.sqrt(squared+1)
a = 0 # start
b = 2 # end
n = 6 # increase for more accuracy
dx = (b-a)/n
print(f"dx = {dx}")
result = 0
xis = range(n+1)
for i in xis:
xi = a + (dx*i)
t = f(xi)
if i == 0 or i == n:
result += t
print(f"{i}: {t}")
elif i%2==1:
result += 4 * t
print(f"{i}: {t*4}")
else:
result += 2 * t
print(f"{i}: {t*2}")
print(f"result: {result}")
print(f"dx/3: {dx/3}")
result *= (dx/3)
print(f"result * (dx/3): {result}")
import math
def f(x):
squared = math.pow(x, 2)
return math.sqrt(squared + 1)
a = 0
b = 2
n = 26
dx = (b-a)/n
xis = range(n+1)
print(f"dx = {dx}")
result = 0
for i in xis:
xi = a+(dx*i)
if i == 0 or i == n:
t = f(xi)
print(f"{i}: {t}")
result += t
else:
t = f(xi)
print(f"{i}: {t*2}")
result += t
print(f"result: {result}")
print(f"dx/2: {dx/2}")
result *= (dx/2)
print(f"result * (dx/2): {result}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment