<jupyter_start><jupyter_text>CDES Honours - Test 1 Instructions* Read all the instructions carefully.* The test consists of **50 Marks**, with **two** hours available.* The written section is to be answered in the book provided.* You must only access Moodle tests and NOT Moodle.* The programming section is to be answered within this Jupyter notebook and resubmitted to Moodle.* Do not rename the notebook, simply answer the questions and resubmit the file to Moodle.* The moodle submission link will expire at exactly **11:15** and **NO** late submission will be accepted. Please make sure you submit timeously!* You may use the **Numpy** help file at: https://docs.scipy.org/doc/numpy-1.15.4/reference/* **NB!!!** Anyone caught using Moodle (and its file storing), flash drives or external notes will be awarded zero and reported to the legal office.  Written Section* Answer the following questions in the answer book provided. Question 1 (15 Marks)1. Consider the following equation:$$v_t = a v_{xx}.$$    1. Set up an **implicit** scheme using a forward difference approximation for time and a Crank-Nicolson difference approximation for the spatial variable. (5 marks)    2. Using the von Neumann stability analysis, discuss the stability of the scheme given in (A). You may assume that $a >0$. (10 marks)     Question 2 (5 Marks)2. Given the equation produced by the operation $P = \dfrac{\partial}{\partial t} + \alpha\dfrac{\partial}{\partial x}$:$$Pu = u_t + \alpha u_{x}, \qquad \alpha > 0.$$Evaluate the consistency of the FTFS scheme with the difference operator $P_{\Delta t, \Delta x}$.  Programming Section  Question 1Given the heat equation where:$$u_t = u_{xx}, \quad 0 < x < 1, \quad t \geq 0, \quad u(0, t) = u(1, t) = 0, \quad u(x, 0) = \sin(\pi x),$$and:$$\Delta x = 0.1, \quad \Delta t = 0.0005.$$Note: the analytical solution is:$$u(x, t) = e^{-\pi^2 t}\sin(\pi x)$$Implement an FTCS scheme to find an approximation at $t = 0.5$. The function should return the solution matrix $U$.<jupyter_code>def heat_eq(dt, dx, tf, f, D, alpha, beta):
    # YOUR CODE HERE
    raise NotImplementedError()
# Run this test cell to check your code
# Do not delete this cell
# 1 mark
# Unit test
dx    = 0.1
dt    = 0.005
tf    = 0.5
f     = lambda x: np.sin(np.pi * x) 
D     = 1
alpha = 0
beta  = 0
tans  = np.array([0.       , 0.309017 , 0.5877853, 0.809017 , 0.9510565, 1.       ,
                  0.9510565, 0.809017 , 0.5877853, 0.309017 , 0.       ])
ans   = heat_eq(dt, dx, tf, f, D, alpha, beta)
nt.assert_array_almost_equal(tans, ans[0])
print('Test case passed!!!')
# Hidden test
# No output will be produced
# 9 marks<jupyter_output><empty_output><jupyter_text>Question 2Implement a Crank-Nicolson scheme on the heat equation given in Question 1 and find an approximation to $t = 0.5$. The function should return the solution matrix $U$.<jupyter_code>def crankNicolson(dt, dx, tf, f, D, alpha, beta):
    # YOUR CODE HERE
    raise NotImplementedError()
# Run this test cell to check your code
# Do not delete this cell
# 1 mark
# Unit test
dx    = 0.1
dt    = 0.005
tf    = 0.5
f     = lambda x: np.sin(np.pi * x) 
D     = 1
alpha = 0
beta  = 0
tans  = np.array([0.       , 0.309017 , 0.5877853, 0.809017 , 0.9510565, 1.       ,
                  0.9510565, 0.809017 , 0.5877853, 0.309017 , 0.       ])
ans   = crankNicolson(dt, dx, tf, f, D, alpha, beta)
nt.assert_array_almost_equal(tans, ans[0])
print('Test case passed!!!')
# Hidden test
# No output will be produced
# 9 marks<jupyter_output><empty_output><jupyter_text>Question 31. Write a function which computes the absolute error and relative error (across the whole vector) at the final time `tf` of the two schemes in Questions 1 and 2. The function should also compute the norm at `tf` of the absolute difference between the approximate solution (of each scheme) and the true solution. The function should return 6 outputs, namely; `errExpAbs, errExpRel, errCNAbs, errCNRel, normExp, normCN`.<jupyter_code>dx    = 0.1
dt    = 0.005
tf    = 0.5
f     = lambda x: np.sin(np.pi * x) 
D     = 1
alpha = 0
beta  = 0

def errorFunction():
    # YOUR CODE HERE
    raise NotImplementedError()
# Run this test cell to check your code
# Do not delete this cell
# 1 mark
# Unit test
ans   = errorFunction()
# test output1 is vector
assert(ans[0].shape[0] > 1)
# test output2 is vector
assert(ans[1].shape[0] > 1)
# test output3 is vector
assert(ans[2].shape[0] > 1)
# test output4 is vector
assert(ans[3].shape[0] > 1)
# test output5 is scalar
assert(ans[4].shape == ())
# test output6 is scalar
assert(ans[5].shape == ())
print('Test case passed!!!')
# Hidden test
# No output will be produced
# 1marks
# Hidden test
# No output will be produced
# 1 marks
# Hidden test
# No output will be produced
# 1 marks
# Hidden test
# No output will be produced
# 1 marks
# Hidden test
# No output will be produced
# 1 marks
# Hidden test
# No output will be produced
# 1 marks<jupyter_output><empty_output><jupyter_text>2. Using the information of Question 3(1), plot the absolute and relative errors for both schemes on the same set of axis.<jupyter_code># 3 marks
# YOUR CODE HERE
raise NotImplementedError()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup