Day 18 Pre-class assignment

Goals for today's pre-class assignment

In this pre-class assignment, you are going to learn how to:

  • Numerically integrate a function
  • Numerically differentiate a function
  • Get a sense of how the result depends on the step size you use.

Assignment instructions

Watch the videos below and complete the assigned programming problems.


In [ ]:
from IPython.display import YouTubeVideo

In [ ]:
# WATCH THE VIDEO IN FULL-SCREEN MODE

YouTubeVideo("JXJQYpgFAyc",width=640,height=360)  # Numerical integration

Question 1: Write a function that uses the rectangle rule to integrates $f(x) = \sin(x)$ from $x_{beg}= 0$ to $x_{end} = \pi$ by taking $N_{step}$ equal-sized steps $\Delta x = \frac{x_{beg} - x_{end}}{N_{step}}$. Allow $N_{step}$ and the beginning and ending of the range to be defined by user-set parameters. For values of $N_{step} = 10, 100$, and $1000$, how close are you to the true answer? (In other words, calculate the error as defined above.)

Note 1: $\int_{0}^{\pi} \sin(x) dx = \left. -\cos(x) \right|_0^\pi = 2$

Note 2: The "error" is defined as $\epsilon = |\frac{I - T}{T}|$, where I is the integrated answer, T is the true (i.e., analytic) answer, and the vertical bars denote that you take the absolute value.


In [ ]:
# Put your code here

In [ ]:
# WATCH THE VIDEO IN FULL-SCREEN MODE

YouTubeVideo("b0K8LiHyrBg",width=640,height=360)  # Numerical differentiation

Question 2: Write a function that calculates the derivative of $f(x) = e^{-2x}$ at several points between -3.0 and 3.0, using two points that are a distance $\Delta x$ from the point, x, where we want the value of the derivative. Calculate the difference between this value and the answer to the analytic solution, $\frac{df}{dx} = -2 e^{-2x}$, for $\Delta x$ = 0.1, 0.01 and 0.001 (in other words, calculate the error as defined above).

Hint: use np.linspace() to create a range of values of x that are regularly-spaced, create functions that correspond to $f(x)$ and $\frac{df}{dx}$, and use numpy to calculate the derivatives and the error. Note that if x is a numpy array, a function f(x) that returns a value will also be a numpy array. In other words, the function:

def f(x):
    return np.exp(-2.0*x)

will return an array of values corresponding to the function $f(x)$ defined above if given an array of x values.


In [ ]:
# Put your code here

Question 3: For the two programs above, approximately how much does the error go down as you change the step size $\Delta x$ by factors of 10?

// put your answer here

Assignment wrapup

Question 4: What questions do you have, if any, about any of the topics discussed in this assignment after watching the videos and trying to write the programs?

// put your answer here

Question 5: How long did this assignment take you to complete?

// put your answer here

Congratulations, you're done!

Submit this assignment by uploading it to the course Desire2Learn web page. Go to the "Pre-class assignments" folder, find the dropbox link for Day 18, and upload it there.

See you in class!