Numerical Python

profilepvkvs_1986
NumericalPythonAssignment.pdf

Assignment 04 Specifications Prep

Assignment 04 Specifications Last update - 2018-08-24 19:07Z

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 gain additional proficiency in the programming and documentation of Python for scientific applications in the Jupyter Notebook environment ● To gain practical experience in a root-finding problem.

The deliverable that you should plan on submitting is a SINGLE Jupyter Notebook (.ipynb) file that contains your code and discussions in a professional-looking formatted report.

Information on the VM that I have created for you, along with Jupyter setup is available via the course Moodle site. These assignments have been set up with the VM in mind, so you don’t have to worry about installing new software. You are, of course, welcome to use your own distributions, but you are on your own if you run into technical problems. All grading will be done in this Jupyter VM environment.

Solution of a real-world root finding problem

This problem is motivated by Problem 5.19 of your Chapra textbook, but adds to it and provides additional explanations.

The following graphic illustrates an empty sphere suspended in a body of fluid (usually water). Your goal will be to find the value of , the distance from the water surface to the top of the sphere, as a function of various parameters including the density of the sphere and the water, as well as the volume of the sphere.

Assignment 04 Specifications Prep https://docs.google.com/document/d/e/2PACX-1vQ-pCzDgygwd2RwhI...

1 of 7 9/2/2018, 5:08 PM

The portion of the sphere that will be exposed above the water surface depends on the buoyancy force, which is equal to the weight of fluid displaced by the submerged portion of an object. The Archimedes principle states this mathematically as follows:

where is the total sphere volume and is the below-water volume. The above-water volume is given as

and it naturally follows that

.

To find the value of for the specified parameters, you will want to find the root of the equation presented in the Archimedes principle

recognizing that will cancel out, and that and can be expressed in terms of and/or . To make it even clearer for you, your job is ultimately to find the value of which makes the above equation true (recast the equation to zero, and find the root of that equation).

To do this, you should - all within a Jupyter notebook - write code that defines the function you want to find the root of, plots the function (see following example - not correct solution) with the parameters in the title (these values should not be hardcoded into the plot statements - they should use constants defined at the top of the program)

Assignment 04 Specifications Prep https://docs.google.com/document/d/e/2PACX-1vQ-pCzDgygwd2RwhI...

2 of 7 9/2/2018, 5:08 PM

so that you can get a rough idea of where the root is, then uses the secant root finding method to calculate the root of the equation out to six significant digits. Your code should neatly print each iteration so that it is easy to see how it is converging. The following is an example of neatly-formatted output (but not correct solutions):

Further, your code should be neat and very well documented, and equations should be expressed as constants and variables so that the code is readable and reusable, and can be easily (by changing a single variable) used to perform a variety of experiments. In Appendix 1 I give an example of good coding versus bad coding

Here are specific requirements for the assignment

1. In a well-formatted introductory section of your Notebook, describe the problem in your own words. Jupyter allows you to use Markdown to neatly format your code. Additionally, if you want to typeset your equations, Jupyter allows you to do so in a Markdown section by using LaTeX within $ delimiters. However, this is not required of you, and it can be tedious if you’re not used to it. For those who might be interested, I’ve provided an example in Appendix 3. 2. Create a neatly-formatted and well-documented code that solves the problem described above. It should create a plot and a neatly-formatted set of iterations as described above. Your constants , and should be clearly defined at the top of your code so that they can be easily modified to perform experiments.

3. Perform a first experiment using values of , and . Save these values, as you will put them in a table below.

4. Perform another experiment, this time using a sphere density of . 5. Perform another experiment using the original sphere density, but placing the sphere in salt water rather than fresh water.

Assignment 04 Specifications Prep https://docs.google.com/document/d/e/2PACX-1vQ-pCzDgygwd2RwhI...

3 of 7 9/2/2018, 5:08 PM

6. Perform another experiment using the original sphere density, but placing the sphere in liquid mercury. 7. Create a neatly-formatted concluding Markdown section in your Jupyter notebook, which includes a table of the above results, and a discussion of why you think your model is correct based on your results, and any other knowledge you might be able to add. See Appendix 2 of this document for an example of creating a table in Markdown.

Your work should be submitted as a single .ipynb Jupyter notebook

Appendix 1 - Example of good coding and bad coding

The following is modeled after Problem 5.15 of the Chapra text, and is meant to show you both good and bad examples of coding equations.

Given

graphically estimate the value of that satisfies this equation, given

In other words, find the value of such that

In the following code I illustrate two acceptable ways to code the function, and one that’s unacceptable. They each produce exactly the same graphic.

G = 9.81 # m s-2 V = 5.0 # m s-1 T = 2.5 # s L = 4.0 # m

def fgood(H): return np.sqrt(2.0*G*H) * np.tanh( np.sqrt(2.0*G*H) / 2.0*L * T) - V

Assignment 04 Specifications Prep https://docs.google.com/document/d/e/2PACX-1vQ-pCzDgygwd2RwhI...

4 of 7 9/2/2018, 5:08 PM

def fgood2(H, g, v, t, L): return np.sqrt(2.0*g*H) * np.tanh( np.sqrt(2.0*g*H) / 2.0*L * t) - v

def fbad(H): return np.sqrt(19.62*H) * np.tanh( np.sqrt(19.62*H) *0.5 ) - 5.0

h = np.linspace(0,10) plt.plot(h, fgood(H=h)) plt.axhline(y=0, color='black') plt.xlabel("H") plt.ylabel("f(H)") plt.show()

h = np.linspace(0,10) plt.plot(h, fgood2(H=h, g=G, v=V, t=T, L=L)) plt.axhline(y=0, color='black') plt.xlabel("H") plt.ylabel("f(H)") plt.show()

h = np.linspace(0,10) plt.plot(h, fbad(H=h)) plt.axhline(y=0, color='black') plt.xlabel("H") plt.ylabel("f(H)") plt.show()

The one that is unacceptable is bad for two primary reasons

● It is not readable. The hard-coded numbers make it impossible to understand that this is an equation of several variables, and how they relate to each other. Further, if there is a mistake in this coding, it is extremely difficult to find ● It is not reusable. If I wanted you to solve this problem on Saturn, where the value of is much different, you would have to develop a new function with new and confusing hard-coded values in it.

Whether the first or second function is better is debatable. In general, we don’t like global variables in programs, but if we adopt the convention that only constants will be in full capitals, then it’s easier to assume that we simply need to go to the top of the program to see and/or modify their definitions. If we do this, it makes our function parameter list less complicated and, perhaps, easier to read.

Appendix 2 - creating a Table in Markdown

The following will create a simple table in Markdown. It’s centered, which isn’t real pretty, but it’s not always trivial to get it left aligned, so I’m not worrying about it for now.

Assignment 04 Specifications Prep https://docs.google.com/document/d/e/2PACX-1vQ-pCzDgygwd2RwhI...

5 of 7 9/2/2018, 5:08 PM

produces

Appendix 3 - Example of Markdown and LaTeX for typesetting equations

Assignment 04 Specifications Prep https://docs.google.com/document/d/e/2PACX-1vQ-pCzDgygwd2RwhI...

6 of 7 9/2/2018, 5:08 PM

Published by Google Drive – Report Abuse – Updated automatically every 5 minutes

Assignment 04 Specifications Prep https://docs.google.com/document/d/e/2PACX-1vQ-pCzDgygwd2RwhI...

7 of 7 9/2/2018, 5:08 PM