[Edu-sig] Calculus with Stickworks (more Gnu Math)

kirby urner kirby.urner at gmail.com
Sat Sep 30 02:22:07 CEST 2006


I've amended stickworks.py a little to show how the basic ideas of
differential calculus might be spelled out in Python.

First:

Take a traditional classroom Polynomial with enough roots to make it
wiggle up and down (just come up with some factors), then tweak the
vertical scale to keep VPython from zooming out 3 miles just to take
in the view.

    def snakeywakey(x):
        """
        Polynomial with x-axis crossings at 3,2,-3,-7, with scaler
        to keep y-values under control (from a plotting point of view)
        """
	return 0.01 * (x-3)*(x-2)*(x+3)*(x+7)

Second:

Include a generic derivative taker that expects a function as input,
and returns a function that evaluates to approximately df(x)/dx at any
x, assuming whatever you need about continuity and so on.

    def deriv(f, h=1e-5):
        """
        Generic df(x)/dx approximator (discrete h)
        """
	def funk(x):
		return (f(x+h)-f(x))/h
	return funk

Third:

Graph them together in two different colors.  See how a diving slope
means a negative derivative, while a leveling off means derivative = 0
(crossing x axis) and so on.

Eyeballing the two curves together is a big part of "getting" that the
green guy is talking about the *slope* of the red guy at the same x.

    # domain generators
    d1 = dgen(-8, 0.1)
    d2 = dgen(-8, 0.1)

    axes(-8,5,0)

    deriv_snakeywakey = deriv(snakeywakey)

    graph1 = xyplotter(d1, snakeywakey)
    graph2 = xyplotter(d2, deriv_snakeywakey)

    Edge.color = (1,0,0)  # make 4th degree snakeywakey red

    for i in xrange(130):
        graph1.next()

    Edge.color = (0,1,0)  # make its 3rd degree derivative green

    for i in xrange(130):
        graph2.next()

Here's a link to the picture (in reality, you can zoom in and rotate,
look from all points of view -- part of our "beyond flatland"
campaign, designed to remind people why we don't prefer the
flatlanders' calculators to our flatscreen computers).

http://www.4dsolutions.net/ocn/python/pycalculus.png

>>> == RESTART ================================
>>> import stickworks
Visual 2005-01-08
>>> stickworks.testmemore()

Documentation:

"""
Some infrastructure for working with Vectors and Edges, including
an xyplotter generator and axes maker.

By Kirby Urner, Sept 13, 2006

Updated Sept 29, 2006:
make color Edge class-level attribute
add funky derivative demo
refactor a bit

Code:
http://www.4dsolutions.net/ocn/python/stickworks.py

For colorized source:
http://www.4dsolutions.net/cgi-bin/py2html.cgi?script=/ocn/python/stickworks.py

Some relevant discussion:
http://mail.python.org/pipermail/edu-sig/2006-September/007145.html
http://mail.python.org/pipermail/edu-sig/2006-September/007149.html
http://mail.python.org/pipermail/edu-sig/2006-September/007150.html
http://mail.python.org/pipermail/edu-sig/2006-September/007312.html
"""

Kirby


More information about the Edu-sig mailing list