# Grokking Calculus

The way to get insight about mathematics is to visualize the various relationships between variables--by plotting for example. A function [f(x)] is a mathematical expression that assigns an output value for each input value (x). There are all kinds of weird functions that mathematicians can dream up, for example, a function that can give several output values for every input; or, a function where closely spaced input gives disjointed output. Physicists and engineers generally prefer to use functions that are very smooth and give a single output value for every input and, in which the output varies continually with the input, for example, as in Fig. 1. These are the kinds of functions associated with many physical processes of interest. Calculus normally deals with the properties of these types of functions.

Calculus is an important branch of mathematics that is a vital part of the language of physics. It is used in a variety of areas of study, such as: engineering, chemistry, geology, economics, programming, signal processing, electronics, astronomy, biology, architecture, and all kinds of modelling and simulation. The methods of calculus expressed as finite differences are valuable in solving all kinds of dynamical problems on the computer, such as those governing weather systems, or the flow of air over an airplane wing.

## Two Kinds of Problems in Calculus

Calculus deals with two types of mathematical problems:

1. Differential calculus addresses the problem of determining the slope of a tangent line drawn at any given point on a curve that is generated by some formula (Eg., Fig. 1a). A tangent line at a point is one that goes through that point without touching nearby points on that curve. The slope of a tangent line to the curve at a given point gives the rate of change of the output variable relative to the input variable.

2. Integral calculus addresses the problem of finding the area between a curve that is generated by a formula as a function of x and the x-axis (Eg., Fig. 2).

Both problems use the idea of limiting values, the theory of limits, which is the idea that a function or sequence of numbers approaches some limiting value as the input, or independent variable, approaches some value. Specifically in the case of calculus, the ratio of two small quantities progressively approaches some limiting value as the quantities get progressively smaller

Calculus is presented here for those who are interested in wading deeply into the ideas of physics. I will attempt to present a discussion of physics using as few equations as possible, but with reference to the mathematical parts, by hyperlinks for those who may be interested in greater detail.

The history and mathematical development of calculus are discussed in detail in the indicated hyperlink to a Wikipedia article on calculus

The kind of function that lends itself best to the methods of calculus is one that plots to a curve on a certain scale, but approaches progressively a straight line at smaller scales. For example, the whole Earth is round, but as we approach Lake Bonneville, the surface of the Earth gets flatter. Straight lines drawn on that surface become circles if extended around the Earth. However, at some smaller scale reality gets rough again. In the case of Lake Bonneville, the ground is very flat until you shrink your point of view to that of a small ant, which can see the grains of sand and salt as huge and so cannot draw a straight line on that surface. The kind of flatting that mathematicians imagine for this class of functions is one that continues to get progressively flatter on progressively smaller scales, until the function is almost indistinguishable from a straight line.

In the following examples, a snippet of python code (see listings) precedes the figures with corresponding numbering, which it generates. If you run the code, make sure that you run the snippets in sequence so that you do not lose initial values.

Let us begin developing the ideas of calculus by concentrating on single valued functions f(x) of one independent variable (x). It is possible to extend this treatment to single-valued functions of several variables, but we do not do that here in the interest of keeping the discussion as simple as possible.

Consider a plot of x, y [= f(x)], which generates a curve on a two-dimensional piece of graph paper that is ruled as a Cartesian grid, viz., the familiar x and y axes perpendicular to each other. In this case the curve consists of all the points (x, y) generated by assigning the value of y to the function of x within a given, continuous range in the input variable,

y=f(x), x1< x < x2  .                                   (1)

Note that (1) makes the y-values entirely dependent on x-values, and that the range of y (its domain) is entirely limited to the domain over which x is defined. At x=x1, y1 =f(x1) and at x=x2, y2 =f(x2) and the maxima and minima of y in the middle of that range.

Listing 1 a

#______________________________________________________________________________________________________________

