# PHY321: More on Motion and Forces, begin Work and Energy  discussion

**[Morten Hjorth-Jensen](http://mhjgit.github.io/info/doc/web/)**, Department of Physics and Astronomy and Facility for Rare Ion Beams (FRIB), Michigan State University, USA and Department of Physics, University of Oslo, Norway

Date: **Feb 3, 2021**

Copyright 1999-2021, [Morten Hjorth-Jensen](http://mhjgit.github.io/info/doc/web/). Released under CC Attribution-NonCommercial 4.0 license

## Aims and Overarching Motivation

### Monday

We discuss various forces and their pertinent equations of motion

Recommended reading: Taylor 2.1-2.4. Malthe-Sørenssen chapter 6-7 contains many examples.
We will cover in particular a falling object in two dimensions with linear air resistance relevant for homework 3.

### Wednesday

We discuss other force models with examples such as the gravitational
force and a spring force. See Malthe-Sørenssen chapter 7.3-7.5. We
start our discussion of energy and work, see Taylor 4.1

We discuss also exercise 5 from homework 2.

### Friday

We discuss several examples of energy and work. Taylor 4.1-4.3.

## Air Resistance in One Dimension

Last week we considered the motion of a falling object with air
resistance. Here we look at both a quadratic in velocity resistance
and linear in velocity.  But first we give a qualitative argument
about the mathematical expression for the air resistance we used last
Friday.

Air resistance tends to scale as the square of the velocity. This is
in contrast to many problems chosen for textbooks, where it is linear
in the velocity. The choice of a linear dependence is motivated by
mathematical simplicity (it keeps the differential equation linear)
rather than by physics. One can see that the force should be quadratic
in velocity by considering the momentum imparted on the air
molecules. If an object sweeps through a volume $dV$ of air in time
$dt$, the momentum imparted on the air is

$$
\begin{equation}
dP=\rho_m dV v,
\label{_auto1} \tag{1}
\end{equation}
$$

where $v$ is the velocity of the object and $\rho_m$ is the mass
density of the air. If the molecules bounce back as opposed to stop
you would double the size of the term. The opposite value of the
momentum is imparted onto the object itself. Geometrically, the
differential volume is

$$
\begin{equation}
dV=Avdt,
\label{_auto2} \tag{2}
\end{equation}
$$

where $A$ is the cross-sectional area and $vdt$ is the distance the
object moved in time $dt$.

## Resulting Acceleration
Plugging this into the expression above,

$$
\begin{equation}
\frac{dP}{dt}=-\rho_m A v^2.
\label{_auto3} \tag{3}
\end{equation}
$$

This is the force felt by the particle, and is opposite to its
direction of motion. Now, because air doesn't stop when it hits an
object, but flows around the best it can, the actual force is reduced
by a dimensionless factor $c_W$, called the drag coefficient.

$$
\begin{equation}
F_{\rm drag}=-c_W\rho_m Av^2,
\label{_auto4} \tag{4}
\end{equation}
$$

and the acceleration is

$$
\begin{eqnarray}
\frac{dv}{dt}=-\frac{c_W\rho_mA}{m}v^2.
\end{eqnarray}
$$

For a particle with initial velocity $v_0$, one can separate the $dt$
to one side of the equation, and move everything with $v$s to the
other side. We did this in our discussion of simple motion and will not repeat it here.

On more general terms,
for many systems, e.g. an automobile, there are multiple sources of
resistance. In addition to wind resistance, where the force is
proportional to $v^2$, there are dissipative effects of the tires on
the pavement, and in the axel and drive train. These other forces can
have components that scale proportional to $v$, and components that
are independent of $v$. Those independent of $v$, e.g. the usual
$f=\mu_K N$ frictional force you consider in your first Physics courses, only set in
once the object is actually moving. As speeds become higher, the $v^2$
components begin to dominate relative to the others. For automobiles
at freeway speeds, the $v^2$ terms are largely responsible for the
loss of efficiency. To travel a distance $L$ at fixed speed $v$, the
energy/work required to overcome the dissipative forces are $fL$,
which for a force of the form $f=\alpha v^n$ becomes

$$
\begin{eqnarray}
W=\int dx~f=\alpha v^n L.
\end{eqnarray}
$$

For $n=0$ the work is
independent of speed, but for the wind resistance, where $n=2$,
slowing down is essential if one wishes to reduce fuel consumption. It
is also important to consider that engines are designed to be most
efficient at a chosen range of power output. Thus, some cars will get
better mileage at higher speeds (They perform better at 50 mph than at
5 mph) despite the considerations mentioned above.

## Going Ballistic, Projectile Motion or a Softer Approach, Falling Raindrops

As an example of Newton's Laws we consider projectile motion (or a
falling raindrop or a ball we throw up in the air) with a drag force. Even though air resistance is
largely proportional to the square of the velocity, we will consider
the drag force to be linear to the velocity, $\boldsymbol{F}=-m\gamma\boldsymbol{v}$,
for the purposes of this exercise.

Such a dependence can be extracted from experimental data for objects moving at low velocities, see for example Malthe-Sørenssen chapter 5.6.

We will here focus on a two-dimensional problem.

## Two-dimensional falling object

The acceleration for a projectile moving upwards,
$\boldsymbol{a}=\boldsymbol{F}/m$, becomes

$$
\begin{eqnarray}
\frac{dv_x}{dt}=-\gamma v_x,\\
\nonumber
\frac{dv_y}{dt}=-\gamma v_y-g,
\end{eqnarray}
$$

and $\gamma$ has dimensions of inverse time.

If you on the other hand have a falling raindrop, how do these equations change? See for example Figure 2.1 in Taylor.
Let us stay with a ball which is thrown up in the air at $t=0$.

## Ways of solving these equations

We will go over two different ways to solve this equation. The first
by direct integration, and the second as a differential equation. To
do this by direct integration, one simply multiplies both sides of the
equations above by $dt$, then divide by the appropriate factors so
that the $v$s are all on one side of the equation and the $dt$ is on
the other. For the $x$ motion one finds an easily integrable equation,

$$
\begin{eqnarray}
\frac{dv_x}{v_x}&=&-\gamma dt,\\
\nonumber
\int_{v_{0x}}^{v_{x}}\frac{dv_x}{v_x}&=&-\gamma\int_0^{t}dt,\\
\nonumber
\ln\left(\frac{v_{x}}{v_{0x}}\right)&=&-\gamma t,\\
\nonumber
v_{x}(t)&=&v_{0x}e^{-\gamma t}.
\end{eqnarray}
$$

This is very much the result you would have written down
by inspection. For the $y$-component of the velocity,

$$
\begin{eqnarray}
\frac{dv_y}{v_y+g/\gamma}&=&-\gamma dt\\
\nonumber
\ln\left(\frac{v_{y}+g/\gamma}{v_{0y}-g/\gamma}\right)&=&-\gamma t_f,\\
\nonumber
v_{fy}&=&-\frac{g}{\gamma}+\left(v_{0y}+\frac{g}{\gamma}\right)e^{-\gamma t}.
\end{eqnarray}
$$

Whereas $v_x$ starts at some value and decays
exponentially to zero, $v_y$ decays exponentially to the terminal
velocity, $v_t=-g/\gamma$.

## Solving as differential equations

Although this direct integration is simpler than the method we invoke
below, the method below will come in useful for some slightly more
difficult differential equations in the future. The differential
equation for $v_x$ is straight-forward to solve. Because it is first
order there is one arbitrary constant, $A$, and by inspection the
solution is

$$
\begin{equation}
v_x=Ae^{-\gamma t}.
\label{_auto5} \tag{5}
\end{equation}
$$

The arbitrary constants for equations of motion are usually determined
by the initial conditions, or more generally boundary conditions. By
inspection $A=v_{0x}$, the initial $x$ component of the velocity.

## Differential Equations, contn
The differential equation for $v_y$ is a bit more complicated due to
the presence of $g$. Differential equations where all the terms are
linearly proportional to a function, in this case $v_y$, or to
derivatives of the function, e.g., $v_y$, $dv_y/dt$,
$d^2v_y/dt^2\cdots$, are called linear differential equations. If
there are terms proportional to $v^2$, as would happen if the drag
force were proportional to the square of the velocity, the
differential equation is not longer linear. Because this expression
has only one derivative in $v$ it is a first-order linear differential
equation. If a term were added proportional to $d^2v/dt^2$ it would be
a second-order differential equation.  In this case we have a term
completely independent of $v$, the gravitational acceleration $g$, and
the usual strategy is to first rewrite the equation with all the
linear terms on one side of the equal sign,

$$
\begin{equation}
\frac{dv_y}{dt}+\gamma v_y=-g.
\label{_auto6} \tag{6}
\end{equation}
$$

## Splitting into two parts

Now, the solution to the equation can be broken into two
parts. Because this is a first-order differential equation we know
that there will be one arbitrary constant. Physically, the arbitrary
constant will be determined by setting the initial velocity, though it
could be determined by setting the velocity at any given time. Like
most differential equations, solutions are not "solved". Instead,
one guesses at a form, then shows the guess is correct. For these
types of equations, one first tries to find a single solution,
i.e. one with no arbitrary constants. This is called the {\it
particular} solution, $y_p(t)$, though it should really be called
"a" particular solution because there are an infinite number of such
solutions. One then finds a solution to the {\it homogenous} equation,
which is the equation with zero on the right-hand side,

$$
\begin{equation}
\frac{dv_{y,h}}{dt}+\gamma v_{y,h}=0.
\label{_auto7} \tag{7}
\end{equation}
$$

Homogenous solutions will have arbitrary constants.

The particular solution will solve the same equation as the original
general equation

$$
\begin{equation}
\frac{dv_{y,p}}{dt}+\gamma v_{y,p}=-g.
\label{_auto8} \tag{8}
\end{equation}
$$

However, we don't need find one with arbitrary constants. Hence, it is
called a **particular** solution.

The sum of the two,

$$
\begin{equation}
v_y=v_{y,p}+v_{y,h},
\label{_auto9} \tag{9}
\end{equation}
$$

is a solution of the total equation because of the linear nature of
the differential equation. One has now found a *general* solution
encompassing all solutions, because it both satisfies the general
equation (like the particular solution), and has an arbitrary constant
that can be adjusted to fit any initial condition (like the homogeneous
solution). If the equations were not linear, that is if there were terms
such as $v_y^2$ or $v_y\dot{v}_y$, this technique would not work.

## More details

Returning to the example above, the homogenous solution is the same as
that for $v_x$, because there was no gravitational acceleration in
that case,

$$
\begin{equation}
v_{y,h}=Be^{-\gamma t}.
\label{_auto10} \tag{10}
\end{equation}
$$

In this case a particular solution is one with constant velocity,

$$
\begin{equation}
v_{y,p}=-g/\gamma.
\label{_auto11} \tag{11}
\end{equation}
$$

Note that this is the terminal velocity of a particle falling from a
great height. The general solution is thus,

$$
\begin{equation}
v_y=Be^{-\gamma t}-g/\gamma,
\label{_auto12} \tag{12}
\end{equation}
$$

and one can find $B$ from the initial velocity,

$$
\begin{equation}
v_{0y}=B-g/\gamma,~~~B=v_{0y}+g/\gamma.
\label{_auto13} \tag{13}
\end{equation}
$$

Plugging in the expression for $B$ gives the $y$ motion given the initial velocity,

$$
\begin{equation}
v_y=(v_{0y}+g/\gamma)e^{-\gamma t}-g/\gamma.
\label{_auto14} \tag{14}
\end{equation}
$$

It is easy to see that this solution has $v_y=v_{0y}$ when $t=0$ and
$v_y=-g/\gamma$ when $t\rightarrow\infty$.

One can also integrate the two equations to find the coordinates $x$
and $y$ as functions of $t$,

$$
\begin{eqnarray}
x&=&\int_0^t dt'~v_{0x}(t')=\frac{v_{0x}}{\gamma}\left(1-e^{-\gamma t}\right),\\
\nonumber
y&=&\int_0^t dt'~v_{0y}(t')=-\frac{gt}{\gamma}+\frac{v_{0y}+g/\gamma}{\gamma}\left(1-e^{-\gamma t}\right).
\end{eqnarray}
$$

