<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Day 18 Pre-class assignment
Goals for today's pre-class assignment
In this pre-class assignment, you are going to learn how to
Step1:  Question 1
Step2:  Question 2
<ASSISTANT_TASK:>
Python Code:
from IPython.display import YouTubeVideo 
# WATCH THE VIDEO IN FULL-SCREEN MODE
YouTubeVideo("JXJQYpgFAyc",width=640,height=360)  # Numerical integration
Explanation: 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.
End of explanation
# 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))
# WATCH THE VIDEO IN FULL-SCREEN MODE
YouTubeVideo("b0K8LiHyrBg",width=640,height=360)  # Numerical differentiation
Explanation: 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.
End of explanation
# 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())
Explanation: 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.
End of explanation
<END_TASK>