The Science of Programming/As Time Goes By

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

In CME, chapter 8, SPT discusses how Newton used a dot over a variable to indicate differentiation while Liebniz used the notation in this book: \frac{dy}{dx}. The advantage to Liebniz's notation is that it explicitly states which independent variable is to be considered when differentiation occurs (in case there is more than one independent variable).[1]

For example, if

    y = 3x^2 - 5 


    \frac{dy}{dx} = 6x 

On the other hand, differentiating with respect to t instead of x gives:

    \frac{dy}{dt} = 0 

since 3x^2 - 5 is considered a constant with respect to the independent variable t. That is to say, no matter how much you change the value of t, the value of 3x^2 - 5 doesn't change (since you are not changing x, just t).

We have been assuming, up until now, that the independent variable and the variable with respect to which we take derivatives are one and the same.

Let's investigate what our code might look like if we did not make that assumption.

An unassuming differentiation system[edit]

Based upon our visualization for the term constructor, we have been assuming the independent variable is x, because we end up with visualization like:


If we don't hard-wire the independent variable, we will need to pass it in. Here is a skeleton of a version of term where the name of the independent variable is to be passed in:

   function term(a,iv,n)
       function value(x) { ... }
       function toString() { ... }
       function diff(wrtv) { ... }

The first major difference between the new version of term and the old version is that the new term has three formal parameters, instead of two. The second formal parameter, iv, which stands for the independent variable, represents the independent variable. We presume it will be be bound to symbols such as x, t, and the like. We see this in the basic toString method (the one with no simplifications) which changes from:

   function toString() { "" + a + "x^" + n; }


   function toString() { "" + a + iv + "^" + n; }

Note that x in the original version was part of a string and thus fixed. In the second, iv is a variable that is (or rather will be) bound to a symbol. Looking at a visualization will help us sort things out:

   var t = term(5,:w,3);
   sway> t . toString();
   STRING: 5w^3

The second major change is that the differentiation method, diff, now takes an argument, wrtv, which stands for the with-respect-to variable. This is the variable with respect to which differentiation should proceed. If the independent variable and the with-respect-to variable are the same, differentiation proceeds as before. If not, a constant zero is generated:

   function diff(wrtv)
       if (wrtv == iv)
           if(n == 0,term(0,iv,0),term(a * n,iv,n - 1));

Now let's test:

   var a = t . diff(:w);   //iv and wrtv match
   var b = t . diff(:x);   //iv and wrtv do not match
   sway> t . toString();
   STRING: 5w^3
   sway> a . toString();
   STRING: 15w^2
   sway> b . toString();
   STRING: 0

This output assumes the toString method of term performs the simplifications discussed in the previous chapter.

What about the value method?[edit]

We do not need to change the value method for terms. This is because a value for the independent variable is used, rather than its name. In other words, the value method performs a numeric calculation, while the diff method performs a symbolic calculation. Thus, value needs a number and diff needs a name.

Since both toString and diff use the name of a term variable rather than a value, they both needed modification because names are not longer hard-wired.


1. What happens if we rename the formal parameter iv to be x and replace every occurrence of iv with x. Explain.

2. Redefine the simplifying term constructor so that it does not assume the independent variable to be x.

3. Redefine the simplifying sum constructor so that it does not assume the independent variable to be x.

4. Complete the unassuming differentiation system (minus, times, and div).

5. Using sway do 2 on p. 92.

6. Using sway do 4 and 5 on p. 92.

7. We are doing a game to throw a beanbag into a trash can and we want to model the physics correctly. The horizontal displacement is  x = x_0 + v_{x_0} t and the vertical displacement is  z = z_0 + v_{z_0} t - 1/2 g t^2 . What is change of both x and z with t? Let x_0 = 0, v_{x_0} = 3, z_0 = 5, and v_{z_0} = 3. Plot x and z, and the velocity in x and z directions versus time from 0 to 10.


  1. As SPT points out, the dot notation is used with the assumtion that differentiation is performed with respect to time t. Hence, the name of this chapter.

Lather, Rinse, Repeat · Working on the Chain Gang