[Tutor] 3rd ? for today: can Python do integration (numeric and/or trigonometric) and how is the best way to go about this?

Daniel Yoo dyoo@hkn.EECS.Berkeley.EDU
Sun, 3 Sep 2000 19:19:57 -0700 (PDT)

I'll try to answer your three questions here:

1.  "how do you code Python to produce and/or generate a sound?"

I haven't done this myself yet, but there's a Python module for Windows
called 'winsound'.  You'll need to learn enough Python to use modules, but
it shouldn't be too bad.  Here's a link to the reference material on


2. "can Python be learned and used instead of CGI, Perl, and
Java/JavaScript or are those still necessary?"

Computer languages are made to do different things --- It is true that
some things are more appropriate to do in the other languages.  It really
depends on what sort of stuff you'll be doing.  It wouldn't hurt at all to
expand your knowledge by learning the other languages.

Of course, I'm answering this way because, otherwise, it would inflame the
advocates of those other languages... *grin*  For clarification: CGI isn't
really a language but a way of tying programs to HTML forms.  CGI can be
done with any reasonable programming language.

3. "can Python do integration (numeric and/or trigonometric)
and how is the best way to go about this?"

Sure!  You can write a quick Python program to do numerical integration.  
Doing things symbolically might be more work though, and I don't have the
background yet to talk about symbolic integration.  There are languages
suited to do symbolic stuff --- there's Maple and Mathematica.  However,
both of those programs cost money.  Does anyone know of a free symbolic
math package?

It's not too hard to write a program to do integration.  There are several
methods to choose from.  Here's one method:

Let's say we're working with the function y(x) = x.  We'd like to find
the area underneath this curve.  For now, we'll pretend that we don't know
it's a triangle with area A = (x**2)/2.

|   .
|  .
| .
0   1

An approximate way of doing this is to fit a rectangle underneath the
triangle, like this:

   . |
  .  |
 .   |
.    |

We can take the area of this rectangle easily.  This is admittedly silly
--- a triangle isn't a rectangle!  But let's say we use two rectangles
instead of just one:

  | .|
  |. |
---  |
 .|  |
. |  |

It's still silly, but not quite as much.  Let's try three thinner

 --| |
 |.| |
-| | |
 | | |

In fact, calculus says that as we use more rectangles, this silly way of
approximating the integral through rectangles approaches the true area
underneath that curve.  I think the term for this method is called a
"Riemann Sum".

This description is meant to be fuzzy --- you'll want to experiment with
the idea, and then write it as a program.  Try to program it yourself
first, and we can help you if you run into problems.