Project: Visualization

Consider whether or not the images in the previous chapter helped you to understand Monte Carlo simulation. Would lists of numbers have been as helpful? Visualization has become a key application of computer technology in the recent past, thanks largely to the availability of inexpensive graphics cards. In this project, we will use turtle graphics to create an animated visualization of the Monte Carlo simulation from Chapter 10, where the dots appear randomly as they are generated by the simulation.

Chapter 6 introduced both the Python turtle module and the online Python documentation. The exercises will lead you to develop this visualization in stages. Use the documentation to find new turtle functions as you need them.

Exercises

  1. Begin the program visual.py by writing a line(x0, y0, x1, y1) function that draws a straight line from (x0, y0) to (x1, y1). In main(), set the world coordinates to have lower-left corner (−0.5,−0.5) and upperright corner (2, 2). Then use the line() function to draw x and y axes, as well as a horizontal line at height y = 1.
  2. Write a plot(f, x0, x1, n) function to draw a plot of y = f(x) over the interval [x0, x1] using n line segments. Most software draws function graphs in this way, as a sequence of very short straight lines. While you could use the line() function from the previous exercise to do this, use the following direct algorithm instead:
     Calculate dx, the width in the x direction of each segment.
     Begin at x = x0.
     Move to (x, f(x)) without drawing.
     For each segment:
      Increase x by dx.
      Draw to the next point (x, f(x))

    Use your function to plot f(x)=ex2 on [0, 2]. Experiment with the number of segments to get a nice image.

  3. Write a placedot(f, x, y) function that draws the correct dot at (x, y): a large blue dot if the point is below the graph of f, or a plain dot if it is above. Use your function to finish the visualization by drawing 50 random dots within the box. Hide the turtle at the end, and close the window when the user clicks in it.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.184.155