# The Science of Programming/As Time Goes By

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: .
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

```
```

then

```
```

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

```
```

since 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 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

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

3x^2

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) { ... } this; }

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 **i**ndependent **v**ariable, 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; }

to:

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
**w**ith-**r**espect-**t**o **v**ariable. 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)); } else { constant(0); } }

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.

## Questions

edit**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
and the vertical displacement is
.
What is change of both *x* and *z* with *t*?
Let ,
,
, and
.
Plot *x* and *z*,
and the velocity in *x* and *z* directions versus time from 0 to 10.

## Footnotes

edit- ↑
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.