[previous] [up] [next]     [index]
Next: Intermezzo 4 Defining Functions on Up: Mathematical Examples Previous: The Area Under a

The Slope of a Function

Let us take another look at the function graph in figure [cross-reference].
external
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'.


external

Figure: The graph of some function


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, tex2html_wrap_inline72589 and tex2html_wrap_inline72591 ; the constant tex2html_wrap_inline72593 , 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 [cross-reference]. If the point of interest has coordinate x, the two points are tex2html_wrap_inline72597 and tex2html_wrap_inline72599 . Hence the slope of the line is

displaymath72601

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.


Exercises Exercise 23.5.1

The equation for a line is

displaymath72603

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 one operation 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:

  1. tex2html_wrap_inline72605
  2. tex2html_wrap_inline72607
  3. tex2html_wrap_inline72609
  4. tex2html_wrap_inline72611
  5. tex2html_wrap_inline72613
 Solution

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 [cross-reference].

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 tex2html_wrap_inline72617 from exercise [cross-reference]. 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 tex2html_wrap_inline72617 to compare outputs; in the second case we can draw the result in one color and the hand-constructed line in a different one and observe the effect. Solution

Once we have an intersecting line through tex2html_wrap_inline72597 and tex2html_wrap_inline72599 , we can also get a line with the proper slope. By decreasing tex2html_wrap_inline72593 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.[footnote]


Exercises Exercise 23.5.3

Use the operation graph-fun in the teachpack graphing.ss to draw the mathematical function

displaymath72631

The operation works just like draw-line (see exercise [cross-reference].)

Suppose we wish to determine the slope at x = 2. Pick an tex2html_wrap_inline72635 and determine the slope of the line that goes through tex2html_wrap_inline72597 and tex2html_wrap_inline72599 with the above formula. Compute the line with line-from-point+slope from exercise [cross-reference] and use draw-line to draw it into the same coordinate system as y. Repeat the process with tex2html_wrap_inline72641 and then with tex2html_wrap_inline72643Solution


If our goal is to define the differential operator as a Scheme function, we can approximate it by setting tex2html_wrap_inline72593 to a small number and by translating the mathematical formula into a Scheme expression:
external

;; d/dx : (num -> num) -> (num -> num)
;; to compute the derivative function of f numerically 
(define (d/dx f) 
  (local ((define (fprime x) 
            (/ (- (f (+ x  tex2html_wrap_inline72593 )) (f (- x  tex2html_wrap_inline72593 ))) 
               (* 2  tex2html_wrap_inline72593 ))) 
          (define  tex2html_wrap_inline72593  ...)) 
    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 tex2html_wrap_inline72593 )) (a-line (- x tex2html_wrap_inline72593 ))) (* 2 tex2html_wrap_inline72593 ))) (define tex2html_wrap_inline72593 ...)) fprime)

= (define (fprime x) (/ (- (a-line (+ x tex2html_wrap_inline72593 )) (a-line (- x tex2html_wrap_inline72593 ))) (* 2 tex2html_wrap_inline72593 ))) (define tex2html_wrap_inline72593 ...) fprime

Now, if we think of (+ x tex2html_wrap_inline72593 ) and (- x tex2html_wrap_inline72593 ) as numbers, we can evaluate the application of a-line in the definition of fprime:[footnote]

  (define (fprime x)
    (/ (- (+ (* 3 (+ x  tex2html_wrap_inline72593 )) 1) (+ (* 3 (- x  tex2html_wrap_inline72593 )) 1)) 
       (* 2  tex2html_wrap_inline72593 )))

= (define (fprime x) (/ (- (* 3 (+ x tex2html_wrap_inline72593 )) (* 3 (- x tex2html_wrap_inline72593 ))) (* 2 tex2html_wrap_inline72593 )))

= (define (fprime x) (/ (* 3 (- (+ x tex2html_wrap_inline72593 ) (- x tex2html_wrap_inline72593 ))) (* 2 tex2html_wrap_inline72593 )))

= (define (fprime x) (/ (* 3 (* 2 tex2html_wrap_inline72593 )) (* 2 tex2html_wrap_inline72593 )))

= (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 tex2html_wrap_inline72593 is small, we actually got the correct answer. In general, however, the answer will depend on tex2html_wrap_inline72593 and will not be precise.


Exercises Exercise 23.5.4

Pick a small tex2html_wrap_inline72593 and use d/dx to compute the slope of

displaymath72713

at x = 2. How does the result compare with your calculation in exercise [cross-reference]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 tex2html_wrap_inline72593 to 2, 1, .5. Try the same for some other value of xSolution


[previous] [up] [next]     [index]
Next: Intermezzo 4 Defining Functions on Up: Mathematical Examples Previous: The Area Under a

PLT