Quiz: Numerical methods in Python

Dr. Huidae Cho
Institute for Environmental and Spatial Analysis...University of North Georgia

1   Penalties

Filenames: -2 each (total up to -10)

Compress the following four (or five for bonus points) files into FirstLastname_quiz_2.zip and upload this one file to D2L ⇒ Assignments ⇒ Quiz 2 folder:

  • circ_area_for.py (10 points)
  • circ_area_while.py (10 points)
  • numeric_int_hard.py (30 points)
  • numeric_int_dyn.py (50 points)
  • circ_area_polar.py (optional for 30 bonus points)

Debugging: -3 per line added, removed, or edited

2   Question 1: An unbiased numerical method for estimating the circular area

10 points for each version

Implement the for (circ_area_for.py) and while (circ_area_while.py) versions of method 3 (rectangular height at $x+0.5\,dx$) in Python.


3   Question 2: Numerical integration of a hard-coded polynomial function

30 points; Either while or for version is acceptable; Use the unbiased mid-point method.

Write Python code (numeric_int_hard.py) that calculates the integration of $f(x)=-2x^4+2x^3+2x^2-2x+0.2$ between $x=-1.2$ and $x=1$ numerically using the mid-point method (rectangular height at $x+0.5\,dx$). Use the following fact to validate your code: \[\int_{-1.2}^1 f(x)\,dx=\left.-\frac{2}{5}x^5+\frac{1}{2}x^4+\frac{2}{3}x^3-x^2+0.2x+C\,\right|_{-1.2}^1=0.7665387.\]


Sample run:

n? 1000

4   Question 3: Numerical integration of an arbitrary quartic function

50 points; Either while or for version is acceptable; Use the unbiased mid-point method.

Write the same numerical integration code in Python (numeric_int_dyn.py) for an arbitrary quartic function \[f(x)=a_4x^4+a_3x^3+a_2x^2+a_1x+a_0\] and a user-defined range of $x$ \[x_s\le x\le x_e.\]

Sample run for the same quartic function in question 2:

a4? -2.0
a3? 2.0
a2? 2.0
a1? -2.0
a0? 0.2
xs? -1.2
xe? 1.0
n? 1000

5   Bonus question: Circular area in the polar coordinate system

20 points for the underestimating or overestimating method; 30 points for the unbiased mid-point method; Either while or for version is acceptable.

Write Python code (circ_area_polar.py) that estimates the circular area using $n$ triangles in the polar coordinate system. Use $d\theta$ for incrementing the angle $\theta$ from $0$ to $2\pi$. You can find $\pi$ and trigonometric functions from the math module (math.pi, math.cos(), math.sin(), math.tan()).

Please do NOT submit a random file just to earn partial points. There will be no partial points at all. It is a all-or-nothing bonus question. You earn 20 or 30 points depending on your method only if your code works correctly.

Sample run:

r? 1.0
n? 1000

6   Solution