# 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: ${\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).
For example, if
then
On the other hand, differentiating with respect to t instead of x gives:
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

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:
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:
to:
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:
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:
Now let's test:
This output assumes the toString method of term performs the simplifications discussed in the previous chapter.

## What about the value method?

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

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/2gt^{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.

## Footnotes

* ↑ 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 →