Chapter 10

Simulation

Consider the problem of finding the area under the curve f(x)= e x 2 between x = 0 and x = 2:

Figure 10.1:

Figure showing graph of f(x)= e−x2.

Graph of f(x)= e x 2 .

Even if you know calculus, this is a difficult task. However, a relatively simple idea will allow us to approximate the area.

The idea begins with putting a box around the graph that contains it completely. For a function that stays positive, this means finding a maximum value m; in this case, we can use m = 1:

Figure 10.2:

Figure showing box containing area.

Box containing area.

Now imagine throwing random ...

Get A Concise Introduction to Programming in Python now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.