PLEASE READ CAREFULLY THIS ASSIGNMENT MUST BE DONE ON PYTHON
PLEASE READ CAREFULLY. THIS ASSIGNMENT MUST BE DONE ON PYTHON 3.5. PLEASE PROVIDE THE PYTHON CODE THAT WILL ALLOW ME TO SOLVE THIS.
PHYS381 Assignment#3 Due Wednesday Feb 8 Use scipy.integrate.romberg to evaluate the triple integral dz dy dx Try to obtain results for a range of precision eg. tol le-3, 1e-4, See how small you can make the tolerance and still obtain a result in under a minute or so. (It may be helpful to adjust divmax.) Determine the number of calls N to the integrand required for each value of tol. Plot the graph of N versus tol and discuss the result.Solution
def integLeft(a, b, f, nbins=10):
\'\'\'Return the integral from a to b of operate f victimization the mitt rule
>>> integLeft(0, 1, f, 4)
0.71875
\'\'\'
h = float(b-a)/nbins # enable input of whole number a and b
assert h > zero # warn if b not > a
assert type(nbins) == int
sum = 0.0
for n in range(nbins): # [0, 1, ... nbins-1]
add = add + h * f(a + n*h) # mitt rule
return sum
\'\'\'
The first issue you will notice regarding Python, is that there aren\'t any brackets and
semicolons or different punctuation to outline statements and code blocks. Thus
\"program structure\" is established by indentation. whereas this might seem to be a
bad plan to anyone World Health Organization has not tried Python, it truly is healthier for teaching
(and for knowledgeable programmers World Health Organization would possibly build easy blunders). The
interpreter catches indentation errors, and will not let the coed continue with
a refined eror as a result of a permed bracket is out of place. lecturers now not have
to nag students regarding correct vogue.
Notes on the code above:
1) Python contains a terribly versatile argument-passing syntax. If you embody a
\"default value\" with a parameter (nbins=10), that produces it a \"keyword\"
parameter. Keyword parameters area unit facultative in a very decision to the operate.
2) Python variables don\'t have any sort. Parameters a, b and f on top of area unit expected to
be 2 floats and a operate. whereas sort declarations would catch some errors,
Python expects the coder write express and complete checks in those
situations wherever defensive programming is vital (like user input).
The checks on top of do lots quite simply sort checking. They conjointly function
documentation. Students can learn early regarding sensible programming practices, if
teachers embody some dangerous inputs in their machine-controlled check cases. On the opposite
hand, if the stress is on learning algorithms, not programming, these tests
can be overlooked. Python permits that flexibility.
~\'\'\'
def integMid(a, b, f, nbins=10):
\'\'\'Return the integral from a to b of operate f victimization the center rule
>>> integMid(0, 1, f, 4)
0.828125
\'\'\'
h = float(b-a)/nbins
assert h > zero
assert type(nbins) == int
sum = 0.0
x = a + h/2 # initial center
whereas (x < b):
add += h * f(x)
x += h
return sum
def integTrap(a, b, f, nbins=10):
\'\'\'Return the integral from a to b of operate f victimization quadrangle rule
>>> integTrap(0, 1, f, 4)
0.84375
\'\'\'
h = float(b-a)/nbins
assert h > zero
assert type(nbins) == int
add = (h/2) * (f(a) + f(b)) # endpoints area unit special
for n in range(1, nbins): # [1, 2, ... nbins-1]
add += h * f(a + n*h)
return sum
if __name__ == \'__main__\':
\'\'\'
Everything from here down runs provided that this \"integration.py\" module is run
directly from the interpreter. Here is that the place for all the mussy \"overhead\"
needed to run tests and generate nicely-formatted output. The mess is unnoticed
if this module is foreign into another, as an example, to create this module half
of a library you would possibly decision to try and do integrations.
\'\'\'
def table_of_errors(a, b, f, exact):
\'\'\'
Print timings and a table of errors comparison these integration strategies to
a precise result. Show however the error decreases as o(1/nbins) or
o(1/nbins**2), reckoning on the strategy.
~\'\'\'
print
print \"nbins Left middle Trap\"
for nbins in [4, 40, 400]:
print \'%4s .9f .9f .9f\' the troubles (nbins,
integLeft(a,b,f,nbins), integMid(a,b,f,nbins), integTrap(a,b,f,nbins))
print
for nbins in [4, 40, 400]:
print \'%4s .9f .9f .9f\' the troubles (nbins,
integLeft(a,b,f,nbins) - actual,
integMid(a,b,f,nbins) - actual,
integTrap(a,b,f,nbins) - actual )
print
from time import time # from time module import time operate
t0 = time() # seconds
integLeft (0, 1, f, 1000)
t1 = time()
integMid (0, 1, f, 1000)
t2 = time()
integTrap (0, 1, f, 1000)
t3 = time()
print (\"Time for one thousand bins:\ .6f .6f .6f\" %
( (t1-t0), (t2-t1), (t3-t2) ) )
table_of_errors(0, 1, f, 5/6.)
\'\'\'
>>> table_of_errors(0, 1, f, 5/6.)
nbins Left middle lure
4 0.718750000 zero.828125000 zero.843750000
40 0.820937500 zero.833281250 zero.833437500
400 0.832084375 zero.833332812 zero.833334375
4 -0.114583333 -0.005208333 zero.010416667
40 -0.012395833 -0.000052083 zero.000104167
400 -0.001248958 -0.000000521 zero.000001042
Time for one thousand bins:
0.000865 zero.000763 zero.000814
\'\'\'
from doctest import testmod
testmod(verbose=True) # run the doctests when this module is run.
\'\'\'
Doctests serve 2 purposes:
1) They justify by example however your functions area unit referred to as.
2) They function \"unit tests\". If you ever build a amendment that breaks associate degree
existing check \"testmod\" can holler at you :>)
Don\'t let refined errors elapse. If a program is to fail, let it fail like a shot
and loudly.
Notes:
\"testmod\" needs the output to be precisely as within the example. we have a tendency to had to maneuver
the table_of_errors check outside of the operate (where testmod does not see it)
to allow for run-to-run variations within the timings.
~\'\'\'
don\'t have any text to check? don\'t have any text to check? Click \"Select Samples\".


