[Tutor] Advise...

Johan Nilsson johan.nilsson at freenet.de
Fri Jan 28 18:40:37 CET 2005


Jacob,

Apart from all the other comments you received, here are my thoughts.

I think you could do one more thing to speed up your calculations and 
that is to use a more efficient method. The Reimann sum is not a very 
efficient.
One simple method that is rahter popular is Simpson's rule.

The calculations are not much more complicated than what you already have


equation347

You then you just need to make sure that the number of intervals are even.

Johan

Jacob S. wrote:

> Hi all.
>
>    Long time no see. (About five days, right?)
> Anyway, I know the first thing that some of you are going to say is 
> using eval(). I don't want a whole
> guilt trip on security risks and all that. I do not want to share the 
> code with anyone else while it's on my
> computer, and I sure don't have anyone near me that knows python. I 
> would be surprised if more than 50
> people in Portland, IN knew anything about python. So security is not 
> a problem. I guess what I'm
> looking for is someone who knows the Reimann Sum better than I do and 
> can tell me whether I can do
> something to make it more efficient. It's horribly slow with ten 
> thousand steps-- I don't know the notation
> very well, but it loops the loop O(step*(maximum-minimum)) times, 
> which is horribly sucky.
>    In case anyone doesn't know, Reimann's sum is the computer's 
> version of the definite integral, the area
> under a curve in a particular domain.
>
> Basic input and output.
> If a curve is a straight line, say y = x, the area under the line on 
> an interval can be found by geometric means.
> However, if you use a parabola, or any other function, say y = 3*x**2,
>
>
> What is the function? 3*x*x
> What is the minimum? 2
> What is the maximum? 5
> 117.000435
>
> Which, considering that it is supposed to be exactly 117, It's darn 
> good. Unfortunately, it also takes about
> 10 seconds to do all that.
> Any suggestions? Any advice? TIA
> Jacob Schmidt
>
>
> ############################
> from __future__ import division
> import psyco
> psyco.full()
> fofx = raw_input("What is the function? ")
> minimum = raw_input("What is the minimum? ")
> maximum = raw_input("What is the maximum? ")
> minimum = float(minimum)
> maximum = float(maximum)
> total = 0
> step = 100000
> x = minimum
> while minimum <= x <= maximum:
>    area = eval(fofx)*1/step
>    total = total+area
>    x = x+1/step
> print total
> #############################
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor


-------------- next part --------------
Skipped content of type multipart/related


More information about the Tutor mailing list