Numerical Methods | |
|
Simpson's ruleSimpson's rule states that the integral of a function over a small interval $dx$ is, $$\int \limits_{x_0-dx}^{x_0} f(x) dx \approx \frac{dx}{6}\left(f(x_0-dx) + 4f\left(\frac{x_0-dx/2}{2}\right)+f(x_0)\right).$$The integral, $$ I = \int \limits_a^b f(x) dx, $$ can then be performed by dividing $(b-a)$ into a number of small intervals and summing the results. The code snipet below performs this function. This is not numerically efficient because the function $f(x)$ is called twice for the same value of $x$ in every interval. |