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 fractional error as defined below.)
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. This gives you the fractional difference between your integrated answer and the true answer.
In [ ]:
# Put your code here
import math
Nstep = 10
begin = 0.0
end = 3.1415926
dx = (end-begin)/Nstep
sum = 0.0
xpos = 0.0
for i in range(Nstep):
thisval = math.sin(xpos)*dx
sum += thisval
xpos += dx
error = abs(sum-2.0)/2.0
print("for dx = {0:3f} we get an answer of {1:3f} and a fractional error of {2:4e}".format(dx,sum,error))
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
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
def f(x):
return np.exp(-2.0*x)
def dfdx(x):
return -2.0*np.exp(-2.0*x)
x = np.linspace(-3.0,3.0, 100)
dx = 1.0e-2
deriv = (f(x+dx)-f(x-dx))/(2.0*dx)
error = np.abs((deriv-dfdx(x))/dfdx(x))
plt.plot(x,error)
print("the average fractional error is:", error.mean())
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
The error should go down by approximately a factor of 10 as the step size goes down by a factor of 10, for both the numerical integration and differentiation.
In [ ]:
from IPython.display import HTML
HTML(
"""
<iframe
src="https://goo.gl/forms/IMkGVL5XnxqZM8EP2?embedded=true"
width="80%"
height="1200px"
frameborder="0"
marginheight="0"
marginwidth="0">
Loading...
</iframe>
"""
)