<div dir="ltr">"""<br>Discrete math approach to Calculus<br><br>This is what I imagine as a segue from discrete math to<br>calc, using Python.  We're using a tiny delta_x = h to<br>compute values discretely, and then comparing those<br>
computed series with functions "at the limit" such as<br>calculus would give us.  It's the same segue we typically<br>encounter using Sigma notation to introduce Riemann Sum<br>notation in Algebra 2 today.  All the assumptions are<br>
deliberately simplified:  continuous functions in one<br>variable.<br><br>(c) MIT License, K. Urner, 4D Solutions, 2014<br><br>"""<br><br>from math import log<br><br>h = delta_x = 1e-2  # tiny delta<br><br>
class Integral:<br><br>    def __init__(self, func):<br>        self.func = func<br><br>    def __call__(self, a, b):<br>        if not a<=b:<br>            raise ValueError<br>        area = 0.0<br>        x = a<br>        while x <= b:<br>
            area += self.func(x) * delta_x<br>            x += delta_x<br>        return area<br><br>class Derivative:<br><br>    def __init__(self, func):<br>        self.func = func<br><br>    def __call__(self, x):<br>
        f = self.func<br>        return (f(x+h) - f(x-h)) / (2*h)<br><br><br>def parabola(x):<br>    """Parabola"""<br>    return x * x  # parabola<br><br>def int_parabola(x):<br>    """Parabola"""<br>
    return (x ** 3)/3  # integral of parabola<br><br>def deriv_parabola(x):<br>    """Derivative of Parabolic function"""<br>    return 2*x  # parabola<br><br>def reciprocal(x):<br>    """Reciprocal"""<br>
    return 1/x<br><br>def int_reciprocal(x):<br>    """Integral of Reciprocal"""<br>    return log(x)   # integral is Ln(x)<br><br>def deriv_reciprocal(x):<br>    """Derivative of Reciprocal"""<br>
    return -x**-2<br><br>def report(f, domain, limint, limderiv, C=0):<br>    integral_f = Integral(f)<br>    derivative_f = Derivative(f)<br>    print("=" * 30)<br>    print(f.__doc__, [(x,f(x)) for x in domain])<br>
    print()<br>    print("Approx Integral  : ", ["({}, {:>5.2f})".format(x, integral_f(a=domain[0], b=x) + C) for x in domain])<br>    print("Limit Integral   : ", ["({}, {:>5.2f})".format(x, limint(x)) for x in domain])<br>
    print()<br>    print("Approx Derivative: ", ["({}, {:>5.2f})".format(x, derivative_f(x)) for x in domain])<br>    print("Limit Derivative : ", ["({}, {:>5.2f})".format(x, limderiv(x)) for x in domain])<br>
<br>report(parabola, range(-10, 11), limint = int_parabola, limderiv = deriv_parabola, C = -334) # C = constant offset<br>report(reciprocal, range(1, 11), limint = int_reciprocal, limderiv = deriv_reciprocal)<br></div>