Numerical Methods | |
|
Trapezoidal ruleThe trapezoidal 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}{2}\left(f(x_0-dx) +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. The main advantage of this method is it's simplicity. |