The Science of Programming/Slippery Slopes

From Wikibooks, open books for an open world
Jump to: navigation, search

In Chapter X of CME, SPT states that the derivative (at a given point) can be viewed as the slope of the tangent to a curve (at that point). Let's use our software to examine slopes.

Consider parabolas of the form:

    y = (x - h)^2 + v 

The h variable induces a rightward shift of the parabola if h is positive and a leftward shift if h is negative. The v variable induces an upward shift if positive and a downward shift if negative. Thus, these curves bottom out at the point (h,v). For h = 3 and v = 1, we get:

    y = (x - 3)^2 + 1 
    y = (x - 3)(x -3) + 1 
    y = x^2 - 3x - 3x + 9 + 1 
    y = x^2 - 6x + 10 

Plotting the parabola yields:


We can see from the plot that when x = 3, the curve bottoms out and the tangent at the point should be horizontal, having a slope of zero. To the right of that point, the slope rises to the left, so the derivative in that area should be positive. To the left of the bottom point, the slope rises to the left, so the derivative in that area should be negative.

To confirm our intuition, we shall encode the parabola as a sum of terms:

   var y = term(1,:x,2) minus term(6,:x,1) plus term(10,:x,0);

and take the derivative:

   var y' = y . diff(:x);

Let's look at y and its derivative at x = 2,3,4:

   sway> y . value(2)
   sway> y . value(3)
   sway> y . value(4)
   sway> y' . value(2)
   INTEGER: -2
   sway> y' . value(3)
   sway> y' . value(4)

Just as we suspected.

Solving problems[edit]

Let's take a break from programming and use the software we've been given to solve some problems given in CME.



Working on the Chain Gang · Peaks and Valleys