[Edu-sig] a segue from discrete math to calculus using Python

Kirby Urner kurner at oreillyschool.com
Tue Feb 4 08:08:31 CET 2014


On Mon, Feb 3, 2014 at 1:31 PM, A. Jorge Garcia <calcpage at aol.com> wrote:

> Great post Kirby!
>
> Your approach is very similar to what I do with my Calculus classes.
> However, I use python under SAGE and I emphasize Riemann Sums.
>
> In fact, we do a lot of what I used to call "Scientific Computing" in a
> manner very reminiscent of a MATLAB approach for:
> Limits, Difference Quotient, Newton's Method, Riemann Sums and Euler's
> Method.
>
> All these topics are very accessible for students even in PreCalculus and
> Computer Science classes by using python!
>
> Don't you just love Pythonic Math?
>


Thanks Jorge.

I sure do [ love Pythonic Math ].

I'm looking forward to attending more SAGE talks / presentations.

Not enough proposals have it in my view (I'm currently plowing through a
bunch of talk proposals for OSCON).

In using "bare naked Python" I'm leaving a door open to reviewing calculus,
but also to learning about class decorators:

__author__ = 'Kirby Urner'

"""
(c) MIT License, K. Urner, 4D Solutions, 2014
"""

from math import log

h = delta_x = 1e-2  # tiny delta

class Derivative:

    def __init__(self, func):
        self.func = func

    def __call__(self, x):
        f = self.func
        return (f(x+h) - f(x-h)) / (2*h)     # or do it your way

@Derivative        #  class decorator
def parabola(x):
    """Parabola"""
    return x * x  # parabola

def report(f, domain):
    print("=" * 30)
    print([ (x, round(parabola(x),2)) for x in domain]) # discrete
approximation

domain = range(-10, 11)
report(parabola, domain)


==============================
[(-10, -20.0), (-9, -18.0), (-8, -16.0), (-7, -14.0), (-6, -12.0), (-5,
-10.0), (-4, -8.0), (-3, -6.0), (-2, -4.0), (-1, -2.0), (0, 0.0), (1, 2.0),
(2, 4.0), (3, 6.0), (4, 8.0), (5, 10.0), (6, 12.0), (7, 14.0), (8, 16.0),
(9, 18.0), (10, 20.0)]

yep, that looks like D(parabola) <==> def f(x):  return 2 * x where D()
makes a derivative function from a function (D per Spivak's Calculus on
Manifolds).

The integral should look like pow(x, 3)/3 + C.

Applying the other decorator:

class Integral:

    def __init__(self, func):
        self.func = func

    def __call__(self, a, b):
        """definite integral over [a, b] using discrete approximation"""
        if not a<=b:
            raise ValueError
        area = 0.0
        x = a
        while x <= b:
            area += self.func(x) * delta_x
            x += delta_x
        return area

@Integral
def parabola(x):
    """Parabola"""
    return x * x  # parabola

def report(f, domain):
    print("=" * 30)
    print([ (x, round(parabola(domain[0], x),2)) for x in domain]) #
discrete approximation
    print([ (x, round(x**3/3 + 334, 2) ) for x in domain ] )  # limiting
function

domain = range(-10, 11)
report(parabola, domain)

The empirical discrete number for the Riemann Sum (sigma to not yet the
limit)
tracks the calculus result (limiting result) pretty closely.  That's why
we're calling this
the standard segue, the difference being it's in the context of a computer
language
(one with REPL, ala SAGE).

==============================
[(-10, 1.0), (-9, 91.24), (-8, 163.49), (-7, 219.75), (-6, 262.01), (-5,
292.29), (-4, 312.58), (-3, 324.88), (-2, 331.19), (-1, 333.51), (0,
333.83), (1, 334.17), (2, 336.52), (3, 342.88), (4, 355.25), (5, 375.63),
(6, 406.01), (7, 448.41), (8, 504.82), (9, 577.24), (10, 667.67)]
[(-10, 0.67), (-9, 91.0), (-8, 163.33), (-7, 219.67), (-6, 262.0), (-5,
292.33), (-4, 312.67), (-3, 325.0), (-2, 331.33), (-1, 333.67), (0, 334.0),
(1, 334.33), (2, 336.67), (3, 343.0), (4, 355.33), (5, 375.67), (6, 406.0),
(7, 448.33), (8, 504.67), (9, 577.0), (10, 667.33)]

Kirby




> -----Original Message-----
> From: Kirby Urner <kurner at oreillyschool.com>
> To: edu-sig <edu-sig at python.org>
> Sent: Mon, Feb 3, 2014 3:37 pm
> Subject: [Edu-sig] a segue from discrete math to calculus using Python
>
> """
> Discrete math approach to Calculus
>
> This is what I imagine as a segue from discrete math to
> calc, using Python.  We're using a tiny delta_x = h to
> compute values discretely, and then comparing those
> computed series with functions "at the limit" such as
> calculus would give us.  It's the same segue we typically
> encounter using Sigma notation to introduce Riemann Sum
> notation in Algebra 2 today.  All the assumptions are
> deliberately simplified:  continuous functions in one
> variable.
>
> (c) MIT License, K. Urner, 4D Solutions, 2014
>
> """
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20140203/ff53fa0b/attachment-0001.html>


More information about the Edu-sig mailing list