Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

UCT PHY4000W Computational Physics

Tutorial 4

SLXCHL001 - Chloé Sole


(Dated: April 29, 2018)

Question 2

Given the integral in equation 1, along with its analytical answer we were asked to evaluate it numerically using
multiple methods.

r
πr2
Z p
dx r2 − x2 = (1)
−r 2
The first method used was a self implemented trapezoidal rule which calculated the area by approximating it to N
trapezoids defined by the function at the endpoints of the trapezoid.
For a given (N+1) dimension x and f(x)=y array and x points being equally spaced this boils down to:
((x[1]-x[0])/2)*(y[0]+2*np.sum(y[1:-1]+y[-1]))
The visualized area can be seen in figure 1 in the top plot. The relative error (:= |πcalc − π|/π) has also been plotted
in the bottom plot in figure 1. As expected we see a rapid decrease in the error of the calculated integral as the
number of trapezoids increase.

Similarly as was done for the trapezoidal rule, the Simpson rule was implemented using the algorithm:
((x[1]-x[0])/6)*(y[0]+2*np.sum(y[1:-1])+4*np.sum(func((x[:-1]+np.roll(x,1)[:-1])/2))+y[-1])
Refer to figure 1 (the bottom plot) for the relative error in the implemented Simpson rule integration area.

Figure 1 also shows the relative error in the scipy.integrate.trapz and scipy.integrate.simps functions. As
expected my error in the trapezoidal rule is the same as the error from the scipy.integrate.trapz. My Simpson
implementation is typically larger than the scipy implementation. I believe this to be a product of the additional
correction terms in the expansion which I didn’t account for. The self-implemented Simpson rule has some weird
behavior at low N which I can’t explain.

To calculate the expected N at which the Simpson rule would produce a precision similar to that of double precision
(≈ 10−16 ) I first considered just increasing N in large steps until it converged to that value however this proved to
being very slow (even in steps of 1000) and became tiresome at around precision values of 10−11 . I then tried the
approach of fitting a hyperbolic function to the calculated values and then using the best fit function to predict when
it would have the desired precision. This gave an unreasonable result of N=65 which is not true since at N=160 we
still have not reached a precision less than 10−4 .

For the Gaussian Quadrature method the weightings and the x values were chosen by those given to us in the lecture
slides and for N=1,2,3,4 the relative error has been plotted in figure 1 in the bottom plot. Clearly this method reduces
error much faster (as a function of N). Reaching much more precise approximations for the integral for much less effort.

TABLE I: Table showing the compared relative error and speed of different implementations and methods to
calculate the desired integral. For the Simpson and Trapezoidal methods N=160 for the functional no N was
required as all it took was the function to be integrated and the bounds over which to integrate as parameters.
Method Trapezoidal Simpson Functional Integration
Implementation Own Scipy Own Scipy scipy.integrate.quad
Relative error 0.000523 0.000523 0.00464 0.000204 1.131e-15
Speed (×10−5 s) 1.725 1.275 3.058 2.939 105.022

Table I shows the speed and the relative error of all the methods and implementations explored. Even at the time
taken by the scipy functional integration method (for much larger N) the precision of the Trapezoidal and the Simpson
method are not on the same order of magnitude at all.
2

FIG. 1: Figure showing the effective error of the different methods used to evaluate the integral in equation 1. Note
that Gaussian Quadrature was only evaluate for N=1,2,3,4. The top plot is a visualization of the trapezoidal rule for
N trapezoids.

You might also like