Image:Graph of sliding derivative line.gif

From Wikipedia, the free encyclopedia

Summary

Description Boulder, Colorado

Graph of sliding derivative line

Source

self-made

Date

February 19, 2008

Author

dino ( talk)

Permission
( Reusing this image)

See below.

Licensing

I made this with SAGE, an open-source math package, and release it to the world.

A version of the source code lives at this link.

A wikipedian asked, so the SAGE source code I used to generate the image follows, verbose documentation and all.

Below. The current image has different constants than the original code I posted,

number_of_plot_points = 50

and

figure_size = 2.5

Anyone wishing to use/modify/shoot at this source code is free to:

#*****************************************************************************
#       Copyright (C) 2008 Dean Moore < deanlorenmoore@gmail.com >,
#                                         < dino@boulder.net >
#
#  Distributed under the terms of the GNU General Public License (GPL)
#                   http://www.gnu.org/licenses/
#*****************************************************************************
 
#################################################################################
#                                                                               #
# What this program does, needed input, boilerplate info & disclaimers:         #
#                                                                               #
# This program was written for the mathematical package SAGE (by implication,   #
# using the Python programming language), and animates a sliding derivative     #
# line to a given one-variable single-valued function, defined as *f* (below).  #
#                                                                               #
# A picture paints a thousand words, and it's best to look at some sample       #
# output.  In short, it's an animated illustration of a standard Calc I         #
# concept.                                                                      #
#                                                                               #
# Once function *f* is defined, parameters *a* and *b* define the top & bottom  #
# x-values, as the interval [a, b].  The program is designed to churn without   #
# further input, doing all needed calculations after definition of the          #
# function & interval; the user may wish to vary a few other parameters as:     #
#                                                                               #
# The number of frames in the final "movie," the length, thickness & color of   #
# the function's curve, the length, thickness & color of the tangent line, and  #
# a few others (below documentation).                                           #
#                                                                               #
# Caveat: don't use unbounded functions, e.g., f(x) = 1/x on (0,1].  No         #
# functions with unbounded first derivatives, e.g., f(x) = x^(1/3) about x = 0. #
#                                                                               #
# Doubtless there are other ways to break the code of which I have not          #
# thought.  If the user finds it amusing, fine with me.                         #
#                                                                               #
# User-input material:                                                          #
#                                                                               #
# f: The function.  No unbounded functions; no unbounded first derivatives.     #
#                                                                               #
# a: lower x-bound.                                                             #
# b: upper x-bound.                                                             #
#                                                                               #
# number_of_plot_points: Number of frames in final "movie."                     #
#                                                                               #
# thickness_of_curve: Thickness of function's curve.                            #
# color_of_curve:     By name.                                                  #
#                                                                               #
# length_of_deriv_line: Length of line that slides along, always tangent to     #
#                       the function *f*; its slope always the derivative of    #
#                       *f* at point of tangency.                               #
# thickness_deriv_line: Thickness of derivative line.                           #
# color_of_deriv_line:  By name.                                                #
#                                                                               #
# sliding_point_size:     By name, how big is the tracing point  See output.    #
# color_of_sliding_point: A point traces the curve; this is its colour.          #
#                                                                               #
# figure_size: Regulates size of final image.                                   #
#                                                                               #                                                                               
# delay_between_frames: Delay between frames of final "movie."                  #
#                                                                               #
# The code isn't that error-checking, assuming certain numbers positive,        #
# assuming b > a, assuming *f* "nice" with a "nice" derivative.  The program    #
# isn't designed to be idiot-proof.                                             #
#                                                                               #
#################################################################################
 
