Skip to content

Instantly share code, notes, and snippets.

@techtide
Last active October 15, 2021 10:53
Show Gist options
  • Save techtide/73950589a70a3bafec7ead16d3511a28 to your computer and use it in GitHub Desktop.
Save techtide/73950589a70a3bafec7ead16d3511a28 to your computer and use it in GitHub Desktop.
Compute Darboux Integral (Upper and Lower Riemann Sums)
from sympy import *
import numpy as np
from scipy import misc
from sympy.parsing.sympy_parser import parse_expr
x, y, z, t = symbols('x y z t')
k, m, n = symbols('k m n', integer=True)
f, g, h = symbols('f g h', cls=Function)
init_printing(use_unicode = True)
print("Welcome to Upper Riemann Sum and Lower Riemann Sum Calculator.")
print("[1] Enter f(x):")
expr = parse_expr(input())
print("Processed the following expression: f(x) = {}".format(expr))
print("[2] Enter each element of the partitions set in increasing order (press X to stop).")
partitions, typed = [], ""
while typed != "X":
typed = input()
if typed != "X": partitions.append(float(typed))
assert len(partitions) > 1
delta = abs(partitions[1] - partitions[0])
print("Processed the following partitions: {} with Delta = {}".format(partitions, delta))
while True:
print("[3] Enter what type of Riemann/Darboux Sum is wanted or exit: 0=LOWER SUM, 1=UPPER SUM, CTRL+D=EXIT")
type_of_sum = int(input())
print("The computation will be: {}".format("Upper Darboux Sums." if type_of_sum == 1 else "Lower Darboux Sums."))
areas = []
for i in range(len(partitions) - 1):
lower_bound = partitions[i]
upper_bound = partitions[i + 1]
f_lower_bound = expr.subs(x, lower_bound)
f_upper_bound = expr.subs(x, upper_bound)
print("---------------------------------------------")
print("x:\t [{}, {}]".format(lower_bound, upper_bound))
print("f(x):\t [{}, {}]".format(f_lower_bound, f_upper_bound))
if type_of_sum == 1:
max_x = upper_bound if max([f_lower_bound, f_upper_bound]) == f_upper_bound else lower_bound
max_fx = expr.subs(x, max_x)
print("Maximum: ({}, {})".format(max_x, max_fx))
print("Area:\t {}".format(delta * max_fx))
areas.append(delta * max_fx)
else:
min_x = lower_bound if min([f_lower_bound, f_upper_bound]) == f_lower_bound else upper_bound
min_fx = expr.subs(x, min_x)
print("Minimum: ({}, {})".format(min_x, min_fx))
print("Area:\t {}".format(delta * min_fx))
areas.append(delta * min_fx)
print("---------------------------------------------")
print("Answer for {}: {}".format("UPPER DARBOUX SUMS Uf(P)" if type_of_sum == 1 else "LOWER DARBOUX SUMS Lf(P)", sum(areas)))
from scipy import misc
import numpy as np
from sympy import *
from functools import reduce
x, y, z, t = symbols('x y z t')
k, m, n = symbols('k m n', integer=True)
f, g, h = symbols('f g h', cls=Function)
init_printing(use_unicode=True)
# expr = (x ** 2) * cos(pi * x)
expr = (x ** 3) - (2 * x) + 3
# expr = x ** 2
expr_derivative = diff(expr)
# solutions = solve(expr_derivative)
partitions = [2, 2.5, 3, 3.5, 4]
delta = 0.50
areas = []
print("f(x) = {}".format(expr))
print("f'(x) = {}".format(expr_derivative))
for i in range(len(partitions) - 1):
lower_bound = partitions[i]
upper_bound = partitions[i + 1]
f_lower_bound = expr.subs(x, lower_bound)
f_upper_bound = expr.subs(x, upper_bound)
fprime_lower_bound = expr_derivative.subs(x, lower_bound)
fprime_upper_bound = expr_derivative.subs(x, upper_bound)
print("-----------------------------------------")
print("x:\t[{}, {}]".format(lower_bound, upper_bound))
print("f(x):\t[{}, {}]".format(f_lower_bound, f_upper_bound))
print("f'(x):\t[{}, {}]".format(fprime_lower_bound, fprime_upper_bound))
# x_solutions_in_interval = [x for x in solutions if x <= upper_bound and x >= lower_bound] + [lower_bound, upper_bound]
# f_solutions_in_interval = [expr.subs(x, z) for z in x_solutions_in_interval]
print("Minimum: {}".format(min([lower_bound, upper_bound])))
print("Area: {}".format(delta * expr.subs(x, min([lower_bound, upper_bound]))))
areas.append(delta * expr.subs(x, min([lower_bound, upper_bound])))
print("-----------------------------------------")
print("Answer: {}".format(sum(areas)))
from scipy import misc
import numpy as np
from sympy import *
from functools import reduce
x, y, z, t = symbols('x y z t')
k, m, n = symbols('k m n', integer=True)
f, g, h = symbols('f g h', cls=Function)
init_printing(use_unicode=True)
# expr = (x ** 2) * cos(pi * x)
expr = (x ** 3) - (2 * x) + 3
# expr = x ** 2
expr_derivative = diff(expr)
# solutions = solve(expr_derivative)
partitions = [2, 2.5, 3, 3.5, 4]
delta = 0.50
areas = []
print("f(x) = {}".format(expr))
print("f'(x) = {}".format(expr_derivative))
for i in range(len(partitions) - 1):
lower_bound = partitions[i]
upper_bound = partitions[i + 1]
f_lower_bound = expr.subs(x, lower_bound)
f_upper_bound = expr.subs(x, upper_bound)
fprime_lower_bound = expr_derivative.subs(x, lower_bound)
fprime_upper_bound = expr_derivative.subs(x, upper_bound)
print("-----------------------------------------")
print("x:\t[{}, {}]".format(lower_bound, upper_bound))
print("f(x):\t[{}, {}]".format(f_lower_bound, f_upper_bound))
print("f'(x):\t[{}, {}]".format(fprime_lower_bound, fprime_upper_bound))
# x_solutions_in_interval = [x for x in solutions if x <= upper_bound and x >= lower_bound] + [lower_bound, upper_bound]
# f_solutions_in_interval = [expr.subs(x, z) for z in x_solutions_in_interval]
print("Maximum: {}".format(max([lower_bound, upper_bound])))
print("Area: {}".format(delta * expr.subs(x, max([lower_bound, upper_bound]))))
areas.append(delta * expr.subs(x, max([lower_bound, upper_bound])))
print("-----------------------------------------")
print("Answer: {}".format(sum(areas)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment