[Tutor] (no subject)

Steven D'Aprano steve at pearwood.info
Sat Sep 28 05:52:04 CEST 2013


On Fri, Sep 27, 2013 at 03:56:59PM -0400, Katie wrote:

> I am trying to write a program using Python v. 2.7.5 that will compute 
> the area under the curve y=sin(x) between x = 0 and x = pi. Perform 
> this calculation varying the n divisions of the range of x between 1 
> and 10 inclusive and print the approximate value, the true value, and 
> the percent error (in other words, increase the accuracy by increasing 
> the number of trapezoids). Print all the values to three decimal 
> places.


There are four parts to this:

1) Determine the true value of the area. You'll need some calculus 
   for that. Do you know how to work out the area under a graph?

   A = integral of sin(x) between 0 and pi

2) Write some code to use the trapezoid method to calculate the 
   approximate area under the graph, using N steps.

   Do you know how the trapezoid method works? I really need to 
   draw a picture here, which I can't do. Imagine if you sliced 
   the graph up into N equal-width slices. Each slice will be
   pi/N in width, since the whole graph is pi units across. 
   (Starting at 0, going to pi, so there are pi units in total;
   diving into N pieces means each one in pi/N across.)

   Each slice will look like a trapezoid, or specifically, a
   rectangle with a triangle at the top. I'm going to try 
   "drawing" the trapezoid, but rotated side-on because it is 
   easier:


   x1 +-----------------------------    y1
   .  |                             \
   .  |                              \
   x2 +-------------------------------  y2


   You should notes about the trapezoid method from class, or 
   from your text book. If all else fails, you can google for
   it and find *dozens* of websites that talk about it:

   https://duckduckgo.com/html/?q=trapezoid+method


3) Then you need some code to calculate the total area from the 
   area of all those slices.

4) And finally you need to calculate the difference between actual 
   area and the calculated area, and print to three decimal places.


Your over-all code will look something like this:


from math import pi, sin
actual_area = ***something goes here***
for N in range(1, 11):
    # calculate the approximate area
    total_area = 0
    width = pi/N  # width of each trapezoid  
    x1 = 0  # start at zero each time
    x2 = x1+width
    while x2 <= pi:
       y1 = ***something goes here***
       y2 = ***something goes here***
       area_of_slice = ***something goes here***
       total_area += area_of_slice
       x1, x2 = ***something goes here***
    # now print the results
    error = ***something goes here***
    print ("N = %d; Actual = %.3f; Calculated = %.3; Error = %.3f"
           % (N, actual_area, total_area, error))


Oh, there's an even better way to calculate the slices, instead of using 
a while-loop you can use a for-loop. But give it a go with the 
while-loop first, see if you can work out what needs to be done to 
change it to a for-loop. As usual, check your notes from class.

> I am not sure what the code should look like. I was told that I should 
> only have about 12 lines of code for these calculations to be done.

Well, I could probably squeeze everything into 12 lines if I really 
wanted to. But in my opinion, certainly 20 lines (not counting comments 
or blank lines) is plenty.



-- 
Steven


More information about the Tutor mailing list