#################################################################################
#                                                                               #
# Why did I write it?  A challenge to learn more, to do it in SAGE.  I looked   #
# at <  http://en.wikipedia.org/wiki/Derivative > & thought, "Gee, maybe some    #
# more lively images.  It's 2008."                                              #
#                                                                               #
# The code's not perfect.  The sliding line runs faster where the curve is      #
# steep, and kind of slow where it's flat.  This is an unavoidable consequence  #
# of breaking interval [a, b] into equal subintervals.  I can scope in my head  #
# one way to make the sliding line run at a somewhat consistent rate, but it    #
# would be CPU-intensive & bothersome.                                          #
#                                                                               #
# Written by Dean Moore, February, 2008.                                        #
#                                                                               #
#################################################################################
 
# User-defined material:
 
f = x*sin(x^2) # The function.
 
a = -1 # Bottom x-value
b =  3 # Top x-value
 
number_of_plot_points = 125 # Number of points at which the function & derivative
                            # are sampled: the more, the smoother runs the final
                            # "movie" -- and the slower the program runs, the
                            # slower the "movie" & the more bytes to the image.
 
thickness_of_curve = 0.75      # Thickness of function's curve.
color_of_curve     = (0, 0, 1) # By name.
 
length_of_deriv_line = 3         # Length of "sliding" derivative line.
thickness_deriv_line = 1         # Thickness of derivative line.
color_of_deriv_line  = (1, 0, 0) # By name.
 
sliding_point_size     = 20        # By name; how big is the tracing point.
color_of_sliding_point = (0, 0, 0) # A point traces the curve; this is its colour.
  
figure_size = 4 # Regulates size of final picture. The bigger, the
                # more bytes / image, and the slower it runs.
 
delay_between_frames = 0 # Time between "frames" of final "movie."
 
# End user-defined material.
 
# The rest of the program churns without user input.  The disinterested user
# may safely ignore the rest, unless s/he wishes to modify the code -- or I
# made some error I didn't catch.
 
# Top matter, no nice way to classify.  Stuff that needs done:
 
v = [] # This will hold graphic image of function's curve.
 
length_of_deriv_line /= 2 # We draw half below, half
                          # above.  User doesn't need to know this. 
 
# We use the same loop several times, from bottom of interval at *a* to
# to top at *b* by an obvious step; put the range in a parameter.  Note
# we include the endpoint:
 
loop_range = srange(a, b + (b - a)/number_of_plot_points, (b - a)/number_of_plot_points) # Done with this.
 
# We use each function value three times; place them
# in a dictionary for efficiency.
 
function_values = {} # Blank dictionary to hold function values.
for i in loop_range:
   function_values[i] = f(i) # Done filling dictionary.
 
f_prime = f.derivative()  # Derivative of function *f* (above).
 
def deriv_line(x0,x):                                # A function of two variables:
   return f_prime(x0)*x + ( f(x0) - f_prime(x0)*x0 ) # Draws a line through (x0, f(x0)),
                                                     # slope (df/dx)(x0).  The derivative
                                                     # and function are evaluated at
                                                     # "fixed" *x0*; the "real" independent
                                                     # variable is *x*. Parens are only
                                                     # for clarity, y = mx + b form.  
# End top matter.
 
# The next code is more involved.  We need to make a sliding derivative
# line of constant length.  While experimenting I tried from, say, one
# below & one above on the x-axis, and it looked awful.
 
# Define four dictionaries that will hold coordinates of line segments:
  
bottom_x_points = {}
bottom_y_points = {}
top_x_points    = {}
top_y_points    = {}
 
# Next loop populates the dictionaries with coordinates of line segments of
# constant length *length_of_deriv_line* (well, its original value, twice
# its current value):
 
for i in loop_range:
    theta = RDF(arctan(f_prime(i)))             # Angle of derivative line.
                                                # Use trig & basic calc
                                                # to verify next block:
    c = RDF(cos(theta))      # Both of these are
    s = RDF(sin(theta))      # used twice, never
                              # outside this block.