If the question was to find the position at a time $t$, we would be
finished. However, the more common goal in a projectile equation
problem is to find the range, i.e. the distance $x$ at which $y$
returns to zero. For the case without a drag force this was much
simpler. The solution for the $y$ coordinate would have been
$y=v_{0y}t-gt^2/2$. One would solve for $t$ to make $y=0$, which would
be $t=2v_{0y}/g$, then plug that value for $t$ into $x=v_{0x}t$ to
find $x=2v_{0x}v_{0y}/g=v_0\sin(2\theta_0)/g$. One follows the same
steps here, except that the expression for $y(t)$ is more
complicated. Searching for the time where $y=0$, and we get

$$
\begin{equation}
0=-\frac{gt}{\gamma}+\frac{v_{0y}+g/\gamma}{\gamma}\left(1-e^{-\gamma t}\right).
\label{_auto15} \tag{15}
\end{equation}
$$

This cannot be inverted into a simple expression $t=\cdots$. Such
expressions are known as "transcendental equations", and are not the
rare instance, but are the norm. In the days before computers, one
might plot the right-hand side of the above graphically as
a function of time, then find the point where it crosses zero.

Now, the most common way to solve for an equation of the above type
would be to apply Newton's method numerically. This involves the
following algorithm for finding solutions of some equation $F(t)=0$.

1. First guess a value for the time, $t_{\rm guess}$.

2. Calculate $F$ and its derivative, $F(t_{\rm guess})$ and $F'(t_{\rm guess})$.

3. Unless you guessed perfectly, $F\ne 0$, and assuming that $\Delta F\approx F'\Delta t$, one would choose

4. $\Delta t=-F(t_{\rm guess})/F'(t_{\rm guess})$.

5. Now repeat step 1, but with $t_{\rm guess}\rightarrow t_{\rm guess}+\Delta t$.

If the $F(t)$ were perfectly linear in $t$, one would find $t$ in one
step. Instead, one typically finds a value of $t$ that is closer to
the final answer than $t_{\rm guess}$. One breaks the loop once one
finds $F$ within some acceptable tolerance of zero. A program to do
this will be added shortly.

## Motion in a Magnetic Field

Another example of a velocity-dependent force is magnetism,

$$
\begin{eqnarray}
\boldsymbol{F}&=&q\boldsymbol{v}\times\boldsymbol{B},\\
\nonumber
F_i&=&q\sum_{jk}\epsilon_{ijk}v_jB_k.
\end{eqnarray}
$$

For a uniform field in the $z$ direction $\boldsymbol{B}=B\hat{z}$, the force can only have $x$ and $y$ components,

$$
\begin{eqnarray}
F_x&=&qBv_y\\
\nonumber
F_y&=&-qBv_x.
\end{eqnarray}
$$

The differential equations are

$$
\begin{eqnarray}
\dot{v}_x&=&\omega_c v_y,\omega_c= qB/m\\
\nonumber
\dot{v}_y&=&-\omega_c v_x.
\end{eqnarray}
$$

One can solve the equations by taking time derivatives of either equation, then substituting into the other equation,

$$
\begin{eqnarray}
\ddot{v}_x=\omega_c\dot{v_y}=-\omega_c^2v_x,\\
\nonumber
\ddot{v}_y&=&-\omega_c\dot{v}_x=-\omega_cv_y.
\end{eqnarray}
$$

The solution to these equations can be seen by inspection,

$$
\begin{eqnarray}
v_x&=&A\sin(\omega_ct+\phi),\\
\nonumber
v_y&=&A\cos(\omega_ct+\phi).
\end{eqnarray}
$$

One can integrate the equations to find the positions as a function of time,

$$
\begin{eqnarray}
x-x_0&=&\int_{x_0}^x dx=\int_0^t dt v(t)\\
\nonumber
&=&\frac{-A}{\omega_c}\cos(\omega_ct+\phi),\\
\nonumber
y-y_0&=&\frac{A}{\omega_c}\sin(\omega_ct+\phi).
\end{eqnarray}
$$

The trajectory is a circle centered at $x_0,y_0$ with amplitude $A$ rotating in the clockwise direction.

The equations of motion for the $z$ motion are

$$
\begin{equation}
\dot{v_z}=0,
\label{_auto16} \tag{16}
\end{equation}
$$

which leads to

$$
\begin{equation}
z-z_0=V_zt.
\label{_auto17} \tag{17}
\end{equation}
$$

Added onto the circle, the motion is helical.

Note that the kinetic energy,

$$
\begin{equation}
T=\frac{1}{2}m(v_x^2+v_y^2+v_z^2)=\frac{1}{2}m(\omega_c^2A^2+V_z^2),
\label{_auto18} \tag{18}
\end{equation}
$$

is constant. This is because the force is perpendicular to the
velocity, so that in any differential time element $dt$ the work done
on the particle $\boldsymbol{F}\cdot{dr}=dt\boldsymbol{F}\cdot{v}=0$.

One should think about the implications of a velocity dependent
force. Suppose one had a constant magnetic field in deep space. If a
particle came through with velocity $v_0$, it would undergo cyclotron
motion with radius $R=v_0/\omega_c$. However, if it were still its
motion would remain fixed. Now, suppose an observer looked at the
particle in one reference frame where the particle was moving, then
changed their velocity so that the particle's velocity appeared to be
zero. The motion would change from circular to fixed. Is this
possible?

The solution to the puzzle above relies on understanding
relativity. Imagine that the first observer believes $\boldsymbol{B}\ne 0$ and
that the electric field $\boldsymbol{E}=0$. If the observer then changes
reference frames by accelerating to a velocity $\boldsymbol{v}$, in the new
frame $\boldsymbol{B}$ and $\boldsymbol{E}$ both change. If the observer moved to the
frame where the charge, originally moving with a small velocity $v$,
is now at rest, the new electric field is indeed $\boldsymbol{v}\times\boldsymbol{B}$,
which then leads to the same acceleration as one had before. If the
velocity is not small compared to the speed of light, additional
$\gamma$ factors come into play,
$\gamma=1/\sqrt{1-(v/c)^2}$. Relativistic motion will not be
considered in this course.

## Sliding Block tied to a Wall

Another classical case is that of simple harmonic oscillations, here
represented by a block sliding on a horizontal frictionless
surface. The block is tied to a wall with a spring. If the spring is
not compressed or stretched too far, the force on the block at a given
position $x$ is

$$
F=-kx.
$$

## Back and Forth, Sliding Block with no friction

The negative sign means that the force acts to restore the object to an equilibrium position. Newton's equation of motion for this idealized system is then

$$
m\frac{d^2x}{dt^2}=-kx,
$$

or we could rephrase it as

$$
\frac{d^2x}{dt^2}=-\frac{k}{m}x=-\omega_0^2x,
\label{eq:newton1} \tag{19}
$$

with the angular frequency $\omega_0^2=k/m$.

We will derive the above force when we start studying **harmonic oscillations**.

## Final rewrite

With the position $x(t)$ and the velocity  $v(t)=dx/dt$ we can reformulate Newton's equation in the following way

$$
\frac{dx(t)}{dt}=v(t),
$$

and

$$
\frac{dv(t)}{dt}=-\omega_0^2x(t).
$$

With initial conditions $x(t_0)=x_0$ and $v(t_0)=v_0$ we can in turn solve the differential equations.

## Analytical Solution

The above differential equation has the advantage that it can be
solved analytically with general solutions on the form

$$
x(t)=A\cos{\omega_0t}+B\sin{\omega_0t},
$$

and

$$
v(t)=-\omega_0 A\sin{\omega_0t}+\omega_0 B\cos{\omega_0t},
$$

where $A$ and $B$ are constants to be determined from the initial conditions.

This provides in turn an important test for the numerical solution and
the development of a program for more complicated cases which cannot
be solved analytically.

We will discuss the above equations in more detail when we discuss harmonic oscillations.

## Summarizing the various motion problems 1

The examples we have discussed above were included in order to
illustrate various methods (which depend on the specific problem) to
find the solutions of the equations of motion.
We have solved the equations of motion in the following ways:

**Solve the differential equations analytically.**

We did this for example with the following object in one or two dimensions or the sliding block.
Here we had for example an equation set like

$$
\frac{dv_x}{dt}=-\gamma v_x,
$$

and

$$
\frac{dv_y}{dt}=-\gamma v_y-g,
$$

and $\gamma$ has dimension of inverse time.

## Summarizing the various motion problems 2

**Integrate the equations.**

We could also in case we can separate the degrees of freedom integrate. Take for example one of the equations in the previous slide

$$
\frac{dv_x}{dt}=-\gamma v_x,
$$

which we can rewrite in terms of a left-hand side which depends only on the velocity and a right-hand side which depends only on time

$$
\frac{dv_x}{v_x}=-\gamma dt.
$$

Integrating we have (since we can separate $v_x$ and $t$)

$$
\int_{v_0}^{v_t}\frac{dv_x}{v_x}=-\int_{t_0}^{t_f}\gamma dt,
$$

where $v_f$ is the velocity at a final time and $t_f$ is the final time.
In this case we found, after having integrated the above two sides that

$$
v_f(t)=v_0\exp{-\gamma t}.
$$

## Summarizing the various motion problems 3

**Solve the differential equations numerically.**

Finally, using for example Euler's method, we can solve the
differential equations numerically. If we can compare our numerical
solutions with analytical solutions, we have an extra check of our
numerical approaches.

The example code on the next slide is relevant for homework 3. Here we deal with a falling object in two dimensions. Except for the derivations above with an
air resistance which is linear in the velocity, homework 3 uses a quadratic velocity dependence.

## Code example using Euler's methods

**Note**: this code needs some additional expressions and will not run

