Integrate the following equation using the Trapazoid Rule, Simpson's rule, and Gauss-Legendre quadrature from x=0 to x=1:
$$f(x) = x\cdot\tanh(d\cdot(x-0.5))+1.$$Use d=10. Make a log-log plot of the relative error of the methods versus the number of grid points. To get the "exact" integral, you can use the built-in integrator.
In python, use the following:
from scipy.integrate import quad
(Ie, abserr) = quad(f,0,1)
When varying the number of points, because we are plotting on a log scale, and because the error goes as a power, we want the number of points to increase as a power. I did something like:
npoints = 2^1, 2^2, 2^3, 2^4, 2^5, 2^6, 2^7, 2^8, 2^9, 2^10,
for 10 runs. Then, when you plot on a log scale you'll have an even spacing of points on the x-axis.
Use your results to verify that the trapazoid and Simpson methods have convergence rates $O(\Delta x^2)$ and $O(\Delta x^4)$, respectively.
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import quad
This problem is from Hoffman's book: Chapter 4 problem 106 (b).
The following temperature and rate data were measured:
T(K) | K$_f$ |
---|---|
1000 | 7.5E15 |
2000 | 3.8E15 |
3000 | 2.5E15 |
4000 | 1.9E15 |
5000 | 1.5E15 |
Find parameters $B$, $\alpha$, and $E/R$ for the model
$$K = BT^{\alpha}\exp\left(-\frac{E}{RT}\right).$$Plot the model with the best coefficients with a smooth line (use lots of T points). Also plot the measured points using data markers (not lines).
Evaluate the function $f(x)=\exp(4x)$ at $x=0.55$ using a cubic spline fit to points $x=0,\,0.2,\,0.4,\,0.8,\,1.0.$ Also report the relative error at $x=0.55$. (Code the spline yourself.)