[Tutor] (no subject)
Fabrice POMBET
fp2161 at gmail.com
Sat Sep 28 13:48:00 CEST 2013
> To be a little more complete,
(1)A=integral of f between a and b=F(b)-F(a)
where F is the primitive of f (i.e f is the derivative function of F)
in your little example, f=sin(x) <=> F=-cos(x), and therefore:
A=-cos(pi)-(-cos(0))=-(-1)-(-(1))=2
(2) The trapezoid method is a way of approximating the area under the curve (you are missing the 'curved' area on top of the trapezoid, hence the approximation). The more there are trapezoids, the smaller the curvy bit gets, the more accurate it gets!
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Sat, 28 Sep 2013 13:52:04 +1000
> From: Steven D'Aprano <steve at pearwood.info>
> To: tutor at python.org
> Subject: Re: [Tutor] (no subject)
> Message-ID: <20130928035203.GS7989 at ando>
> Content-Type: text/plain; charset=us-ascii
>
> 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
>
>
> ------------------------------
>
> Message: 2
> Date: Sat, 28 Sep 2013 14:44:25 +1000
> From: Steven D'Aprano <steve at pearwood.info>
> To: tutor at python.org
> Subject: Re: [Tutor] Help on class
> Message-ID: <20130928044425.GT7989 at ando>
> Content-Type: text/plain; charset=iso-8859-1
>
> On Fri, Sep 27, 2013 at 10:07:39PM +0800, bharath ks wrote:
>> Hello,
>>
>> May i know why object 'c' does not prompt for employee name and
>> employee id in the following code i get out put as?
>
> Default values in Python functions and methods are evaluated once only,
> when the function or method is defined ("compile time"), not each time
> it is run.
>
> You can test this yourself:
>
> import time
> def long_function():
> # Simulate a long-running calculation
> print("Calculation started at %s" % time.ctime())
> time.sleep(30)
> print("Calculation completed at %s" % time.ctime())
> return 42
>
>
> def test(value=long_function()):
> print(value)
>
>
> If you run this code, as I did, you will get something like:
>
> py> def test(value=long_function()):
> ... print(value)
> ...
> Calculation started at Sat Sep 28 14:37:38 2013
> Calculation completed at Sat Sep 28 14:38:08 2013
>
> only once, when the test function is created. Then, you can run that
> function as often as you like without the lengthy calculation being
> repeated:
>
>
> py> test()
> 42
> py> test()
> 42
> py> test(23)
> 23
> py> test()
> 42
>
>
> This is called "eager evaluation of default arguments", as opposed to
> "lazy evaluation of default arguments". To get lazy evaluation, stick
> the code inside the function, with a sentinel value:
>
> py> def test2(value=None):
> ... if value is None:
> ... value = long_function()
> ... print(value)
> ...
> py> test2()
> Calculation started at Sat Sep 28 14:41:08 2013
> Calculation completed at Sat Sep 28 14:41:38 2013
> 42
> py> test2(23)
> 23
> py> test2()
> Calculation started at Sat Sep 28 14:42:13 2013
> Calculation completed at Sat Sep 28 14:42:43 2013
> 42
>
>
> --
> Steven
>
>
> ------------------------------
>
> Message: 3
> Date: Sat, 28 Sep 2013 15:39:51 +1000
> From: Amit Saha <amitsaha.in at gmail.com>
> To: Jacqueline Canales <jackiexxduh3 at gmail.com>
> Cc: "tutor at python.org" <tutor at python.org>
> Subject: Re: [Tutor] List Python Question..Please help
> Message-ID:
> <CANODV3mOt-uLCZyBVxd-Qh0L7hVEd2Gb3n9DCMfK5xMOd87hjA at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
>
> On Sat, Sep 28, 2013 at 3:36 PM, Jacqueline Canales
> <jackiexxduh3 at gmail.com> wrote:
>> Thank you guys so much i was able to figure it out. I definitely thought to
>> much into the the problem and made it harder on myself. Cant thank you
>> enough for assisting me. I have one more problem with the coding tho.
>>
>> composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen']
>> new_list = []
>> person = new_list
>> for person in composers:
>> if person[0].lower() == person[-1].lower():
>> print(person)
>>
>> Output:
>> Saint-Saens
>> Easdale
>> Nielsen
>
> Great work!
>
>>
>> composers = ['Antheil', 'Saint-Saens', 'Beethoven', 'Easdale', 'Nielsen']
>> new_list = []
>> person = new_list
>> for person in composers:
>> if person[0].lower() == person[-1].lower():
>> new_list.append(person)
>> print(new_list)
>>
>> output:
>> ['Saint-Saens']
>> ['Saint-Saens', 'Easdale']
>> ['Saint-Saens', 'Easdale', 'Nielsen']
>
>
>
>>
>> How can i make the output of the names into just one individual list.
>
> You mean, just print it once? The last line of your output?
>
> Just print after the loop is over.
>
>
>
>
>
> --
> http://echorand.me
>
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> https://mail.python.org/mailman/listinfo/tutor
>
>
> ------------------------------
>
> End of Tutor Digest, Vol 115, Issue 60
> **************************************
More information about the Tutor
mailing list