Using the Euler method which was derived in the lecture notes, a short
program can be written to evaluate the concentration over a grid in
time. Here a function
is first defined and then the Table
function is used to generate the concentrations up to a time of
100. The time step size h is set to 1 so 100 steps are required.
To make it easier to change the time step size and/or the total length
of time, the number of timesteps is evaluated from the totaltime and
stepsize.
 | Clear[k,h,nsteps,a,n,data];
k=0.01;
totaltime=100;
h=1;
nsteps = Round[totaltime/h];
a[n_] := a[n] = a[n-1] - k*h*a[n-1] ;
a[0] = 1;
data = Table[{ n*h, a[n] }, { n, 0, nsteps }]; |
Here the function a[n] is defined by the recursion relation obtained
by the finite difference approximation of the first derivative in the
rate law. The initial concentration of A is here chosen to be 1.
The Table function evaluates the function and creates a list of
the values. The generated list can be graphed using ListPlot
 | ListPlot[data, PlotJoined -> True ] |
Q: Repeat the calculation with smaller and larger values for the
step size, h, to find what range will give accurate results to, say,
three significant figures. Compare the numerical calculations with
the analytical solution by plotting them on the same graph.
Hannes Jonsson
Modified by Thomas L. Marchioro II
and the Undergraduate Computational Engineering and Science project