User-friendly Xlisp-stat Calculus

Description

I'm working on a series of functions to make things easy for a calculus student to use in their studies. The platform I'm using for development is the mac, and I haven't tested this with others, but don't expect that there will be many problems.

Included are a function parser, so that functions are entered in a more natural fashion, e.g.

        (func f(x) x ^ 3 - 2 * x + 3)
or
        (func f(x y) x ^ 2 - y ^ 2)
plotting is more natural:
        (plot f)
("plot" plots most anything, including several functions together, functions of several variable, matrices, etc., and has features like zoom (some stolen from Young's ViSta stuff).

A "calculator" also allows students to do calculations in infix notation, e.g.

        (calc sqrt 35)

Sample code

Differentiation, integration, tangent lines and planes, etc. are included. Here is the student primer as it currently stands: it is intended to be loaded, from the menu or using the (load "student.lsp") command.
(message "
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;                             Student Primer
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        In this short session, we illustrate the standard operations that you
may have cause to use. Give them a try, and then modify this file to do similar
things (but on functions of your own choosing!).

                                                        Andy Long
                                                        Winter, 1996
")

(message "
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;                             Defining functions: func
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Let's define the function f, where f(x)=x^3-2*x+3, using func:

        (func f(x) x ^ 3 - 2 * x + 3)

(Note all the spaces: they're necessary!)
")

        (terpri)
        (princ "(func f(x) x ^ 3 - 2 * x + 3) ; f(x)=x^3-2*x+3")
        (terpri)
        (func f(x) x ^ 3 - 2 * x + 3) ; f(x)=x^3-2*x+3

(message "
        Okay, it's defined! (I actually sent that command above to xlisp-stat,
including the semi-colon: the semicolon acts as a comment, and anything
following it on a line is ignored by xlisp-stat.)

        Note that you must separate operators (e.g. ^, -, *, +) from numbers by
spaces. The exception to this rule is if you use parentheses: that is,

                            (func f(x) (1 + x)*(x))

would be okay, because the 1 + x and x are contained within parentheses.
")

(message "
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;                             Plotting functions: plot
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        Plotting is fairly easy: there are a variety of options (like using the
\"title\" command to give your plot a meaningful name, or the \"domain\" or
\"range\" commands to set the window if you want to). You can see your options
by typing (help new-plot). (\"help\" is the usual way of getting help on a
command, such as new-plot.) But plot does a pretty good job with little
information.

        (new-plot) ; initializes the plotting environment.
        (title \"Plot of f\")
        (plot f -3 2) ; plot f from -3 to 2.
")
        (new-plot) ; initializes the plotting environment.
        (title "Plot of f")
        (plot f -3 2) ; plot f from -3 to 2.
(message "
        You can play with the plot: click the mouse button inside the plot
window: if you do this quickly, it should zoom out. If you draw a box with the
mouse you'll zoom in on the area within the box (hold down the mouse button at
one point, and pull the mouse to another point: you're making two corners of a
box!).
")
(message "
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;                             Derivatives: diff
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        You may need to look at a derivative from time to time: the \"diff\"
command is useful for such:

        (diff f x) ; What's its derivative look like?
        (diff f x fp) ; define the derivative function fp.
        (title \"Plot of f's Derivative\")
        (plot fp -3 2)
")
        (diff f x) ; What's its derivative look like?
        (diff f x fp) ; define the derivative function fp.
        (title "Plot of f's Derivative")
        (plot fp -3 2)

(message "")
(message "
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;                             A calculator: calc
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        If you just need to calculate something, in the course of your work,
use the \"calc\" function:

        (calc sqrt 47)
        (calc 45 / 192)
")
        (print (calc sqrt 47))
        (print (calc 45 / 192))
        (terpri)

(message "")
(message "
Calc can also be used to check the value of a variable you have defined, or
that has been defined for you in the course of something else: if you want to
set a variable (call it a) to 3, you use the \"def\" function and type

        (def a 3)

Typing a variable's name without any parentheses is the easy way to check it's
value, however.

        a

Note that this doesn't work with functions, however: don't try
typing their names alone on the command line (unless you like error message!).
")
        (print (def a 3))
        (print a)
        (terpri)

(message "")
(message "
        To check the value of a function at the value a, you would type

        (f a)

rather than (f(a)), f(a), or the like.
")
        (print (f a))
        (terpri)

(message "")
(message "
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;                             Tangent lines
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        What does the tangent line approximation to f at x=-.5 look like at
x=1? Use the tangent command:

        (title \"Tangent line plot\")
        (tangent f -.5 1)
")
        (title "Tangent line plot")
        (tangent f -.5 1)

(message "

        What luck! It's right on! Notice that you can zoom in or out on the
tangent point (or any point) with your mouse button. That's an especially cool
little trick for looking at this type of plot, but, as noted earlier, it is a
general feature of the plot command.
")
(message "
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;                             Derivative plots
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        The special command diffplot plots f and some derivatives:

        (title \"Derivative plots\")
        (diffplot f -3 2)

This command will plot f and its derivative on the interval (-3 2).
")
        (title "Derivative plots")
        (diffplot f -3 2)

(message "

        If you want more derivatives, add an additional number (up to 3):

        (diffplot f -3 2 2) ; that last \"2\" means the second derivative, too.
        (diffplot f -3 2 3) ; that last \"3\" means the second and third
                            ; derivatives.
")
        (diffplot f -3 2 2) ; that last "2" means the second derivative, too.
        (diffplot f -3 2 3) ; that last "3" means the second and third
                            ; derivatives.

(message "")
(message "
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;                             Plotting several functions
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        You may want to plot several functions of your own choosing together:

        (title \"Multiple plots\")
        (func g(x) x ^ 2)
        (func h(x) sin x)
        (func ff(x) - exp x)
        (plot g h ff -3 2)
")
        (title "Multiple plots")
        (func g(x) x ^ 2)
        (func h(x) sin x)
        (func ff(x) - exp x)
        (plot g h ff -3 2)

(message "")
(message "
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;                             Roots
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        You may need to find roots, of f or its derivatives: allow Newton!
Note the use of the domain function: it sets the domain for all future
plots, so don't forget! Let's find a zero of our cubic function f:

        (domain -3 2)        
        (title \"Newton's Method\")
        (newton f -1)
;; Newton can sometimes have a hard time! This is a funny show: try starting at
;; 0, rather than -1, sometime:
;;        (newton f 0)
")
        (domain -3 2)        
        (title "Newton's Method")
        (newton f -1)

(message "")
(message "
        If you don't want the graphical output, just the value, type instead

        (roots f 1)
        (roots fp (-2 2)) ; note: we're giving two different initial guesses
")
        (princ (roots f 1))
        (terpri)
        (princ (roots fp (-2 2)))
        (terpri)

(message "")
(message "
        Or try muller's method (which requires 3 initial guesses):

        (muller f -2 0 2 :num-roots 3)

Muller's method will find complex roots, as well as real ones (Newton can NEVER
find a complex root). Since f is a cubic, it has three roots , which muller will
discover. (Note the use of the :num-roots option - this is a typical way of
telling xlisp-stat that you want to give an additional piece of information.)

        Newton found the real one above, but here are all three of them:
")
        (princ (muller f -2 0 2 :num-roots 3))
        (terpri)

(message "")
(message "
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;                             Integration
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        Left- and right-rectangle rules let you calculate definite integrals:

        (lrr f 0 1 100) ; (left) integral of f from 0 to 1 with 100 steps:
        (rrr f 0 1 100) ; (right) integral of f from 0 to 1 with 100 steps:
")
        (print (lrr f 0 1 100))
        (terpri)
        (print (rrr f 0 1 100))
        (terpri)

(message "")
(message "
        Simpson's rule does it better than the rectangle rules (with a given
tolerance: the default is tolerance is 1e-6, which equals .000001 (the decimal
place has moved left six places)):

        (simpson f 0 1)
")
        (print (simpson f 0 1))
        (terpri)

(message "")
(message "
        How about a picture of the anti-derivative of a function? This is
actually a numerical approximation of the integral, and so is clumsy:

        (integral f.int f 0 x .001) ; integral defines a function, here called
                                    ; f.int, which represents that
                                    ; anti-derivative of f which is zero at 0.
                                    ; The .001 represents a tolerance for the
                                    ; numerical integration: a large tolerance
                                    ; helps speed things up!
        (title \"Numerical anti-derivative\")
        (plot f.int f 0 pi)
")
        (integral f.int f 0 x .001) ; integral defines a function, here called
                                    ; f.int, which represents that
                                    ; anti-derivative of f which is zero at 0.
                                    ; The .001 represents a tolerance for the
                                    ; numerical integration: a large tolerance
                                    ; helps speed things up!
        (title "Numerical anti-derivative")
        (plot f.int f 0 pi)


(message "")
(message "
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;                             2-d function stuff
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        You can plot 2-d functions, which are also defined with \"func\":

        (func ff(x y) cos x + sin y)
")
        (func ff(x y) cos x + sin y)

(message "
        Okay, I've taken care of defining it, using the func command: now to
plot it. Note the use of these plotting functions, like (title \"...\") and
(spin-domain -pi pi -pi pi) (which sets the part of the xy-plane of interest):

        (new-plot)
        (spin-points 20)
        (spin-axes nil)
        (title \"f(x y) = cos x + sin y\")
        (spin-domain -pi pi -pi pi)
        (plot ff)
")
        (new-plot)
        (spin-points 20)
        (spin-axes nil)
        (title "f(x y) = cos x + sin y")
        (spin-domain -pi pi -pi pi)
        (plot ff)

(message "")
(message "
        Diffplot also plots a 2-d function and its partial derivatives:

        (title \"cos x + sin y and derivatives\")
        (diffplot ff)

kind of a confusing plot, sometimes, but...
")
        (title "cos x + sin y and derivatives")
        (diffplot ff)

(message "")
(message "
        Diffplot also allows you to have a look at the tangent plane to a
function: here it is at (1,1).

        (spin-points 10)
        (title \"cos x + sin y and tangent plane\")
        (diffplot ff -pi/2 pi/2 -pi/2 pi/2 1 1)

(Note that it is the addition of the last two values that indicates to
diffplot that you want the tangent plane.)
")
        (spin-points 10)
        (title "cos x + sin y and tangent plane")
        (diffplot ff -pi/2 pi/2 -pi/2 pi/2 1 1)

(message "")
(message "

        Or, just plot any old bunch of 2-d functions together.

        (title \"Flying Sheets\")
        (func ff3 (x y) (12 + cos x + sin y))
        (func ff2 (x y) (6 + cos x + sin y))
        (plot ff ff2 ff3)
")
        (title "Flying Sheets")
        (func ff3 (x y) (12 + cos x + sin y))
        (func ff2 (x y) (6 + cos x + sin y))
        (plot ff ff2 ff3)

(message "")
(message "
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;                             Discrete data sets:
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        You may want to plot some data. You can define the data using the
\"data\" command: 

        (data page3 (1 3) (2 4) (3 5) (4 3) (5 7) (6 5))

        (title \"Data from Page 3\")
        (plot page3) ; (Note that plot will profess confusion, but does the
                     ; right thing.)
")
        (data page3 (1 3) (2 4) (3 5) (4 3) (5 7) (6 5))

        (title "Data from Page 3")
        (plot page3) ; (Note that plot will profess confusion, but does the
                     ; right thing.)

(message "")
(message "

        Now suppose you want to pull off the x and y values from the ordered
pair information you gave: use the \"def\" (for define) command, and the x's
and y's commands xs and ys:

        (def page3.x (xs page3))
        (def page3.y (ys page3))
        (plot page3.x page3.y)
")
        (def page3.x (xs page3))
        (def page3.y (ys page3))
        (plot page3.x page3.y)

(message "")
(message "
        You may wonder why you couldn't use the def command for the page3 data
itself: def takes the next thing and assigns it to the name, but only one
thing, while data takes the whole bunch and assigns it to the name.

One may often want to construct a grid of data: grid takes as it's arguments
the endpoints, followed by the number of steps desired from left endpoint to
right endpoint:

        (title \"One-dimensional gridded data\")
        (grid -5 5 20)
        (def gdata (grid -5 5 20))

This will generate a grid of points from -5 to 5, with steps of .5,
{-5, -4.5, ... , 4.5, 5}, and call it \"gdata\". Now we can put those values
into f (remember that f is that cubic defined above), calling that \"fdata\",
and when we plot it it should look sort of like our initial plot:

        (def fdata (f gdata))
        (plot gdata fdata)
")
        (title "One-dimensional gridded data")
        (grid -5 5 20)
        (def gdata (grid -5 5 20))
        (def fdata (f gdata))
        (plot gdata fdata)

(message "")
(message "
        If you have a table of data that you want to enter, then you might
consider the \"table\" command:

        This is the beef data from page 3 of the Harvard Consortium's
Multivariate Calculus text: beef consumption as a function of salary and price.
(Note that the table command defines beef as a 5x4 table of values, given in the
following lines.)

        (data prices 3 3.5 4 4.5) ; these were the prices (in $1)
        (data salaries 20 40 60 80 100) ; these were the salaries (in $1000)
        (table beef 5 4
        2.65 2.59 2.51 2.43
        4.14 4.05 3.94 3.88
        5.11 5.00 4.97 4.84
        5.35 5.29 5.19 5.07
        5.79 5.77 5.60 5.53
        )
        (new-plot)
        (title \"Beef consumption(salary,price)\")
        (plot beef)
")
        (data prices 3 3.5 4 4.5)
        (data salaries 20 40 60 80 100)
        (table beef 5 4
        2.65 2.59 2.51 2.43
        4.14 4.05 3.94 3.88
        5.11 5.00 4.97 4.84
        5.35 5.29 5.19 5.07
        5.79 5.77 5.60 5.53
        )
        (new-plot)
        (title "Beef consumption(salary,price)")
        (plot beef)

(message "")
(message "
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;                       There's lots more! Some details....
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        Xlisp-Stat is a powerful language built on lists, written in the
language LISP (for LISt Programming). This is free software, and it is
available for PC or the Mac (and even UNIX). See me for the code, or just
download it yourself from these machines. 

        We'll introduce new stuff as we need it. In the meantime, please let me
know what you do and don't like about this, and suggest improvements. Remember,
this software is free, and you are encouraged to take it with you!

        For additional help on any commands, use either the \"help\" or \"?\"
commands: (help command-name) gives help on just that command-name, e.g.

        (help plot)

as does (? command-name) (which gives help on that command-name and any
containing it: i.e., if you give \"plot\", you'll get help on any command
containing the word plot (too many!)).

        (? plot)

        You can try \"hulp\", which is menu driven, but it isn't very complete
yet, and needs some work. You can help me with it by writing some examples for
your favorite functions and sending them to me. I'll include them.

        (hulp)

        There's lots more to do with xlispstat: for example, the \"stat\" in the
name should indicate to you that you can do statistics galore, and that's true.
You can do histograms, means, and plenty of other neat stuff. But this is
rather incidental for the calc student. See me with additional questions,
comment, gripes, and complaints; or send email to:

                         longa@picard.macs.ripon.edu

See my home page for more information about obtaining xlisp-stat for your own
personal use (but note that many of the commands listed above require software
that I have written, so you must get those from me personally).

                         http://picard.macs.ripon.edu/~longa/andy.html
")

Project Investigators

Andy Long

Contact person

Andy Long, Department of Mathematics and Computer Science, Ripon College, Ripon, WI, 54971

Andy Long
longa@picard.macs.ripon.edu