Let us take another look at the function graph in
figure
.
For many problems,
we need to be able to draw a line that has the same slope as some curve at
a certain point. Indeed, computing the slope is often the true goal. In
economics problems, the slope is the growth rate of a company if the curve
represents the income over time. In a physics problem, the curve could
represent the velocity of some object; its slope, at any point, is then
the current acceleration of the object.
Determining the slope of some function f at some point x is to differentiate the function. The differential operator (also called a functional) returns a function f' (pronounced ``f prime''). It tells us for any x what the slope of f is at that point. Computing f' is complicated, so it is again a good task for a computer program. The program consumes some function f and produces f'.
To design a ``differentiator'' we must study how we could construct lines
that have the same slope as a curve. In principle, such a line touches the
curve at just that point. But suppose we relax this constraint for a
moment and look at straight lines that intersect the curve close to
the point of interest. We pick two points that are equally far away from
x, say,
and
; the constant
, pronounced
epsilon, represents some small distance. Using the two corresponding points
on the curve, we can determine a straight line that has the proper slope.
The situation is sketched in figure
. If the point
of interest has coordinate x, the two points are
and
. Hence the slope of the line is
That is, the difference between the height of the right point and the left point divided by their horizontal distance. Determining the line from the slope and one of the points or even from two points is an exercise.
By now, it is straightforward to translate this equation into Scheme:
(define (y x) (+ (* a x) b))To obtain a concrete line we must replace a and b with numbers.
The teachpack graphing.ss provides two operations for drawing lines: graph-line. The operation consumes a line like y and a color, say, RED. Use graph-line to draw the graphs of the following lines:
Exercise 23.5.2
It is a standard mathematical exercise to develop the equation for a line
from a point on the line and its slope. Look up the method in your
mathematics book. Then develop the function line-from-point+slope,
which implements the method. The function consumes a posn (the
point) and a number (the slope). It produces a function that represents the
line in the spirit of exercise
.
Testing a function-producing function like line-from-point+slope
can be done in two ways. Suppose we apply the function to (0,4) and 1. The
result should be line
from exercise
. To check this,
we can either apply the result of
(line-from-point+slope (make-posn 0 4) 1)to some numbers, or we can draw the result using the operations in graphing.ss. In the first case, we must use
Once we have an intersecting line through
and
, we can also get a line with the proper slope. By
decreasing
until it is (almost) indistinguishable from 0, the two
intersection points move closer and closer until they are one, namely,
(x,f(x)), the point for which we wish to know the slope.
Use the operation graph-fun in the teachpack graphing.ss to draw the mathematical function
The operation works just like draw-line (see
exercise
.)
Suppose we wish to determine the slope at x = 2. Pick an
and
determine the slope of the line that goes through
and
with the above formula. Compute the line with
line-from-point+slope from exercise
and use
draw-line to draw it into the same coordinate system as
y. Repeat the process with
and then with
. Solution
If our goal is to define the differential operator as a Scheme function,
we can approximate it by setting
to a small number and by
translating the mathematical formula into a Scheme expression:
;; d/dx : (num -> num) -> (num -> num)
;; to compute the derivative function of f numerically
(define (d/dx f)
(local ((define (fprime x)
(/ (- (f (+ x
)) (f (- x
)))
(* 2
)))
(define
...))
fprime))
Note that d/dx consumes and produces functions--just like the
differential operator in mathematics.
As mentioned in the introduction to this section, the differential operator computes the function f' from some function f. The former computes the slope of f for any x. For straight lines, the slope is always known. Hence a function that represents a straight line is an ideal test case for d/dx. Let us consider
(define (a-line x) (+ (* 3 x) 1))The evaluation of (d/dx a-line) proceeds as follows:
(d/dx a-line)= (local ((define (fprime x) (/ (- (a-line (+ x
)) (a-line (- x
))) (* 2
))) (define
...)) fprime)
= (define (fprime x) (/ (- (a-line (+ x
)) (a-line (- x
))) (* 2
))) (define
...) fprime
Now, if we think of (+ x
) and (- x
) as
numbers, we can evaluate the application of a-line in the
definition of fprime:
(define (fprime x)
(/ (- (+ (* 3 (+ x
)) 1) (+ (* 3 (- x
)) 1))
(* 2
)))
= (define (fprime x)
(/ (- (* 3 (+ x
)) (* 3 (- x
)))
(* 2
)))
= (define (fprime x)
(/ (* 3 (- (+ x
) (- x
)))
(* 2
)))
= (define (fprime x)
(/ (* 3 (* 2
))
(* 2
)))
= (define (fprime x)
3)
In other words, the result of (d/dx a-line) always returns 3,
which is the slope of a-line. In short, we not only got a close
approximation because
Pick a small
and use d/dx to compute the slope of
at x = 2. How does the result compare with your calculation in
exercise
? Solution
Exercise 23.5.5
Develop the function line-from-two-points. It consumes two points p1 and p2. Its result is a Scheme function that represents the line through p1 and p2.
Question: Are there any situations for which this function may fail to compute a function? If so, refine the definition to produce a proper error message in this case. Solution
Exercise 23.5.6
Compute the slope of the following function
(define (f x)
(+ (* 1/60 (* x x x))
(* -1/10 (* x x))
5))
at x = 4. Set