More advanced function programming: Monte Carlo integration
Many scientific problems involve computations of integrals. If f is a scalar function of one variable and integratable, we can write the integral as:
where F(x) is the anti-derivative of f. Calculating the integral of most functions analytically can be a daunting and often impossible task, which is why we turn to the numerical alternative.
There are different ways to compute the integral numerically. Here we will write a function that uses the Monte Carlo method. Later we discuss the other integration methods that come with Octave. Now, assume that f is positive in the interval x ∈ [a;b] and has a maximum M here. We can then form a rectangle with area given by (b a) x M. This is illustrated in the figure below. Here random points lie inside the rectangle with area (b a) x M, where b = 2, a = 0.5 and M = 0.4356. The blue curve represents the graph of a function f which integral we seek to compute.
If we randomly set points ...