Python programming

profilesaki1434
Assignment03Specs.pdf

Assignment 03 Specifications Last update - 2018-04-11 17:43Z

Due: Specified on the Moodle assignment page. ​Late submissions will automatically be marked as late and NOT GRADED​. Please make sure you submit your assignment far in advance of the stated deadline, so that you avoid losing credit due to bad networks, etc. It is recommended that you start early and if you have questions, ask them in the course Moodle Discussion forum. If you wait until the last minute to ask questions, it’s very likely that nobody will answer you in time. It is also recommended that you turn in something, even if it’s not complete. Even if you weren’t able to complete the assignment, taking the time to explain what you tried and what problems you faced will likely prevent you from getting a zero. Grading​: Grading is somewhat subjective in this assignment, on a scale of 0 to 10, roughly equivalent to a standard grading scale where 10 would be an A+ (Excellent), 9 would be a B+/A- (Good), 8 would be a C+/B- (Fair), etc. In this particular assignment, if you do all that is asked of you, you should receive a 10. If you neglect, or poorly explain 2 or more minor items, you should receive a 9, and so on. The primary goals of this assignment are

● To continue gaining familiarity with Python scientific computing environments to ○ Solve problems associated with errors, approximation and Taylor series ○ Solve problems associated with root finding methods

The deliverable that you should plan on submitting is a PDF-format formal report on the activities described below and, optionally, an IPython Notebook file.

Practice in programming and convergence testing In Assignment 02, you were tasked with using a class file that I gave you, ​Integrate1D.py​, to explore convergence of numerical integration, using different step sizes (or numbers of rectangles). In this follow-on, I’m providing you with the same class, this time in Assign03.tar.gz​, http://borealscicomp.com/Academic/HU/CISC600/Assign03/

and I want to give you experience in implementing and using a new method within a class. You will integrate a new function with two different approaches - using rectangles and using trapezoids - and report on their performance by producing tables and a single plot. So, in this case you should create a new method, ​trapezoid_integrate()​, which uses trapezoids rather than rectangles to estimate the area under a curve, and then explore the convergence behaviour both with rectangles and trapezoids, plotting both errors in a single graph. Much like you did in Assignment 02, you should explore and document the effects of using increasing numbers of rectangles to integrate the function,

(x) dxf = ∫ π

2 π

sin(x) log(x)

Starting with rectangles, you should iterate with increasing numbers of rectangles0 n = 1 (increase by a factor of 10 at each iteration) until the absolute value of the difference between the result at the current iteration and the previous iteration is less than a value of ..0E ε = 1 − 6 The output of this program should be a table that shows, for each iteration, the number of rectangles, the width of each rectangle ( ), the estimated value of the definite integral, and theh “error” (the absolute value of the difference between the current answer and the answer from the previous iteration). The table should be neatly formatted, with all numbers aligned, looking something like

n h integral error  10 1.0E-1 ????.?E?? ????.?E??  100 1.0E-2 ????.?E?? ????.?E?? . . . . . . . . . . . .

While you are iterating, you should save the values of ​h​ and the ​error​ in lists or arrays so that you can create a pyplot plot of ​error​ vs ​h​. Then, you should perform the same exercise with the trapezoids, producing a similar table, and saving the values of ​h​ and ​error​. Finally, you should create a single plot of both curves, plotting the curves of ​error​ vs ​h​, labeling the two curves appropriately. The ​x​ axis (​h​) should be logarithmic, and you can use your

judgement about whether the ​y​ axis (​error​) should also be logarithmic. Be sure to label the axes meaningfully, and to include a meaningful title. To complete this task, you should embed within your report

● The tables and single graphic for the rectangle and trapezoid solutions ● The well-documented code for your program, and your modified ​Integrate1D.py​. Please

be sure to use a monospace font so that the code is easy to read. ● Any comments you have about this task.

Applied root-finding problem The basis for this task comes from Problem 5.15 in your textbook. As depicted in the following figure, the velocity of water, ​v​ (m/s), discharged from a cylindrical tank through a long pipe can be computed as

v = √2gH tanh t( 2L√2gH )

where ​g​ = 9.81 m/s​2​, ​H​ = initial head (m), ​L​ = pipe length (m), and ​t​ = elapsed time (s). Determine the head needed to achieve ​v​ = 5 m/s in 2.5 s for a 4-m-long pipe. You should cast this equation so that it becomes a root-finding problem, and then write a Python program that will

1. Plot the function so that you can estimate the solution to two significant figures (for example 8.3, which isn’t anywhere near the solution)

2. Use the bisection method to approximate the solution so that the root is zero out to three significant figures (0.00). You should start with an interval of . Create a0.0, 2.0] x = [ neatly formatted table that will show, for each iteration

a. The interval size b. The approximated head, ​H c. The function evaluated at this head (which should be approaching zero)

3. Use the approximated head, ​H​, from the previous step and check your solution by calculating the velocity, ​v

To complete this task, you should embed in your report

● The graph and the table produced by your program ● The well-documented, neatly formatted code for your program

Applied root-finding problem This task is based on Problem 6.19 of your textbook You are designing a spherical tank (see figure below) to hold water for a small village in a developing country. The volume of liquid it can hold can be computed as

hV = π 2 3 3R−h[ ]

where ​V​ = volume (m​3​), ​h​ = depth of water in tank (m), and ​R​ = the tank radius (m). If ​R​ = 3 m, what depth must the tank be filled to so that it holds 30 m​3​? Create a Python program that will use the Newton-Raphson method to determine your answer to a reasonable accuracy, printing pertinent information at each iteration - ​x​new​ and ​f(x​new​)​. Note that an initial guess of ​R​ will always converge. To complete this task, you should

● write a short description of your solution technique ● show your documented Python code ● show the output from each iteration

Root finding for nonlinear system of equations Write Python code to solve the following system of equations using the Newton-Raphson method x y 21 3 + 3 2 =

2y 2 0 x2 + + = You should start by making plots of the graphs to locate approximate values of the intersections, then use this information to obtain solutions to a reasonable degree of accuracy. To complete this task, you should

● write a short description of your solution technique ● show your documented Python code ● show the output from each iteration

Submitting the report You should ​submit your report in PDF and/or ipynb format through Moodle​ before the deadline (preferably long before the deadline, so that you do not suffer from any last-minute problems). It is permissible to submit “partially-completed” reports before the deadline, in case you want to make sure “something” gets submitted. Only the latest submission will be graded. Your report should contain the items requested above. I have tried to ​highlight key deliverables in red​. If you choose to submit part of your assignment in PDF and other parts in ipynb formats, please use the PDF document as your “root” document, and refer me to the ipynb document where necessary. It should be easy to follow. Also, if you choose to use the IPython Notebook, please include in your report your comments about whether you find it to be valuable or not.