```python
%matplotlib inline

# Common imports
import numpy as np
import pandas as pd
from math import *
import matplotlib.pyplot as plt
import os
from pylab import plt, mpl
plt.style.use('seaborn')
mpl.rcParams['font.family'] = 'serif'

#define the gravitational acceleration
g = 9.80655 #m/s^2
# The mass and the drag constant D
D = 0.00245 #mass/length   kg/m
m = 0.2 #kg, mass of falling object
DeltaT = 0.001
#set up final time, here just a number we have chosen
tfinal = 1.0
# set up number of points for all variables
n = ceil(tfinal/DeltaT)
# set up arrays for t, a, v, and y and arrays for analytical results
# Note the brute force setting up of arrays for x and y, vx, vy, ax and ay
# For hw3 you should think of using the 2-dim vectors you used in homework 2
t = np.zeros(n)
vy = np.zeros(n)
y = np.zeros(n)
vx = np.zeros(n)
x = np.zeros(n)
# Initial conditions
vx[0] = 10.0 #m/s
vy[0] = 0.0  #m/s
y[0] = 10.0 #m
x[0] = 0.0 #m
# Start integrating using Euler's method
for i in range(n-1):
    # expression for acceleration, you need to set them up
#    ax = You need to set up the expression for force and thereby the acceleration in the x-direction
#    ay = You need to set up the expression for force and thereby the acceleration in the y-direction
    # update velocity and position
    vx[i+1] = vx[i] + DeltaT*ax
    x[i+1] = x[i] + DeltaT*vx[i]
    vy[i+1] = vy[i] + DeltaT*ay
    y[i+1] = y[i] + DeltaT*vy[i]
    # update time to next time step and compute analytical answer
    t[i+1] = t[i] + DeltaT
#  Here you need to set up the analytical solution for y(t) and x(t)

if ( y[i+1] < 0.0):
        break
data = {'t[s]': t,
        'Relative error in y': abs((y-yanalytic)/yanalytic),
        'vy[m/s]': vy,
        'Relative error in x': abs((x-xanalytic)/xanalytic),
        'vx[m/s]': vx
}
NewData = pd.DataFrame(data)
display(NewData)
# save to file
NewData.to_csv(outfile, index=False)
#then plot
fig, axs = plt.subplots(4, 1)
axs[0].plot(t, y)
axs[0].set_xlim(0, tfinal)
axs[0].set_ylabel('y')
axs[1].plot(t, vy)
axs[1].set_ylabel('vy[m/s]')
axs[1].set_xlabel('time[s]')
axs[2].plot(t, x)
axs[2].set_xlim(0, tfinal)
axs[2].set_ylabel('x')
axs[3].plot(t, vx)
axs[3].set_ylabel('vx[m/s]')
axs[3].set_xlabel('time[s]')
fig.tight_layout()
plt.show()
```

## Work, Energy, Momentum and Conservation laws

The previous three cases have shown us how to use Newton’s laws of
motion to determine the motion of an object based on the forces acting
on it. For two of the cases there is an underlying assumption that we can find an analytical solution to a continuous problem.
With a continuous problem we mean a problem where the various variables can take any value within a finite or infinite interval.

Unfortunately, in many cases we
cannot find an exact solution to the equations of motion we get from
Newton’s second law. The numerical approach, where we discretize the continuous problem, allows us however to study a much richer set of problems.
For problems involving Newton's laws and the various equations of motion we encounter, solving the equations numerically, is the standard approach.

It allows us to focus on the underlying forces. Often we end up using the same numerical algorithm for different problems.

Here we introduce a commonly used technique that allows us to find the
velocity as a function of position without finding the position as a
function of time—an alternate form of Newton’s second law. The method
is based on a simple principle: Instead of solving the equations of
motion directly, we integrate the equations of motion. Such a method
is called an integration method.

This allows us also to introduce the **work-energy** theorem. This
theorem allows us to find the velocity as a function of position for
an object even in cases when we cannot solve the equations of
motion. This introduces us to the concept of work and kinetic energy,
an energy related to the motion of an object.

And finally, we will link the work-energy theorem with the principle of conservation of energy.

## The Work-Energy Theorem

Let us define the kinetic energy $K$ with a given velocity $\boldsymbol{v}$

$$
K=\frac{1}{2}mv^2,
$$

where $m$ is the mass of the object we are considering.
We assume also that there is a force $\boldsymbol{F}$ acting on the given object

$$
\boldsymbol{F}=\boldsymbol{F}(\boldsymbol{r},\boldsymbol{v},t),
$$

with $\boldsymbol{r}$ the position and $t$ the time.
In general we assume the force is a function of all these variables.
Many of the more central forces in Nature however, depende only on the
position. Examples are the gravitational force and the force derived
from the Coulomb potential in electromagnetism.

## Rewriting the Kinetic Energy

Let us study the derivative of the kinetic energy with respect to time $t$. Its continuous form is

$$
\frac{dK}{dt}=\frac{1}{2}m\frac{d\boldsymbol{v}\cdot\boldsymbol{v}}{dt}.
$$

Using our results from exercise 3 of homework 1, we can write the derivative of a vector dot product as

$$
\frac{dK}{dt}=\frac{1}{2}m\frac{d\boldsymbol{v}\cdot\boldsymbol{v}}{dt}= \frac{1}{2}m\left(\frac{d\boldsymbol{v}}{dt}\cdot\boldsymbol{v}+\boldsymbol{v}\cdot\frac{d\boldsymbol{v}}{dt}\right)=m\frac{d\boldsymbol{v}}{dt}\cdot\boldsymbol{v}.
$$

We know also that the acceleration is defined as

$$
\boldsymbol{a}=\frac{\boldsymbol{F}}{m}=\frac{d\boldsymbol{v}}{dt}.
$$

We can then rewrite the equation for the derivative of the kinetic energy as

$$
\frac{dK}{dt}=m\frac{d\boldsymbol{v}}{dt}\boldsymbol{v}=\boldsymbol{F}\frac{d\boldsymbol{r}}{dt},
$$

where we defined the velocity as the derivative of the position with respect to time.

## Discretizing

Let us now discretize the above equation by letting the instantenous terms be replaced by a discrete quantity, that is
we let $dK\rightarrow \Delta K$, $dt\rightarrow \Delta t$, $d\boldsymbol{r}\rightarrow \Delta \boldsymbol{r}$ and $d\boldsymbol{v}\rightarrow \Delta \boldsymbol{v}$.

We have then

$$
\frac{\Delta K}{\Delta t}=m\frac{\Delta \boldsymbol{v}}{\Delta t}\boldsymbol{v}=\boldsymbol{F}\frac{\Delta \boldsymbol{r}}{\Delta t},
$$

or by multiplying out $\Delta t$ we have

$$
\Delta K=\boldsymbol{F}\Delta \boldsymbol{r}.
$$

We define this quantity as the **work** done by the force $\boldsymbol{F}$ during the displacement $\Delta \boldsymbol{r}$. If study the dimensionality of this problem we have mass times length squared divided by time squared, or just dimension energy.

## Difference in kinetic energy

If we now a series of such displacements $\Delta \boldsymbol{r}$ $i=0,1,\dots,n$, we have a difference in kinetic energy at a final position $\boldsymbol{r}_n$ and an
initial position $\boldsymbol{r}_0$ given by

$$
\Delta K=\frac{1}{2}mv_n^2-\frac{1}{2}mv_0^2=\sum_{i=0}^n\boldsymbol{F}_i\Delta \boldsymbol{r},
$$

where $\boldsymbol{F}_i$ are the forces acting at every position $\boldsymbol{r}_i$.

The work done by acting with a force on a set of displacements can
then be as expressed as the difference between the initial and final
kinetic energies.

This defines the **work-energy** theorem.

## From the discrete version to the continuous version

If we take the limit $\Delta \boldsymbol{r}\rightarrow 0$, we can rewrite the sum over the various displacements in terms of an integral, that is

$$
\Delta K=\frac{1}{2}mv_n^2-\frac{1}{2}mv_0^2=\sum_{i=0}^n\boldsymbol{F}_i\Delta \boldsymbol{r}\rightarrow \int_{\boldsymbol{r}_0}^{\boldsymbol{r}_n}\boldsymbol{F}(\boldsymbol{r},\boldsymbol{v},t)d\boldsymbol{r}.
$$

This integral defines a path integral since it will depend on the given path we take between the two end points. We will replace the limits with the symbol $c$ in order to indicate that we take a specific countour in space when the force acts on the system. That is the work $W_{n0}$ between two points $\boldsymbol{r}_n$ and $\boldsymbol{r}_0$ is labeled as

$$
W_{n0}=\frac{1}{2}mv_n^2-\frac{1}{2}mv_0^2=\int_{c}\boldsymbol{F}(\boldsymbol{r},\boldsymbol{v},t)d\boldsymbol{r}.
$$

Note that if the force is perpendicular to the displacement, then the force does not affect the kinetic energy.

Let us now study some examples of forces and how to find the velocity from the integration over a given path.


# Plotting with Matplotlib

## Prepare for action

```
import numpy as np
import scipy as sp
import sympy

# Pylab combines the pyplot functionality (for plotting) with the numpy
# functionality (for mathematics and for working with arrays) in a single namespace
# aims to provide a closer MATLAB feel (the easy way). Note that his approach
# should only be used when doing some interactive quick and dirty data inspection.
# DO NOT USE THIS FOR SCRIPTS
#from pylab import *

# the convienient Matplotib plotting interface pyplot (the tidy/right way)
# use this for building scripts. The examples here will all use pyplot.
import matplotlib.pyplot as plt

# for using the matplotlib API directly (the hard and verbose way)
# use this when building applications, and/or backends
import matplotlib as mpl
```

How would you like the IPython notebook show your plots? In order to use the
matplotlib IPython magic youre IPython notebook should be launched as

    ipython notebook --matplotlib=inline

Make plots appear as a pop up window, chose the backend: 'gtk', 'inline', 'osx', 'qt', 'qt4', 'tk', 'wx'

    %matplotlib qt

or inline the notebook (no panning, zooming through the plot). Not working in IPython 0.x

    %matplotib inline

```
# activate pop up plots
#%matplotlib qt
# or change to inline plots
#%matplotlib inline
%matplotlib
```

    Using matplotlib backend: module://IPython.kernel.zmq.pylab.backend_inline

### Matplotlib documentation

Finding your own way (aka RTFM). Hint: there is search box available!

* http://matplotlib.org/contents.html

The Matplotlib API docs:

* http://matplotlib.org/api/index.html

Pyplot, object oriented plotting:

* http://matplotlib.org/api/pyplot_api.html
* http://matplotlib.org/api/pyplot_summary.html

Extensive gallery with examples:

* http://matplotlib.org/gallery.html

### Tutorials for those who want to start playing

If reading manuals is too much for you, there is a very good tutorial available here:

* http://nbviewer.ipython.org/github/jrjohansson/scientific-python-lectures/blob/master/Lecture-4-Matplotlib.ipynb

Note that this tutorial uses

    from pylab import *

which is usually not adviced in more advanced script environments. When using

    import matplotlib.pyplot as plt

you need to preceed all plotting commands as used in the above tutorial with

    plt.

Give me more!