# Note sign patterns in next:
 
    bottom_x_points[i] = i                  - length_of_deriv_line*c # Bottom x-value of deriv line
    bottom_y_points[i] = function_values[i] - length_of_deriv_line*s # Bottom y-value of deriv line.
 
    top_x_points[i]    = i                  + length_of_deriv_line*c # Top x-value of deriv line
    top_y_points[i]    = function_values[i] + length_of_deriv_line*s # Top y-value of deriv line.
 
# Done computing line segments that comprise the sliding derivative line.
 
# We find max & min x-values for the graph, so the display looks nice:
 
max_x_val = max(max(top_x_points.values()), max(bottom_x_points.values())) # Not perfect, but if parameter
min_x_val = min(min(top_x_points.values()), min(bottom_x_points.values())) # *number_of_plot_points* is large
                                                                           # enough for a fairly fine "mesh,"
                                                                           # good enough.
# We find max & min y-values for graph:
 
max_y_val = max(max(top_y_points.values()), max(bottom_y_points.values())) # See above documentation of
min_y_val = min(min(top_y_points.values()), min(bottom_y_points.values())) # *max_x_val* & *min_x_val*.
 
# Graph the sliding derivative line:
 
sliding_deriv_line = animate(line([(bottom_x_points[i], bottom_y_points[i]),
                                   (top_x_points[i], top_y_points[i]
                                 )],
                                    thickness = thickness_deriv_line,
                                    rgbcolor = color_of_deriv_line)
                                    for i in loop_range
                            )
 
# Graph the point that slides along the graph.
 
sliding_points      = animate([point((i,function_values[i]),
                               rgbcolor = color_of_sliding_point, 
                               pointsize = sliding_point_size)
                               for i in loop_range
                              ],
                               xmin = min_x_val, ymin = min_y_val,
                               xmax = max_x_val, ymax = max_y_val,
                               figsize = [figure_size,figure_size]
                             )
# Of note on next: when I first did the graph, it was "wiggly."  This
# led to a note to sage-support, which led to a ticket, which someone
# fixed (thanks!), but the fix won't come out until the next version
# of SAGE.  So for this code I use a work-around.  

# Animate the graph: 
 
graph = (plot(f, [a, b], thickness = thickness_of_curve,
                            rgbcolor = color_of_curve))
for i in loop_range:
   v.append(graph) # End of i-loop.
 
curve = animate(v) # Must be done.
 
# Now show the final "movie":
 
(curve + sliding_points + sliding_deriv_line).show(delay = delay_between_frames)
 
# Done with program. 

--- Done source code.

File history

Click on a date/time to view the file as it appeared at that time.

Date/Time Dimensions User Comment
current 03:40, 22 February 2008 250×250 (202 KB) Oleg Alexandrov ( Talk | contribs) (Reverted to version as of 02:46, 22 February 2008, my image is bigger than this)
revert 03:37, 22 February 2008 174×200 (294 KB) Oleg Alexandrov ( Talk | contribs) (Crop a bit)
revert 02:46, 22 February 2008 250×250 (202 KB) Dino ( Talk | contribs) (Fewer bytes, about 208 kb)
revert 01:41, 21 February 2008 500×500 (842 KB) Dino ( Talk | contribs) (Got rid of "wiggling"; thanks to some person on sage-support for fixing this ticket; I did a work-around.)
revert 17:16, 20 February 2008 400×400 (550 KB) Dino ( Talk | contribs)
revert 02:32, 20 February 2008 400×400 (547 KB) Dino ( Talk | contribs) ({{Information |Description=Graph of sliding derivative line |Source=self-made |Date=February 19, 2008 |Location=Boulder, Colorado |Author=~~~ |other_versions= }})

    See the setup instructions for more information.

The following file is a duplicate of this file:

The following pages on Schools Wikipedia link to this image (list may be incomplete):
The Schools Wikipedia is sponsored by SOS Children , and is mainly selected from the English Wikipedia with only minor checks and changes (see www.wikipedia.org for details of authors and sources). See also our Disclaimer.