Question: Q 2 : In this question, you will use Python to perform numerical differentiation the function f ( x ) = e 3 x .

Q2: In this question, you will use Python to perform numerical differentiation the function
f(x)=e3x. Follow these steps:
Create an array named H containing 100 evenly spaced values ranging from 0.001 to 0.5.
These values will serve as the step sizes for the numerical differentiation. See hint at the
end of the question.
For each step size in H, calculate the forward difference approximation of f(x) at xi=0.
Then, determine the percentage of the absolute relative error for each step size. Plot this
percentage against the values in H and include the plot in your report.
Repeat the process for the backward difference approximation of f(x) at xi=0.
Calculate the percentage of the absolute relative error for each step size in H and plot
these percentages against H. Include this plot in your report.
Do the same for the centred difference approximation of f(x) at xi=0). Calculate the
percentage of the absolute relative error for each step size in H and plot these
percentages against H. This plot should also be included in your report.
Analyse and discuss what happens to the error as the step size decreases.
Compare the three methods and determine which approximation is the most accurate.
Include the Python code used for these steps in your report.
Hint:
import matplotlib.pyplot as plt
import numpy as np
def f(x) :
return np.exp(3**x)
# Derivative of the function
def df(x) :
return 3**np*exp(3**x)
# Create an array H with 100 numbers evenly distributed between 0.001 and 0.5
H=np. inspace (0.001,0.5,100)
# Exact derivative at x=0
exact_derivative =df()
# Forward difference approximation
forward_diff =f(H)-f()H
...
# Calculate the percentage of the absolute relative error
forward_error = np.abs((forward_diff - exact_derivative
 Q2: In this question, you will use Python to perform numerical

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!