ipython –pylab
from pylab import *
a=1.0
x=arange(0,1.01, 0.01)
y=a*sin(math.pi*x)
figure()
p=fill([0.,0.,1.0,1.0],[0, 1.5*a,1.5*a,0], facecolor=’w')
# plot([0.,1.0],[1.5*a,1.5*a], color=’w')
lines=plot(x,y)
plt.setp(p, edgecolor=’w', linewidth=2.0)
x0=0.35
y0=a*sin(math.pi*x0)
plot(x0,y0, ‘ko’, markeredgecolor=’r')
dydx0=math.pi*a*cos(math.pi*x0)
ytan=y0+(x-x0)*dydx0
tline=plot(x,ytan, color=’r')
xlabel(‘x’, color=’k')
ylabel(‘y’, color=’k')
title(“Function and Tangent line”, color=’k')
text(x0-0.05,y0+.1,r’(x0,y0)’)

#______________________________________________________________________________________________________________

Fig. 1a Line tangent to a sine function at the point (x0, y0).

The mathematical function plotted in Fig. 1a was generated by python code in Listing 1a. Note that the plot becomes progressively more linear on progressively smaller intervals , until the curve segment resembles a straight line. (See Figs. 1a-1c and corresponding listings.)

### Listing 1b (Python code for generating Fig. 1b

#___________________________________________________________________

x1=x0-0.05
x2=x0+0.05
x=arange(0,1.01, 0.01)*(x2-x1)+x1
y=a*sin(math.pi*x)
y1=a*sin(math.pi*x1)
y2=a*sin(math.pi*x2)
figure()
p=fill([x1,x2,x2,x1,x1],[y1,y1,y2,y2,y1], facecolor='w')
plt.setp(p, edgecolor='w', linewidth=2.0)
lines=plot(x,y)
plot(x0,y0, 'ko', markeredgecolor='r')
dydx0=math.pi*a*cos(math.pi*x0)
ytan=y0+(x-x0)*dydx0
tline=plot(x,ytan, color='r')
xlabel('x', color='k')
ylabel('y', color='k')
title("Function and Tangent line", color='k')
text(x0-0.005,y0+.01,r'(x0,y0)')

#__________________________________________________________________

Fig. 1b As in Fig. 1a, but defined over a smaller range of x.

The Figs. 1a -1c illustrate how over a progressively smaller range of x, the values of y given by (1) for the function,

f(x)=sin(π*x),

generate a series of plots progressively approaching the tangent line at the point (x0, y0), where the value of x at that point is x0 =3.5 (Fig. 3).

Listing 1c

#______________________________________________________________

x1=x0-0.005
x2=x0+0.005
x=arange(0,1.01, 0.01)*(x2-x1)+x1
y=a*sin(math.pi*x)
y1=a*sin(math.pi*x1)
y2=a*sin(math.pi*x2)
figure()
p=fill([x1,x2,x2,x1,x1],[y1,y1,y2,y2,y1], facecolor='w')
plt.setp(p, edgecolor='w', linewidth=2.0)
lines=plot(x,y+(1-rnd())))
plot(x0,y0, 'ko', markeredgecolor='r')
dydx0=math.pi*a*cos(math.pi*x0)
ytan=y0+(x-x0)*dydx0
tline=plot(x,ytan, color='r')
xlabel('x', color='k')
ylabel('y', color='k')
title("Function and Tangent line", color='k')
text(x0-0.005,y0+.01,r'(x0,y0)')

#______________________________________________________________

Fig. 1c As in Fig. 1a and 1b but in a still more restricted range.

### The Derivative as the Limit of a Sequence of Ratios

Listing 3 is a python snippet for estimating the value of the derivative of the function (2) at the point x0 =3.5 and y0 = f(x0). The derivative is estimated by the ratios,

df/dx ≈ [f(x0+xs)-f(x0)]/xs,        (3a)
over the sequence of displacements
xs=1.00,....., 0.1, 0.01, 0.001, 0.0001, 0.00001 ,

where the symbol,≈, stands for approximately equal. In (3b), the interval over which the derivative is estimated (xs) drops by a power of ten over what it is in Fig. 3a. Note that over the smaller range, the approach of xs to zero on the scale of 0.01 is almost linear, and the limit approximated by looking at the graph is about 1.46. Python gives as the estimate of the derivative for the smallest value of xs (0.00001) as 1.42620925, which compares well to the value of the actual derivative computed by the methods of calculus and evaluated with python, 1.42625321878. The two differ only in the 5th decimal place by about 4 x 10-5, which means that with a computer, we can estimate the value of derivatives fairly well using finite differences.

In physics problems, one does not normally require high precision given by python, especially when working with experimental values, which have a scale of measurement error that renders writing the number at higher precision meaningless. What python does is not to introduce further error by treating the numbers entering a calculation with a high precision. What physicists do in this situation, is to estimate the size of error in the result of calculations by the method of propagation of errors, which is adopted from calculus, and then round off the final number adding an estimate of the error as ± some smaller value.

Listing 2

#_________________________________________________________________

a=1.0
x=arange(0,1.01, 0.01)
y=a*sin(math.pi*x)
x1=concatenate((x,x[::-1]))
y1=concatenate((0*y,y[::-1]))
figure()
p=fill(x, y, facecolor='g')
plt.setp(p, edgecolor='w', linewidth=2.0)
lines=plot(x,y, color='r')
plt.setp(lines, edgecolor='r', linewidth=2.0)
plot([0.,1.0],[1.5*a,1.5*a], color='w')
xlabel('x', color='k')
ylabel('y', color='k')
title("Integral as an area under a curve", color='k')

#__________________________________________________________________

Fig. 2 An Integral as the area under a curve that is generated by a function of x.

Listing 3

#__________________________________________________________________

a=1.0

figure()

# define an array that defines decreasing increments of x about point defined by x0:
xs=array([1.00,0.1,0.01, 0.001, 0.0001, 0.00001])

#an array of derivative estimates at shrinking scales of dx=xs
dydxs=(a*sin(math.pi*(x0+xs))-y0)/xs
pdp=plot(xs, dydxs)

# plot derivative computed by methods of calculus at the limit, xs=0:
plot(0,dydx0, 'ko', markeredgecolor='r')
xlabel('delx', color='k')
ylabel('dely/delx estimate', color='k')

#___________________________________________________________________

Fig. 3a The derivative of the function at the point (x0,y0) computed from the limit of ratio of dy/dx.

### Definition of a Derivative

The derivative of a function ( df(x)/dy) is itself a function that is given at every point in the range of both the independent and output variable through a procedure that involves finding the limit of the ratio of increment of the output to that of the input:

df(x)/dy=Lim Δx—>0 [f(x+Δx)-f(x)]/Δx.   (4a)

Note that the ratio is not defined numerically at the limit, but anywhere as close as you want to approach Δx=0. In Fig. 3, the approach was generated by descending orders of magnitude, which in listing 1 are given as:   Δx=array([1.00,0.1,0.01, 0.001, 0.0001, 0.00001]).

Fig. 3b Plot of the ratio of the change in y and the change in x about a restricted range of xhanges in x about the point x0,y0.

Well known functions functions subjected to the above derivative procedure, often give a term that is a function only of x, and trailing terms that contain various powers of Δx. As Δx approaches zero, that function of x remains as the limit.

### Finite Difference Approximation

Sometimes an analyst is using tabulated values of a function that may not be defined mathematically, but are defined by observation. Galileo's observations of motion are examples of this. Such situations arise in signal processing, or in higher dimensions, image processing. In this case, the derivatives of an observed function can be estimated from its tabulated values (xi and fi) by using finite differences. Suppose that the observed input values (xi) are evenly spaced, viz.

xi = xo + i*Δx.

In that case, the derivative of the observed function, fi    is estimated as the ratio of finite increments,

dfi/dx≈ (fi+1- fi-1 )/ (xi+1- xi-1 ),

Which is called the centered difference approximation.

Listing 4

#__________________________________________________________________

dydx=0.*x
for i in range(1,99):
dydx[i]=(y[i+1]-y[i-1])/(x[i+1]-x[i-1])

figure()
pdp=plot(x[1:99], dydx[1:99])
dydxf=math.pi*a*cos(math.pi*x)
plot(x[1:99], dydxf[1:99], color='r')
xlabel('x', color='k')
ylabel('y', color='k')
title("Actual(red) and Estimated derivative(blue)", color='k')

#_________________________________________________________________

Fig. 4 Actual and estimated centred difference derivative

The validity of the finite difference approximation is tested in Fig. 4, where the derivative computed by the methods of calculus is first plotted in blue, then the centred difference estimate is plotted on top of it in red. Note that over the entire range of the plot, the estimated derivative completely covers that of the analytically computed derivative.

### Derivatives of Functions that are Powers of x

Below, the various derivatives are derived, showing almost every step in the derivation

_____________________________________________________________________

f(x)=x

df(x)/dx = lim Δx —>0 in [f(x+Δx)-f(x)]/[x+Δx-x]

df(x)/dx =lim xs—>0 in [Δx/Δx]

df(x)/dx = 1

dx/dx =1                                 (5)

___________________________________________________________________

df(x)/dx =x2

df(x)/dx = lim Δx—>0 in [f(x+Δx)-f(x)]/[x+Δx-x]

df(x)/dx = lim Δx—>0 in {[(x+Δx)2- x2)]/Δx}

df(x)/dx = lim Δx—>0 in {[(x2+2x*Δx+Δx 2)- x2]/Δx}

df(x)/dx = lim Δx—>0 in {[2x*Δx+Δx2 ]/Δx}

df(x)/dx = lim Δx—>0 in {2x+Δx]}

df(x)/dx = 2x                                                   (6)

It is possible to expand the function, (x+Δx)n, as a power series in Δx,

(x+Δx)n= xn+n*xn-1 * Δx + const.* xn-2 * Δx2   +...+Δxn.

The constant terms in front of the various products of powers of x and Δx, called binomial coefficients,do not matter here, except for the first two, because they stand in front of terms that go to zero.

df(xn)/dx = lim Δx—>0 in [(x+Δx)n - xn ]/Δx

df(xn)/dx = lim Δx—>0 in [xn+n*xn-1*Δx+ const.* xn-2 * Δx2   +...+Δxn - xn]/Δx

d(xn)dx = lim Δx—>0 in [n*xn-1+ const.* xn-2 * Δx   +...+Δxn-1]

d(xn)dx = n*xn-1.                  (7)

### Endnote

There are properties of derivatives that should be added here and are listed as follows:

if g(x)= A*f(x), where A is constant then

dg(x)/dx=A*df(x)/dx.

The derivative of a sum and product of two is given by the following two formulas:

d[g(x)*f(x)]/dx=f(x)*dg(x)/dx+g(x)*df(x)/dx

d(g(x)+f(x))/dx=dg(x)/dx + df(x)/dx.

Some of the methods of integral calculus will be discussed in the next blog entry on the subject of calculus, and derivatives of trigonometric function are also discussed.

d f(x(t))/dt = [df(x)/dx]*dx(t)/dt

This entry was posted in calculus, derivatives, differential calculus, mathematics, physics, programming, python and tagged , , , , , , . Bookmark the permalink.