[EuroScipy 2012 Matlotlib tutorial](http://www.loria.fr/~rougier/teaching/matplotlib/). Note that here the author uses ```from pylab import * ```. When using ```import matplotliblib.pyplot as plt``` the plotting commands need to be proceeded with ```plt.```

## Plotting template starting point

```
# some sample data
x = np.arange(-10,10,0.1)
```

To change the default plot configuration values.

```
# create a figure instance, note that figure size is given in inches!
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8,6))
# set the big title (note aligment relative to figure)
fig.suptitle("suptitle 16, figure alignment", fontsize=16)

# actual plotting
ax.plot(x, x**2, label="label 12")

# set axes title (note aligment relative to axes)
ax.set_title("title 14, axes alignment", fontsize=14)

# axes labels
ax.set_xlabel('xlabel 12')
ax.set_ylabel(r'$y_{\alpha}$ 12')

# legend
ax.legend(fontsize=12, loc="best")

page_width_cm = 13
dpi = 200
inch = 2.54 # inch in cm
# setting global plot configuration using the RC configuration style
plt.rc('font', family='serif')
plt.rc('xtick', labelsize=20) # tick labels
plt.rc('ytick', labelsize=20) # tick labels
plt.rc('axes', labelsize=20)  # axes labels
# If you don’t need LaTeX, don’t use it. It is slower to plot, and text

# saving the figure in different formats
fig.savefig('figure-%03i.png' % dpi, dpi=dpi)
fig.savefig('figure.svg')
fig.savefig('figure.eps')
```

```

```

```
ax.grid(True)
fig.canvas.draw()
```

```
# following steps are only relevant when using figures as pop up windows (with %matplotlib qt)
# to update a figure with has been modified
fig.canvas.draw()
# show a figure
fig.show()
```

## Exercise

The current section is about you trying to figure out how to do several plotting features. You should use the previously mentioned resources to find how to do that. In many cases, google is your friend!

* add a grid to the plot

```
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8,6))
ax.plot(x,x**2)
#Write code to show grid in plot here
ax.set_xlim(0,10)
ax.set_ylim(0,100)
ax.xaxis.set_major_locator(MultipleLocator(1.0))
ax.xaxis.set_minor_locator(MultipleLocator(0.2))
ax.yaxis.set_major_locator(MultipleLocator(10.0))
ax.yaxis.set_minor_locator(MultipleLocator(2))
ax.grid(which='major', axis='x', linewidth=0.75, linestyle='-', color='0.75')
ax.grid(which='minor', axis='x', linewidth=0.25, linestyle='-', color='0.75')
ax.grid(which='major', axis='y', linewidth=0.75, linestyle='-', color='0.75')
ax.grid(which='minor', axis='y', linewidth=0.25, linestyle='-', color='0.75')

# setting global plot configuration using the RC configuration style
plt.rc('font', family='serif')
plt.rc('xtick', labelsize=20) # tick labels
plt.rc('ytick', labelsize=20) # tick labels
plt.rc('axes', labelsize=20)  # axes labels

fig.show()
```

* change the location of the legend to different places

```
plt.plot(x,x**2, label="label 12")
plt.legend(fontsize=12, loc="best")
plt.legend(fontsize=12, loc="lower left")
plt.legend(fontsize=12, loc="upper center")
plt.legend(fontsize=12, loc="right")
plt.legend(fontsize=12, loc="center")
plt.legend(fontsize=12, loc="center right")
plt.legend(fontsize=12, loc="best")

# setting global plot configuration using the RC configuration style
plt.rc('font', family='serif')
plt.rc('xtick', labelsize=20) # tick labels
plt.rc('ytick', labelsize=20) # tick labels
plt.rc('axes', labelsize=20)  # axes labels
```

* find a way to control the line type and color, marker type and color, control the frequency of the marks (`markevery`). See plot options at: http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.plot

```
plt.plot(x,x**2,lw=0.5,ls="-",c="red",marker="*",mec="black",mew=0.2,mfc="white",markevery=10)

# setting global plot configuration using the RC configuration style
plt.rc('font', family='serif')
plt.rc('xtick', labelsize=20) # tick labels
plt.rc('ytick', labelsize=20) # tick labels
plt.rc('axes', labelsize=20)  # axes labels
```

* add different sub-plots

```
fig = plt.figure(figsize=(14,4))
n_row = 4
for i_row in range(n_row):
    if  i_row != 0:
        ax = fig.add_subplot(1, n_row-1, i_row)
        ax.plot(x,x**2)
        ax.set_xlabel('x_'+str(i_row))
        ax.set_ylabel('y_'+str(i_row))

# setting global plot configuration using the RC configuration style
plt.rc('font', family='serif')
plt.rc('xtick', labelsize=20) # tick labels
plt.rc('ytick', labelsize=20) # tick labels
plt.rc('axes', labelsize=20)  # axes labels

plt.tight_layout()

```

* size the figure such that when included on an A4 page the fonts are given in their true size

```
#Direct input
plt.rcParams['text.latex.preamble']=[r"\usepackage{lmodern}"]
#Options
params = {'text.usetex' : True,
          'font.size' : 11,
          'font.family' : 'lmodern',
          'text.latex.unicode': True,
          }
plt.rcParams.update(params)

# setting global plot configuration using the RC configuration style
plt.rc('font', family='serif')
plt.rc('xtick', labelsize=20) # tick labels
plt.rc('ytick', labelsize=20) # tick labels
plt.rc('axes', labelsize=20)  # axes labels

fig = plt.figure(figsize=(14,4))
fig.subplots_adjust(bottom=0.025, left=0.025, top = 0.975, right=0.975)
n_row = 4
for i_row in range(n_row):
    if  i_row != 0:
        ax = fig.add_subplot(1, n_row-1, i_row)
        ax.plot(x,x**2)
        ax.set_xlabel('x_'+str(i_row))
        ax.set_ylabel('y_'+str(i_row))

plt.tight_layout()

```

* make a contour plot

```
X, Y = np.meshgrid(x,x)

contourf(X, Y, X*(Y**2), 10, alpha=.75, cmap=cm.hot)
C = contour(X, Y, X*(Y**2), 10, colors='black', linewidth=.5)
clabel(C, inline=1, fontsize=10)

xticks([]), yticks([])
show()
```

* use twinx() to create a second axis on the right for the second plot

```
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8,6))
ax.plot(x,x**2)
ax2=ax.twinx()
ax2.plot(x,x**4, 'r')
```

* add horizontal and vertical lines using axvline(), axhline()

```
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8,6))
ax.plot(x,x**2)
ax=gca()
ax.axvline(x=0, ymin=0, ymax=100, c='r')
ax.axhline(y=50, xmin=-10, xmax=10, c='r')

```

* autoformat dates for nice printing on the x-axis using fig.autofmt_xdate()

```
import datetime
dates = np.array([datetime.datetime.now() + datetime.timedelta(days=i) for i in xrange(24)])
x=np.array([(i)**2 for i in xrange(24)])
fig, ax = plt.subplots(nrows=1, ncols=1)
ax.plot(dates,x)
fig.autofmt_xdate(bottom=0.2, rotation=90)
```

## Advanced exercises

We are going to play a bit with regression

* Create a vector x of equally spaced number between $x \in [0, 5\pi]$ of 1000 points (keyword: linspace)

```
x = np.linspace(0,5*pi,1000)
```

* create a vector y, so that y=sin(x) with some random noise

```
y=np.sin(x)+np.random.rand(size(x))
```

* plot it like this:

```
plt.plot(x,y,lw=0.5,ls="",c="black",marker="o",mec="blue",mew=1,mfc="white")

plt.legend(fontsize=12, loc="best")

# setting global plot configuration using the RC configuration style
plt.rc('font', family='serif')
plt.rc('xtick', labelsize=20) # tick labels
plt.rc('ytick', labelsize=20) # tick labels
plt.rc('axes', labelsize=20)  # axes labels
```

Try to do a polynomial fit on y(x) with different polynomial degree (Use numpy.polyfit to obtain coefficients)

Plot it like this (use np.poly1d(coef)(x) to plot polynomials)

```
z = {}
y_fit = {}
for i in range(10):
    z[i] = np.polyfit(x, y, i)
    p = np.poly1d(z[i])
    y_fit[i] = p(x)

```

```
fig = plt.figure(figsize=(10,4))
ax = plt.subplot(111)
ax.plot(x,y,lw=0.5,ls="",c="black",marker="o",mec="blue",mew=0.5,mfc="white",label="noisy")
p = {}
for i in range(10):
    ax.plot(x,y_fit[i],lw=1, label="line "+str(i))

# setting global plot configuration using the RC configuration style
plt.rc('font', family='serif')
plt.rc('xtick', labelsize=20) # tick labels
plt.rc('ytick', labelsize=20) # tick labels
plt.rc('axes', labelsize=20)  # axes labels

# Shink current axis by 20%
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])

# Put a legend to the right of the current axis
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
```

```

```

lesson 3/results/plotting-introduction-exercise - jumu.ipynb


# Computational Physics Lectures:  Introduction to programming (C++ and Fortran)

 **Morten Hjorth-Jensen**, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

Date: **2016**

## Extremely useful tools, strongly recommended

**and discussed at the lab sessions the first two weeks.**

  * GIT for version control, discussed at the lab this week (and next week as well)

  * ipython notebook, mentioned this week

  * QTcreator for editing and mastering computational projects

  * Armadillo as a useful numerical library for C++, highly recommended

  * Unit tests

## A structured programming approach

  * Before writing a single line, have the algorithm clarified and understood. It is crucial to have a logical structure of e.g., the flow and organization of data before one starts writing.

  * Always try to choose the simplest algorithm. Computational speed can be improved upon later.

  * Try to write a as clear program as possible. Such programs are easier to debug, and although it may take more time, in the long run it may save you time. If you collaborate with other people, it reduces spending time on debuging and trying to understand what the codes do. A clear program will also allow you to remember better what the program really does!

## A structured programming approach

  * The planning of the program should be from top down to bottom, trying to keep the flow as linear as possible. Avoid jumping back and forth in the program. First you need to arrange the major tasks to be achieved. Then try to break the major tasks into subtasks. These can be represented by functions or subprograms. They should accomplish limited tasks and as far as possible be independent of each other. That will allow you to use them in other programs as well.

  * Try always to find some cases where an analytical solution exists or where simple test cases can be applied. If possible, devise different algorithms for solving the same problem. If you get the same answers, you may have coded things correctly or made the same error twice or more.

## Getting Started

**Compiling and linking, without QTcreator.**

In order to obtain an executable file for a C++ program, the following
instructions under Linux/Unix can be used

        c++ -c -Wall myprogram.cpp
        c++ -o myprogram myprogram.o

where the compiler is called through the command c++/g++. The compiler
option -Wall means that a warning is issued in case of non-standard
language. The executable file is in this case `myprogram`. The option
`-c` is for compilation only, where the program is translated into machine code,
while the `-o` option links the produced object file `myprogram.o`
and produces the executable `myprogram` .

For Fortran2008 we use the Intel compiler, replace `c++` with `ifort`.
Also, to speed up the code use compile options like

        c++ -O3 -c -Wall myprogram.cpp

## Makefiles and simple scripts

Under Linux/Unix it is often convenient to create a
so-called makefile, which is a script which includes possible
compiling commands.

        # Comment lines
        # General makefile for c - choose PROG =   name of given program
        # Here we define compiler option, libraries and the  target
        CC= g++ -Wall
        PROG= myprogram
        # this is the math library in C, not necessary for C++
        LIB = -lm
        # Here we make the executable file
        ${PROG} :          ${PROG}.o
                           ${CC} ${PROG}.o ${LIB} -o ${PROG}
        # whereas here we create the object file
        ${PROG}.o :       ${PROG}.c
                          ${CC} -c ${PROG}.c

If you name your file for `makefile`, simply type the command
`make` and Linux/Unix executes all of the statements in the above
makefile. Note that C++ files have the extension `.cpp`.

## [Hello world](https://github.com/CompPhysics/ComputationalPhysicsMSU/blob/master/doc/Programs/LecturePrograms/programs/IntroProgramming/cpp/program1.cpp)

**The C encounter.**

Here we present first the C version.

        /* comments in C begin like this and end with */
        #include <stdlib.h> /* atof function */
        #include <math.h>   /* sine function */
        #include <stdio.h>  /* printf function */
        int main (int argc, char* argv[])
        {
          double r, s;        /* declare variables */
          r = atof(argv[1]);  /* convert the text argv[1] to double */
          s = sin(r);
          printf("Hello, World! sin(%g)=%g\n", r, s);
          return 0;           /* success execution of the program */

## Hello World, dissecting the code

**Dissection I.**

The compiler must see a declaration of a function before you can
call it (the compiler checks the argument and return types).
The declaration of library functions appears
in so-called "header files" that must be included in the program, e.g.,

           #include <stdlib.h> /* atof function */

We call three functions (atof, sin, printf)
and these are declared in three different header files.
The main program is a function called main
with a return value set to an integer, int (0 if success).
The operating system stores the return value,
and other programs/utilities can check whether
the execution was successful or not.
The command-line arguments are transferred to the main function through

           int main (int argc, char* argv[])

## Hello World, more dissection

**Dissection II.**

The command-line arguments are transferred to the main function through

           int main (int argc, char* argv[])

The integer `argc` is the no of command-line arguments, set to
one in our case, while
`argv` is a vector of strings containing the command-line arguments
with `argv[0]` containing  the name of the program
and `argv[1]`, `argv[2]`, ... are the command-line args, i.e., the number of
lines of input to the program.
Here we define floating points, see also below,
through the keywords `float` for single precision real numbers and
`double` for double precision. The function
`atof` transforms a text (`argv[1]`) to a float.
The sine function is declared in math.h, a library which
is not automatically included and needs to be linked when computing
an executable file.

With the command `printf` we obtain a formatted printout.
The `printf` syntax is used for formatting output
in many C-inspired languages (Perl, Python, Awk, partly C++).

## Hello World

**Now in C++.**

Here we present first the C++ version.

        // A comment line begins like this in C++ programs
        // Standard ANSI-C++ include files
        #include <iostream>   // input and output
        #include <cmath>      // math functions
        using namespace std;
        int main (int argc, char* argv[])
        {
          // convert the text argv[1] to double using atof:
          double r = atof(argv[1]);  /* convert the text argv[1] to double */
          double s = sin(r);
          cout << "Hello, World! sin(" << r << ") =" << s << endl;
          return 0;           /* success execution of the program */
        }

## C++ Hello World

**Dissection I.**

We have replaced the call to `printf` with the standard C++ function
`cout`. The header file `<iostream.h>` is then needed.
In addition, we don't need to
declare variables like `r` and `s`  at the beginning of the program.
I personally prefer
however to declare all variables at the beginning of a function, as this
gives *me* a feeling of greater readability.

## Brief summary

**C/C++ program.**

  * A C/C++ program begins with include statements of header files (libraries,intrinsic functions etc)

  * Functions which are used are normally defined at top (details next week)

  * The main program is set up as an integer, it returns 0 (everything correct) or 1 (something went wrong)

  * Standard `if`, `while` and `for` statements as in Java, Fortran, Python...

  * Integers have a very limited range.

## Brief summary

**Arrays.**

  * A C/C++ array begins by indexing at 0!

  * Array allocations are done by size, not by the final index value.If you allocate an array with 10 elements, you should index them from $0,1,\dots, 9$.

  * Initialize always an array before a computation.

## Serious problems and representation of numbers

**Integer and Real Numbers.**

  * Overflow

  * Underflow

  * Roundoff errors

  * Loss of precision

## Limits, you must declare variables

**C++ and Fortran declarations.**

type in C/C++ and Fortran2008	 bits	                     range

   int/INTEGER (2)                	    16      	    -32768 to 32767

   unsigned int                   	    16      	    0 to 65535

   signed int                     	    16      	    -32768 to 32767

   short int                      	    16      	    -32768 to 32767

   unsigned short int             	    16      	    0 to 65535

   signed short int               	    16      	    $-32768$ to 32767

   int/long int/INTEGER (4)       	    32      	    -2147483648 to 2147483647

   signed long int                	    32      	    -2147483648 to 2147483647

   float/REAL(4)                  	    32      	    $3.4\times 10^{-44}$ to $3.4\times 10^{+38}$

   double/REAL(8)                 	    64      	    $1.7\times 10^{-322}$ to $1.7\times 10^{+308}$

   long double                    	    64      	    $1.7\times 10^{-322}$ to $1.7\times 10^{+308}$

## From decimal to binary representation
**How to do it.**

$$
a_n2^n+a_{n-1}2^{n-1}  +a_{n-2}2^{n-2}  +\dots +a_{0}2^{0}.
$$

In binary notation we have thus $(417)_{10} =(110110001)_2$
since we have

$$
\begin{align*}
(110100001)_2
&=1\times2^8+1\times 2^{7}+0\times 2^{6}+1\times 2^{5}+0\times 2^{4}+0\times 2^{3}\\
&+0\times 2^{2}+0\times 2^{2}+0\times 2^{1}+1\times 2^{0}.
\end{align*}
$$

## From decimal to binary representation, the actual operation
To see this, we have performed the following divisions by 2

   417/2=208    	    remainder 1    	    coefficient of $2^{0}$ is 1

   208/2=104    	    remainder 0    	    coefficient of $2^{1}$ is 0

   104/2=52     	    remainder 0    	    coefficient of $2^{2}$ is 0

   52/2=26      	    remainder 0    	    coefficient of $2^{3}$ is 0

   26/2=13      	    remainder 1    	    coefficient of $2^{4}$ is 0

   13/2= 6      	    remainder 1    	    coefficient of $2^{5}$ is 1

   6/2= 3       	    remainder 0    	    coefficient of $2^{6}$ is 0

   3/2= 1       	    remainder 1    	    coefficient of $2^{7}$ is 1

   1/2= 0       	    remainder 1    	    coefficient of $2^{8}$ is 1

## [From decimal to binary representation](https://github.com/CompPhysics/ComputationalPhysicsMSU/blob/master/doc/Programs/LecturePrograms/programs/IntroProgramming/cpp/program2.cpp)

**Integer numbers.**

        #include <iostream>
        #include <cmath>
        #include <cstdio>
        #include <cstdlib>
        using namespace std;
        int main (int argc, char* argv[])
        {
          int i;
          int terms[32]; // storage of a0, a1, etc, up to 32 bits
          int save;
          int number = atoi(argv[1]);
          save = number;
          // initialise the term a0, a1 etc
          for (i=0; i < 32 ; i++){ terms[i] = 0;}
          for (i=0; i < 32 ; i++){
            terms[i] = number%2;
            number /= 2;
          }
          // write out results
          cout << "Number of bytes used= " << sizeof(number) << endl;
          for (i=0; i < 32 ; i++){
            cout << " Term nr: " << i << "Value= " << terms[i];
            cout << endl;
          }
          return 0;
        }

## [From decimal to binary representation](https://github.com/CompPhysics/ComputationalPhysicsMSU/blob/master/doc/Programs/LecturePrograms/programs/IntroProgramming/Fortran/program2.f90)

**Integer numbers, Fortran.**

        PROGRAM binary_integer
        IMPLICIT NONE
          INTEGER  i, number, terms(0:31) ! storage of a0, a1, etc, up to 32 bits

          WRITE(*,*) 'Give a number to transform to binary notation'
          READ(*,*) number
        ! Initialise the terms a0, a1 etc
          terms = 0
        ! Fortran takes only integer loop variables
          DO i=0, 31
             terms(i) = MOD(number,2)
             number = number/2
          ENDDO
        ! write out results
          WRITE(*,*) 'Binary representation '
          DO i=0, 31
            WRITE(*,*)' Term nr and value', i, terms(i)
          ENDDO

        END PROGRAM binary_integer

## [Representing Integer Numbers](https://github.com/CompPhysics/ComputationalPhysicsMSU/blob/master/doc/Programs/LecturePrograms/programs/IntroProgramming/cpp/program3.cpp)

**Possible Overflow for Integers.**

        // A comment line begins like this in C++ programs
        // Program to calculate 2**n
        // Standard ANSI-C++ include files */
        #include <iostream>
        #include <cmath>
        using namespace std
        int main()
        {
           int  int1, int2, int3;
        // print to screen
           cout << "Read in the exponential N for 2^N =" << endl;
        // read from screen
           cin >> int2;
           int1 = (int) pow(2., (double) int2);
           cout << " 2^N * 2^N = " << int1*int1 << endl;
           int3 = int1 - 1;
           cout << " 2^N*(2^N - 1) = " << int1 * int3  << endl;
           cout << " 2^N- 1 = " << int3  << endl;
           return 0;

        // End: program main()

## Loss of Precision

**Machine Numbers.**

In the decimal system we would write a number like $9.90625$
in what is called the normalized scientific notation.

$$
9.90625=0.990625\times 10^{1},
$$

and a real non-zero number could be generalized as

$$
\begin{equation}
    x=\pm r\times 10^{{\mbox{n}}},
\label{_auto1} \tag{1}
\end{equation}
$$

with $r$ a number in the range $1/10 \le r < 1$.
In a similar way we can use represent a binary number in
scientific notation as

$$
\begin{equation}
    x=\pm q\times 2^{{\mbox{m}}},
\label{_auto2} \tag{2}
\end{equation}
$$

with $q$ a number in the range $1/2 \le q < 1$.
This means that the mantissa of a binary number would be represented by
the general formula

$$
\begin{equation}
(0.a_{-1}a_{-2}\dots a_{-n})_2=a_{-1}\times 2^{-1}
+a_{-2}\times 2^{-2}+\dots+a_{-n}\times 2^{-n}.
\label{_auto3} \tag{3}
\end{equation}
$$

## Loss of Precision

**Machine Numbers.**

In a typical computer, floating-point numbers are represented
in the way described above, but with certain restrictions
on $q$ and $m$ imposed by the available word length.
In the machine, our
number $x$ is represented as

$$
\begin{equation}
    x=(-1)^s\times {\mbox{mantissa}}\times 2^{{\mbox{exponent}}},
\label{_auto4} \tag{4}
\end{equation}
$$

where $s$ is the sign bit, and the exponent gives the available range.
With a single-precision word, 32 bits, 8 bits would typically be reserved
for the exponent,  1 bit for the sign and 23 for the mantissa.

## Loss of Precision

**Machine Numbers.**

A modification of the scientific notation for binary numbers is to
require that the leading binary digit 1 appears to the left of the binary point.
In this case the representation of the mantissa $q$ would be
$(1.f)_2$ and $ 1 \le q < 2$. This form is rather useful when storing
binary numbers in a computer word, since we can always assume that the leading
bit 1 is there. One bit of space can then be saved meaning that a 23 bits
mantissa has actually 24 bits. This means explicitely that a binary number with 23 bits
for the mantissa reads

$$
\begin{equation}
(1.a_{-1}a_{-2}\dots a_{-23})_2=1\times 2^0+a_{-1}\times 2^{-1}+
+a_{-2}\times 2^{-2}+\dots+a_{-23}\times 2^{-23}.
\label{_auto5} \tag{5}
\end{equation}
$$

## Loss of Precision, example
As an example, consider the 32 bits binary number

$$
(10111110111101000000000000000000)_2,
$$

where the first bit is reserved for the sign, 1 in this case yielding a
negative sign. The exponent $m$ is given by the next 8 binary numbers
$01111101$ resulting in 125 in the decimal system.

## Loss of Precision

**Machine Numbers.**

However, since the
exponent has eight bits, this means it has  $2^8-1=255$ possible numbers in the interval
$-128 \le m \le 127$, our final
exponent is $125-127=-2$ resulting in $2^{-2}$.
Inserting the sign and the mantissa yields the final number in the decimal representation as

$$
-2^{-2}\left(1\times 2^0+1\times 2^{-1}+
1\times 2^{-2}+1\times 2^{-3}+0\times 2^{-4}+1\times 2^{-5}\right)=
$$

$$
(-0.4765625)_{10}.
$$

In this case we have an exact machine representation with 32 bits (actually, we need less than
23 bits for the mantissa).

## Loss of Precision, consequences
If our number $x$ can be exactly represented in the machine, we call
$x$ a machine number. Unfortunately, most numbers cannot  and are thereby
only approximated in the machine. When such a number occurs as the result
of reading some input data or of a computation, an inevitable error
will arise in representing it as accurately as possible by
a machine number.

## Loss of Precision

**Machine Numbers.**

A floating number x, labelled $fl(x)$ will therefore always be represented as

$$
\begin{equation}
  fl(x) = x(1\pm \epsilon_x),
\label{_auto6} \tag{6}
\end{equation}
$$

with $x$ the exact number and the error $|\epsilon_x| \le |\epsilon_M|$, where
$\epsilon_M$ is the precision assigned. A number like $1/10$ has no exact binary representation
with single or double precision. Since the mantissa

$$
\left(1.a_{-1}a_{-2}\dots a_{-n}\right)_2
$$

is always truncated at some stage $n$ due to its limited number of bits, there is only a
limited number of real binary numbers. The spacing between every real binary number is given by the
chosen machine precision.
For a 32 bit words this number is approximately
$ \epsilon_M \sim 10^{-7}$ and for double precision (64 bits) we have
$ \epsilon_M \sim 10^{-16}$, or in terms of a binary base
as $2^{-23}$ and $2^{-52}$ for single and double precision, respectively.

## Loss of Precision

**Machine Numbers.**

In the machine a number is represented as

$$
\begin{equation}
  fl(x)= x(1+\epsilon)
\label{_auto7} \tag{7}
\end{equation}
$$

where $|\epsilon| \leq \epsilon_M$ and $\epsilon$ is given by the
specified precision, $10^{-7}$ for single and $10^{-16}$ for double
precision, respectively.
$\epsilon_M$ is the given precision.
In case of a subtraction $a=b-c$, we have

$$
\begin{equation}
   fl(a)=fl(b)-fl(c) = a(1+\epsilon_a),
\label{_auto8} \tag{8}
\end{equation}
$$

or

$$
\begin{equation}
   fl(a)=b(1+\epsilon_b)-c(1+\epsilon_c),
\label{_auto9} \tag{9}
\end{equation}
$$

## Loss of Precision
The above means that

$$
\begin{equation}
   fl(a)/a=1+\epsilon_b\frac{b}{a}- \epsilon_c\frac{c}{a},
\label{_auto10} \tag{10}
\end{equation}
$$

and if $b\approx c$ we see that there is a potential for an increased
error in $fl(a)$.

## Loss of Precision

**Machine Numbers.**

Define
the absolute error as

$$
\begin{equation}
   |fl(a)-a|,
\label{_auto11} \tag{11}
\end{equation}
$$

whereas the relative error is

$$
\begin{equation}
   \frac{ |fl(a)-a|}{a} \le \epsilon_a.
\label{_auto12} \tag{12}
\end{equation}
$$

## Loss of Precision
The above subraction is thus

$$
\begin{equation}
   \frac{ |fl(a)-a|}{a}=\frac{ |fl(b)-fl(c)-(b-c)|}{a},
\label{_auto13} \tag{13}
\end{equation}
$$

yielding

$$
\begin{equation}
   \frac{ |fl(a)-a|}{a}=\frac{ |b\epsilon_b- c\epsilon_c|}{a}.
\label{_auto14} \tag{14}
\end{equation}
$$

The relative error
is the quantity of interest in scientific work. Information about the
absolute error is normally of little use in the absence of the magnitude
of the quantity being measured.

## Loss of numerical precision

Suppose we wish to evaluate the function

$$
f(x)=\frac{1-\cos(x)}{\sin(x)},
$$

for small values of $x$. Five leading digits. If we multiply the denominator and numerator
with $1+\cos(x)$ we obtain the equivalent expression

$$
f(x)=\frac{\sin(x)}{1+\cos(x)}.
$$

If we now choose $x=0.007$ (in radians) our choice of precision results in

$$
\sin(0.007)\approx 0.69999\times 10^{-2},
$$

and

$$
\cos(0.007)\approx 0.99998.
$$

## Loss of numerical precision

The first expression for $f(x)$ results in

$$
f(x)=\frac{1-0.99998}{0.69999\times 10^{-2}}=\frac{0.2\times 10^{-4}}{0.69999\times 10^{-2}}=0.28572\times 10^{-2},
$$

while the second expression results in

$$
f(x)=\frac{0.69999\times 10^{-2}}{1+0.99998}=
\frac{0.69999\times 10^{-2}}{1.99998}=0.35000\times 10^{-2},
$$

which is also the exact result. In the first expression, due to our
choice of precision, we have
only one relevant digit in the numerator, after the
subtraction. This leads to a loss of precision and a wrong result due to
a cancellation of two nearly equal numbers.
If we had chosen a precision of six leading digits, both expressions
yield the same answer.

## Loss of numerical precision

If we were to evaluate $x\sim \pi$, then the second expression for $f(x)$
can lead to potential losses of precision due to cancellations of nearly
equal numbers.

This simple example demonstrates  the loss of numerical precision due
to roundoff errors, where the number of leading digits is lost
in a subtraction of two near equal numbers.
The lesson to be drawn is that we cannot blindly compute a function.
We will always need to carefully analyze our algorithm in the search for
potential pitfalls. There is no magic recipe however, the only guideline
is an understanding of the fact that a machine cannot represent
correctly *all* numbers.

## Loss of precision can cause serious problems

**Real Numbers.**

  * **Overflow**: When the positive exponent exceeds the max value, e.g., 308 for `DOUBLE PRECISION` (64 bits). Under such circumstances the program will terminate and some compilers may give you the warning `OVERFLOW`.

  * **Underflow**: When the negative exponent becomes smaller than the min value, e.g., -308 for `DOUBLE PRECISION`. Normally, the variable is then set to zero and the program continues. Other compilers (or compiler options) may warn you with the `UNDERFLOW` message and the program terminates.

## Loss of precision, real numbers

### Roundoff errors

A floating point number like

$$
\begin{equation}
   x= 1.234567891112131468 = 0.1234567891112131468\times 10^{1}
\label{_auto15} \tag{15}
\end{equation}
$$

may be stored in the following way. The exponent is  small
and is stored in full precision. However,
the mantissa is not stored fully. In double precision (64 bits), digits
beyond the
15th are lost since the mantissa is normally stored in two words,
one which is the most significant one representing
123456 and the least significant one containing 789111213. The digits
beyond 3 are lost. Clearly, if we are summing alternating series
with large numbers, subtractions between two large numbers may lead
to roundoff errors, since not all relevant digits are kept.
This leads eventually to the next problem, namely

## More on loss of precision

**Real Numbers.**

  * **Loss of precision**: When one has to e.g., multiply two large numbers where one suspects that the outcome may be beyond the bonds imposed by the variable declaration, one could represent the numbers by logarithms, or rewrite the equations to be solved in terms of dimensionless variables. When dealing with problems in e.g., particle physics or nuclear physics where distance is measured in fm ($10^{-15}$ m), it can be quite convenient to redefine the variables for distance in terms of a dimensionless variable of the order of unity. To give an example, suppose you work with single precision and wish to perform the addition $1+10^{-8}$. In this case, the information containing in $10^{-8}$ is simply lost in the addition. Typically, when performing the addition, the computer equates first the exponents of the two numbers to be added. For $10^{-8}$ this has however catastrophic consequences since in order to obtain an exponent equal to $10^0$, bits in the mantissa are shifted to the right. At the end, all bits in the mantissa are zeros.

## A problematic case

**Three ways of computing $e^{-x}$.**

Brute force:

$$
\exp{(-x)}=\sum_{n=0}^{\infty}(-1)^n\frac{x^n}{n!}
$$

Recursion relation for

$$
\exp{(-x)}=\sum_{n=0}^{\infty}s_n=\sum_{n=0}^{\infty}(-1)^n\frac{x^n}{n!}
$$

$$
s_n=-s_{n-1}\frac{x}{n},
$$

$$
\exp{(x)}=\sum_{n=0}^{\infty}s_n
$$

$$
\exp{(-x)}=\frac{1}{\exp{(x)}}
$$

## [Program](https://github.com/CompPhysics/ComputationalPhysicsMSU/blob/master/doc/Programs/LecturePrograms/programs/IntroProgramming/cpp/program4.cpp) to compute $\exp{(-x)}$

**Brute Force.**

        // Program to calculate function exp(-x)
        // using straightforward summation with differing  precision
        using namespace std
        #include <iostream>
        #include <cmath>
        // type float:  32 bits precision
        // type double: 64 bits precision
        #define   TYPE          double
        #define   PHASE(a)      (1 - 2 * (abs(a) % 2))
        #define   TRUNCATION    1.0E-10
        // function declaration
        TYPE factorial(int);

## Program to compute $\exp{(-x)}$

**Still Brute Force.**

        int main()
        {
           int   n;
           TYPE  x, term, sum;
           for(x = 0.0; x < 100.0; x += 10.0)  {
             sum  = 0.0;                //initialization
             n    = 0;
             term = 1;
             while(fabs(term) > TRUNCATION)  {
                 term =  PHASE(n) * (TYPE) pow((TYPE) x,(TYPE) n)
                        / factorial(n);
                 sum += term;
                 n++;
             }  // end of while() loop

## Program to compute $\exp{(-x)}$

**Oh, it never ends!**

              printf("\nx = %4.1f   exp = %12.5E  series = %12.5E
                      number of terms = %d",
                      x, exp(-x), sum, n);
           } // end of for() loop

           printf("\n");           // a final line shift on output
           return 0;
        } // End: function main()
        //     The function factorial()
        //     calculates and returns n!
        TYPE factorial(int n)
        {
           int  loop;
           TYPE fac;
           for(loop = 1, fac = 1.0; loop <= n; loop++)  {
              fac *= loop;

           return fac;
        } // End: function factorial()

## Results $\exp{(-x)}$

**What is going on?**

 $x$ 	 $\exp{(-x)}$	     Series   	 Number of terms in series

   0.0      	    0.100000E+01    	    0.100000E+01     	    1

   10.0     	    0.453999E-04    	    0.453999E-04     	    44

   20.0     	    0.206115E-08    	    0.487460E-08     	    72

   30.0     	    0.935762E-13    	    -0.342134E-04    	    100

   40.0     	    0.424835E-17    	    -0.221033E+01    	    127

   50.0     	    0.192875E-21    	    -0.833851E+05    	    155

   60.0     	    0.875651E-26    	    -0.850381E+09    	    171

   70.0     	    0.397545E-30    	    NaN              	    171

   80.0     	    0.180485E-34    	    NaN              	    171

   90.0     	    0.819401E-39    	    NaN              	    171

   100.0    	    0.372008E-43    	    NaN              	    171

## [Program](https://github.com/CompPhysics/ComputationalPhysicsMSU/blob/master/doc/Programs/LecturePrograms/programs/IntroProgramming/cpp/program5.cpp) to compute $\exp{(-x)}$

        // program to compute exp(-x) without exponentials
        using namespace std
        #include <iostream>
        #include <cmath>
        #define  TRUNCATION     1.0E-10

        int main()
        {
           int       loop, n;
           double    x, term, sum;
           for(loop = 0; loop <= 100; loop += 10)
           {
             x    = (double) loop;          // initialization
             sum  = 1.0;
             term = 1;
             n    = 1;

## Program to compute $\exp{(-x)}$

**Last statements.**

             while(fabs(term) > TRUNCATION)
               {
        	 term *= -x/((double) n);
        	 sum  += term;
        	 n++;
               } // end while loop
             cout << "x = " << x   << " exp = " << exp(-x) <<"series = "
                  << sum  << " number of terms =" << n << endl;
           } // end of for() loop

           cout << endl;           // a final line shift on output

        }  /*    End: function main() */

## Results $\exp{(-x)}$

**More Problems.**

   $x$    	  $\exp{(-x)}$ 	      Series    	 Number of terms in series

   0.000000      	    0.10000000E+01    	    0.10000000E+01     	    1

   10.000000     	    0.45399900E-04    	    0.45399900E-04     	    44

   20.000000     	    0.20611536E-08    	    0.56385075E-08     	    72

   30.000000     	    0.93576230E-13    	    -0.30668111E-04    	    100

   40.000000     	    0.42483543E-17    	    -0.31657319E+01    	    127

   50.000000     	    0.19287498E-21    	    0.11072933E+05     	    155

   60.000000     	    0.87565108E-26    	    -0.33516811E+09    	    182

   70.000000     	    0.39754497E-30    	    -0.32979605E+14    	    209

   80.000000     	    0.18048514E-34    	    0.91805682E+17     	    237

   90.000000     	    0.81940126E-39    	    -0.50516254E+22    	    264

   100.000000    	    0.37200760E-43    	    -0.29137556E+26    	    291

## Most used formula for derivatives

**3 point formulae.**

First derivative  ($f_0 = f(x_0)$, $f_{-h}=f(x_0-h)$ and $f_{+h}=f(x_0+h)$

$$
\frac{f_h-f_{-h}}{2h}=f'_0+\sum_{j=1}^{\infty}\frac{f_0^{(2j+1)}}{(2j+1)!}h^{2j}.
$$

Second derivative

$$
\frac{ f_h -2f_0 +f_{-h}}{h^2}=f_0''+2\sum_{j=1}^{\infty}\frac{f_0^{(2j+2)}}{(2j+2)!}h^{2j}.
$$

## Error Analysis

$$
\epsilon=log_{10}\left(\left|\frac{f''_{\mbox{computed}}-f''_{\mbox{exact}}}
                 {f''_{\mbox{exact}}}\right|\right),
$$

$$
\epsilon_{\mbox{tot}}=\epsilon_{\mbox{approx}}+\epsilon_{\mbox{ro}}.
$$

For the computed second derivative  we have

$$
f_0''=\frac{ f_h -2f_0 +f_{-h}}{h^2}-2\sum_{j=1}^{\infty}\frac{f_0^{(2j+2)}}{(2j+2)!}h^{2j},
$$

and the truncation or approximation error goes like

$$
\epsilon_{\mbox{approx}}\approx \frac{f_0^{(4)}}{12}h^{2}.
$$

## Error Analysis

If we were not to worry about loss of precision, we could in principle
make $h$ as small as possible.
However, due to the computed expression in the above program example

$$
f_0''=\frac{ f_h -2f_0 +f_{-h}}{h^2}=\frac{ (f_h -f_0) +(f_{-h}-f_0)}{h^2},
$$

we reach fairly quickly a limit for where loss of precision due to the subtraction
of two nearly equal numbers becomes crucial.

If $(f_{\pm h} -f_0)$ are very close, we have
$(f_{\pm h} -f_0)\approx \epsilon_M$, where $|\epsilon_M|\le 10^{-7}$ for single and
$|\epsilon_M|\le 10^{-15}$ for double precision, respectively.

We have then

$$
\left|f_0''\right|=
 \left|\frac{ (f_h -f_0) +(f_{-h}-f_0)}{h^2}\right|\le \frac{ 2 \epsilon_M}{h^2}.
$$

## Error Analysis

Our total error becomes

$$
\left|\epsilon_{\mbox{tot}}\right|\le  \frac{2 \epsilon_M}{h^2} +
                          \frac{f_0^{(4)}}{12}h^{2}.
\label{eq:experror} \tag{16}
$$

It is then natural to ask which value of $h$ yields the smallest
total error. Taking the derivative of $\left|\epsilon_{\mbox{tot}}\right|$
with respect to $h$ results in

$$
h= \left(\frac{ 24\epsilon_M}{f_0^{(4)}}\right)^{1/4}.
$$

With double precision and $x=10$ we obtain

$$
h\approx 10^{-4}.
$$

Beyond this value, it is essentially the loss of numerical precision
which takes over.

## Error Analysis

Due to the subtractive cancellation in the expression
for $f''$ there is a pronounced detoriation in accuracy as $h$ is made smaller
and smaller.

It is instructive in this analysis to rewrite the numerator of
the computed derivative as

$$
(f_h -f_0) +(f_{-h}-f_0)=(e^{x+h}-e^{x}) + (e^{x-h}-e^{x}),
$$

as

$$
(f_h -f_0) +(f_{-h}-f_0)=e^x(e^{h}+e^{-h}-2),
$$

since it is the difference $(e^{h}+e^{-h}-2)$ which causes
the loss of precision.

## Error Analysis

$x$	  $h=0.01$ 	 $h=0.001$ 	 $h=0.0001$	 $h=0.0000001$	   Exact

   0.0    	    1.000008      	    1.000000      	    1.000000      	    1.010303         	    1.000000

   1.0    	    2.718304      	    2.718282      	    2.718282      	    2.753353         	    2.718282

   2.0    	    7.389118      	    7.389057      	    7.389056      	    7.283063         	    7.389056

   3.0    	    20.085704     	    20.085539     	    20.085537     	    20.250467        	    20.085537

   4.0    	    54.598605     	    54.598155     	    54.598151     	    54.711789        	    54.598150

   5.0    	    148.414396    	    148.413172    	    148.413161    	    150.635056       	    148.413159

## Error Analysis

The results for $x=10$ are shown in the Table

   $h$    	   $e^{h}+e^{-h}$  	           $e^{h}+e^{-h}-2$

   $10^{-1}$     	    2.0100083361116070    	    $1.0008336111607230\times 10^{-2}$

   $10^{-2}$     	    2.0001000008333358    	    $1.0000083333605581\times 10^{-4}$

   $10^{-3}$     	    2.0000010000000836    	    $1.0000000834065048\times 10^{-6}$

   $10^{-5}$     	    2.0000000099999999    	    $1.0000000050247593\times 10^{-8}$

   $10^{-5}$     	    2.0000000001000000    	    $9.9999897251734637\times 10^{-11}$

   $10^{-6}$     	    2.0000000000010001    	    $9.9997787827987850\times 10^{-13}$

   $10^{-7}$     	    2.0000000000000098    	    $9.9920072216264089\times 10^{-15}$

   $10^{-8}$     	    2.0000000000000000    	    $0.0000000000000000\times 10^{0}$

   $10^{-9}$     	    2.0000000000000000    	    $1.1102230246251565\times 10^{-16}$

   $10^{-10}$    	    2.0000000000000000    	    $0.0000000000000000\times 10^{0}$

## Technical Matter in C/C++: Pointers

A pointer specifies where a value resides in the computer's memory (like a house number specifies where a particular family resides on a street).

A pointer points to an address not to a data container of any kind!

Simple example declarations:

          using namespace std; // note use of namespace
          int main()
         {
           // what are the differences?
           int var;
           cin >> var;
           int *p, q;
           int *s, *t;
           int * a new[var];    // dynamic memory allocation
           delete [] a;
        }

## Technical Matter in C/C++: [Pointer example I](https://github.com/CompPhysics/ComputationalPhysicsMSU/blob/master/doc/Programs/LecturePrograms/programs/IntroProgramming/cpp/program7.cpp)

        using namespace std; // note use of namespace
        int main()
        {
          int var;
          int *p;
          p = &var;
          var  = 421;
          printf("Address of integer variable var : %p\n",&var);
          printf("Its value: %d\n", var);
          printf("Value of integer pointer p : %p\n",p);
          printf("The value p points at :  %d\n",*p);
          printf("Address of the pointer p : %p\n",&p);
          return 0;
        }

## Dissection: Pointer example I

**Discussion.**

        int main()
        {
          int var;     // Define an integer variable var
          int *p;      // Define a pointer to an integer
          p = &var;    // Extract the address of var
          var = 421;   // Change content of var
          printf("Address of integer variable var : %p\n", &var);
          printf("Its value: %d\n", var);  // 421
          printf("Value of integer pointer p : %p\n", p);  // = &var
          // The content of the variable pointed to by p is *p
          printf("The value p points at :  %d\n", *p);
          // Address where the pointer is stored in memory
          printf("Address of the pointer p : %p\n", &p);
          return 0;
        }

## [Pointer example II](https://github.com/CompPhysics/ComputationalPhysicsMSU/blob/master/doc/Programs/LecturePrograms/programs/IntroProgramming/cpp/program8.cpp)

        int matr[2];
        int *p;
        p = &matr[0];
        matr[0] = 321;
        matr[1] = 322;
        printf("\nAddress of matrix element matr[1]: %p",&matr[0]);
        printf("\nValue of the  matrix element  matr[1]; %d",matr[0]);
        printf("\nAddress of matrix element matr[2]: %p",&matr[1]);
        printf("\nValue of the matrix element  matr[2]: %d\n", matr[1]);
        printf("\nValue of the pointer p: %p",p);
        printf("\nThe value p points to: %d",*p);
        printf("\nThe value that (p+1) points to  %d\n",*(p+1));
        printf("\nAddress of pointer p : %p\n",&p);

## Dissection: Pointer example II

        int matr[2];    // Define integer array with two elements
        int *p;         // Define pointer to integer
        p = &matr[0];   // Point to the address of the first element in matr
        matr[0] = 321;  // Change the first element
        matr[1] = 322;  // Change the second element
        printf("\nAddress of matrix element matr[1]: %p", &matr[0]);
        printf("\nValue of the  matrix element  matr[1]; %d", matr[0]);
        printf("\nAddress of matrix element matr[2]: %p", &matr[1]);
        printf("\nValue of the matrix element  matr[2]: %d\n", matr[1]);
        printf("\nValue of the pointer p: %p", p);
        printf("\nThe value p points to: %d", *p);
        printf("\nThe value that (p+1) points to  %d\n", *(p+1));
        printf("\nAddress of pointer p : %p\n", &p);

## Output of Pointer example II

        Address of the matrix element matr[1]: 0xbfffef70
        Value of the  matrix element  matr[1]; 321
        Address of the matrix element matr[2]: 0xbfffef74
        Value of the matrix element  matr[2]: 322
        Value of the pointer: 0xbfffef70
        The value pointer points at: 321
        The value that (pointer+1) points at:  322
        Address of the pointer variable : 0xbfffef6c

## File handling; C-way

        using namespace std;
        #include <iostream>
        int main(int argc, char *argv[])
        {
          FILE *in_file, *out_file;
          if( argc < 3)  {
            printf("The programs has the following structure :\n");
            printf("write in the name of the input and output files \n");
            exit(0);
          }
          in_file = fopen( argv[1], "r");// returns pointer to the  input file
          if( in_file == NULL )  { // NULL means that the file is missing
            printf("Can't find the input file %s\n", argv[1]);
            exit(0);

## File handling; C way cont.

         out_file = fopen( argv[2], "w"); // returns a pointer to the output file
         if( out_file == NULL )  {       // can't find the file
            printf("Can't find the output file%s\n", argv[2]);
            exit(0);
          }
          fclose(in_file);
          fclose(out_file);
          return 0;

## File handling, C++-way

        #include <fstream>

        // input and output file as global variable
        ofstream ofile;
        ifstream ifile;

## File handling, C++-way

        int main(int argc, char* argv[])
        {
          char *outfilename;
          //Read in output file, abort if there are too
          //few command-line arguments
          if( argc <= 1 ){
            cout << "Bad Usage: " << argv[0] <<
              " read also output file on same line" << endl;
            exit(1);
          }
          else{
            outfilename=argv[1];
          }
          ofile.open(outfilename);
          .....
          ofile.close();  // close output file

## File handling, C++-way

        void output(double r_min , double r_max, int max_step,
                    double *d)
        {
        int i;
        ofile << "RESULTS:" << endl;
        ofile << setiosflags(ios::showpoint | ios::uppercase);
        ofile <<"R_min = " << setw(15) << setprecision(8) <<r_min <<endl;
        ofile <<"R_max = " << setw(15) << setprecision(8) <<r_max <<endl;
        ofile <<"Number of steps = " << setw(15) << max_step << endl;
        ofile << "Five lowest eigenvalues:" << endl;
        for(i = 0; i < 5; i++) {
            ofile << setw(15) << setprecision(8) << d[i] << endl;

        }  // end of function output

## File handling, C++-way

        int main(int argc, char* argv[])
        {
          char *infilename;
          // Read in input file, abort if there are too
          // few command-line arguments
          if( argc <= 1 ){
            cout << "Bad Usage: " << argv[0] <<
              " read also input file on same line" << endl;
            exit(1);
          }
          else{
            infilename=argv[1];
          }
          ifile.open(infilename);
          ....
          ifile.close();  // close input file

## File handling, C++-way

        const char* filename1 = "myfile";
        ifstream ifile(filename1);
        string filename2 = filename1 + ".out"
        ofstream ofile(filename2);  // new output file
        ofstream ofile(filename2, ios_base::app);  // append

        //      Read something from the file:

        double a; int b; char c[200];
        ifile >> a >> b >> c;  // skips white space in between

        //      Can test on success of reading:

        if (!(ifile >> a >> b >> c)) ok = 0;

## Call by value or reference
C++ allows the programmer to use solely call by reference (note that call by reference is implemented as pointers). To see the difference between C and C++, consider the following simple examples.
In C we would write

           int n; n =8;
           func(&n); /* &n is a pointer to n */
           ....
           void func(int *i)
           {
             *i = 10; /* n is changed to 10 */
             ....
           }

whereas in C++ we would write

           int n; n =8;
           func(n); // just transfer n itself
           ....
           void func(int& i)
           {
             i = 10; // n is changed to 10
             ....
           }

## Call by value or reference
The reason why we emphasize the difference between call by value and call
by reference is that it allows the programmer to avoid pitfalls
like unwanted changes of variables. However, many people feel that this
reduces the readability of the code.

## Call by value and reference, F90/95

In Fortran we can use `INTENT(IN)`, `INTENT(OUT)`, `INTENT(INOUT)` to let the
program know which values should or should not be changed.

        SUBROUTINE coulomb_integral(np,lp,n,l,coulomb)
          USE effective_interaction_declar
          USE energy_variables
          USE wave_functions
          IMPLICIT NONE
          INTEGER, INTENT(IN)  :: n, l, np, lp
          INTEGER :: i
          REAL(KIND=8), INTENT(INOUT) :: coulomb
          REAL(KIND=8) :: z_rel, oscl_r, sum_coulomb
          ...

This hinders unwanted changes and increases readability.

## [Example codes in c++, dynamic memory allocation](https://github.com/CompPhysics/ComputationalPhysicsMSU/blob/master/doc/Programs/LecturePrograms/programs/Classes/cpp/program4.cpp)

        #include <iostream>
        #include <cmath>
        using namespace std; // note use of namespace
        int main (int argc, char* argv[])
        {
          int i = atoi(argv[1]);
          //  Dynamic memory allocation: need tp declare -a- as a pointer
          //  You can use double *a = new double[i];  or
          double *a;
          a = new double[i];
          // the first of element of a, a[0], and its address is the
          // value of the pointer.
          /* This is a longer comment
             if we want a static memory allocation
             this is the way to do it
          */
          cout << " bytes for i=" << sizeof(i) << endl;
          for (int j = 0; j < i; j++) {
            a[j] = j*exp(2.0);
            cout << "a=" << a[j] << endl;
          }
          // freeing memory
          delete [] a;
          // to check for memory leaks, use the software called -valgrind-
          return 0;           /* success execution of the program */
        }

## [Example codes in c++, writing to file and dynamic allocation for arrays](https://github.com/CompPhysics/ComputationalPhysicsMSU/blob/master/doc/Programs/LecturePrograms/programs/Classes/cpp/program5.cpp)

        #include <iostream>
        #include <cmath>
        #include <fstream>
        #include <iomanip>
        using namespace std; // note use of namespace

        // output file as global variable

        ofstream ofile;

        // Begin of main program

        int main(int argc, char* argv[])
        {
          char *outfilename;
          // Read in output file, abort if there are too few command-line arguments
          if( argc <= 2 ){
            cout << "Bad Usage: " << argv[0] <<
              " read also output file and number of elements on same line" << endl;
            exit(1);
          }
          else{
            outfilename=argv[1];
          }

          //  opening a file for the program
          ofile.open(outfilename);
          int i = atoi(argv[2]);
          //  int *a;
          //a = new int[i];
          double *a = new double[i];
          cout << " bytes for i=" << sizeof(i) << endl;
          for (int j = 0; j < i; j++) {
            a[j] = j*exp(2.0);
            // ofile instead of cout
            ofile << setw(15) << setprecision(8) << "a=" << a[j] << endl;
          }
          delete [] a; // free memory
          ofile.close();  // close output file
          return 0;
        }

## [Example codes in c++, transfer of data using call by value and call by reference](https://github.com/CompPhysics/ComputationalPhysicsMSU/blob/master/doc/Programs/LecturePrograms/programs/Classes/cpp/program6.cpp)

        #include <iostream>
        using namespace std;
        //  Declare functions before main
        void func(int, int*);
        int main(int argc, char *argv[])
        {
          int a;
          int *b;
          a = 10;
          b = new int[10];
          for(int i = 0; i < 10; i++) {
            b[i] = i;
            cout <<  b[i] << endl;
          }
          // the variable a is transferred by call by value. This means
          //  that the function func cannot change a in the calling function
          func( a,b);

          delete [] b ;
          return 0;
        } // End: function main()

        void func( int x, int *y)
        {
          // a becomes locally x  and it can be changed locally
          x+=7;
          //  func gets the address of the first element of y (b)
          // it changes y[0] to 10 and when returning control to main
          // it changes also b[0]. Call by reference
          *y += 10;  //  *y = *y+10;
          //  explicit element
          y[6] += 10;
          //   in this function y[0]  and y[6] have been changed and when returning
          // control to main  this means that b[0] and b[6] are changed.
          return;
        } // End: function func()

## [Example codes in c++, operating on several arrays and printing time used](https://github.com/CompPhysics/ComputationalPhysicsMSU/blob/master/doc/Programs/LecturePrograms/programs/Classes/cpp/program7.cpp)

        #include <cstdlib>
        #include <iostream>
        #include <cmath>
        #include <iomanip>
        #include "time.h"

        using namespace std; // note use of namespace
        int main (int argc, char* argv[])
        {
          int i = atoi(argv[1]);
          double *a, *b, *c;
          a = new double[i];
          b = new double[i];
          c = new double[i];

          clock_t start, finish;
          start = clock();
          for (int j = 0; j < i; j++) {
            a[j] = cos(j*1.0);
            b[j] = sin(j+3.0);
            c[j] = 0.0;
          }
          for (int j = 0; j < i; j++) {
            c[j] = a[j]+b[j];
          }
          finish = clock();
          double timeused = (double) (finish - start)/(CLOCKS_PER_SEC );
          cout << setiosflags(ios::showpoint | ios::uppercase);
          cout << setprecision(10) << setw(20) << "Time used  for vector addition=" << timeused  << endl;
          delete [] a;
          delete [] b;
          delete [] c;
          return 0;           /* success execution of the program */
        }