<jupyter_start><jupyter_text>CDEs Honours - Test 2 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** and **Matplotlib** files found pre-opened in the browser.* **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 (10 Marks)1. Using the von Neumann stability analysis, discuss the stability of the scheme given by:$$u_i^{n + 1} = u_i^n - \dfrac{c\Delta t}{\Delta x}(u_{i}^n - u_{i - 1}^n)$$     Question 2 (5 Marks)2. For the Lax-Fredrichs scheme, the difference operator is given by:$$P_{\Delta x, \Delta t}u = \dfrac{u_i^{n+1} - \frac{1}{2}\left(u_{i + 1}^n + u_{i  -1}^n\right)}{\Delta t} + a\dfrac{u_{i + 1}^n - u_{i - 1}^n}{2\Delta x}$$Evaluate the consistency of the scheme using the Taylor series. Show all working out. Question 3 (10 Marks)Consider the following two-dimensional heat equation:$$v_t = \nu (v_{xx} + v_{yy}).$$Set up a two-stage implicit scheme using the Alternating Direction Implicit method.  Programming Section  Question 1 - 7 MarksConsider the model partial differential equation given by:$$u_t = u_x,\ \ \quad x \in \mathbb{R},\ \ \quad t \geq 0,$$together with the initial data:$$u(x, 0) = \begin{cases} \cos^2 x & |x| \leq \frac{\pi}{2} \\ 0 & |x| \geq \frac{\pi}{2}\end{cases}.$$Solve this initial-value problem using the leap frog scheme. The function should take as inputs, the stepsize `dx`, the mesh ratio `lamb`, the number of number of iterations to perform `N`, the left boundary `l`, and the right boundary `r`. The function should return the solution matrix containing `N + 1` time steps (initial condition inclusive).Given the structure of the leap frog scheme. You may use the analytical solution to construct $u(x, \Delta t)$, i.e. the second time step $\bar{u}^1$.**General Leap Frog:**$$\dfrac{u_i^{n + 1} - u_i^{n - 1}}{2\Delta t} = -c\left(\dfrac{u_{i + 1}^n - u_{i  -1}^n}{2\Delta x}\right) $$**General Solution to Advection Equation**$$u(x, t) = F(x - ct)$$<jupyter_code>def advection(dx, lamb, N, l, r):
    # 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
lamb = 0.99
N    = 15
l    = -3
r    = 3
tans = np.array([0.       , 0.       , 0.       , 0.       , 0.       , 0.       ,
       0.       , 0.       , 0.       , 0.       , 0.       , 0.       ,
       0.       , 0.       , 0.       , 0.0050038, 0.0288888, 0.0715556,
       0.1313031, 0.2057494, 0.2919266, 0.386399 , 0.4854002, 0.5849836,
       0.6811789, 0.7701512, 0.8483534, 0.9126678, 0.9605305, 0.9900333,
       1.       , 0.9900333, 0.9605305, 0.9126678, 0.8483534, 0.7701512,
       0.6811789, 0.5849836, 0.4854002, 0.386399 , 0.2919266, 0.2057494,
       0.1313031, 0.0715556, 0.0288888, 0.0050038, 0.       , 0.       ,
       0.       , 0.       , 0.       , 0.       , 0.       , 0.       ,
       0.       , 0.       , 0.       , 0.       , 0.       , 0.       ,
       0.       ])
sans = advection(dx, lamb, N, l, r)[0, :]
nt.assert_array_almost_equal(tans, sans)
print('Test case passed!!!')
# Run this test cell to check your code
# Do not delete this cell
# 1 mark
# Unit test
dx   = 0.1
lamb = 0.99
N    = 15
l    = -3
r    = 3
tans = np.array([0.       , 0.       , 0.       , 0.       , 0.       , 0.       ,
       0.       , 0.       , 0.       , 0.       , 0.       , 0.       ,
       0.       , 0.       , 0.0050038, 0.0288888, 0.0715556, 0.1313031,
       0.2057494, 0.2919266, 0.386399 , 0.4854002, 0.5849836, 0.6811789,
       0.7701512, 0.8483534, 0.9126678, 0.9605305, 0.9900333, 1.       ,
       0.9900333, 0.9605305, 0.9126678, 0.8483534, 0.7701512, 0.6811789,
       0.5849836, 0.4854002, 0.386399 , 0.2919266, 0.2057494, 0.1313031,
       0.0715556, 0.0288888, 0.0050038, 0.       , 0.       , 0.       ,
       0.       , 0.       , 0.       , 0.       , 0.       , 0.       ,
       0.       , 0.       , 0.       , 0.       , 0.       , 0.       ,
       0.       ])
sans = advection(dx, lamb, N, l, r)[1, :]
nt.assert_array_almost_equal(tans, sans)
print('Test case passed!!!')
# Run this test cell to check your code
# Do not delete this cell
# 1 mark
# Unit test
dx   = 0.1
lamb = 0.99
N    = 15
l    = -3
r    = 3
tans = np.array([ 0.004347 ,  0.025097 ,  0.063355 ,  0.120947 ,  0.1948061,
        0.2792473,  0.3715433,  0.4702795,  0.5709834,  0.6677464,
        0.7571661,  0.8372808,  0.9045055,  0.9547867,  0.9864593,
        0.9994492,  0.9929425,  0.9661625,  0.9204353,  0.858487 ,
        0.7825649,  0.6949164,  0.5992208,  0.4999464,  0.4009085,
        0.3055431,  0.2177426,  0.141349 ,  0.0793848,  0.0341966,
        0.0075785, -0.000149 , -0.0009073,  0.000784 ,  0.0015059,
        0.0002043, -0.0003286,  0.0007653,  0.0011777,  0.0002017,
       -0.0002298,  0.0004765,  0.0006891, -0.0000871, -0.0004477,
        0.0000379,  0.0001615, -0.0004416, -0.0007114, -0.0003237,
       -0.0001971, -0.0006104, -0.0007678, -0.0004255, -0.0002505,
       -0.0004509, -0.0005064, -0.0002535, -0.0000439,  0.       ,
        0.       ])
sans = advection(dx, lamb, N, l, r)[-1, :]
nt.assert_array_almost_equal(tans, sans)
print('Test case passed!!!')
# Hidden test
# No output will be produced
# 4 marks<jupyter_output><empty_output><jupyter_text>Question 2 - 4 MarksContinuing with Question 1, plot the initial condition and final solution (i.e. timestep $N$), on the same set of axis.<jupyter_code># YOUR CODE HERE
raise NotImplementedError()<jupyter_output><empty_output><jupyter_text>Question 3 - 4 MarksThe Root Mean Square Error (RMSE) is defined as:$$\sqrt{\frac{\sum_{i=1}^{n}\left(\hat{u}_{i}-u_{i}\right)^{2}}{n}},$$where $\hat{u}$ is the approximation obtained from your scheme in Question 1, $n$ the length of the vector, and $u$ the true solution obtained from the general solution stated in Question 1. Write a function that computes the RMSE between your approximation and the true solution. The function should take as input `N`, the number of iterations to perform and `lamb`, the mesh ratio. The function should return the real-valued RMSE.<jupyter_code>dx   = 0.1
l    = -3
r    = 3
def error(N, lamb):    
    # YOUR CODE HERE
    raise NotImplementedError()
# Run this test cell to check your code
# Do not delete this cell
# 1 mark
# Unit test
lamb = 0.99
N    = 15
tans = 0.007472418804559846
sans = error(N, lamb)
nt.assert_array_almost_equal(tans, sans)
print('Test case passed!!!')
# Hidden test
# No output will be produced
# 3 marks<jupyter_output><empty_output><jupyter_text>Question 4 - 10 MarksConsider the PDE given by:$$v_t = D\left( v_{xx} + v_{yy}\right), $$$$v(-1, y, t) = v(1, y, t) = v(x, -1, t) = v(x, 1, t) = 0, v(x, y, 0) = e^{-(x^2 + y^2)}  $$Solve the PDE given above using an implicit scheme (i.e. BTCS). Your function should take as inputs `dx, dy, dt, D, N`, the $x$, $y$, $t$ timsteps, the diffusive constant $D$ and the number of iterations to perform $N$. The function should return a 3-dimensional array containing the 2D heatmap of each iteration, with $N + 1$ timesteps (i.e. include the initial condition).<jupyter_code>def heat2D_matrix_eq(dx, dy, dt, D, N):
    # YOUR CODE HERE
    raise NotImplementedError()

# Run this test cell to check your code
# Do not delete this cell
# 3 marks
# Unit test

dx = 0.1
dy = 0.1
dt = 0.1
D  = 1
N  = 10
test = heat2D_matrix_eq(dx, dy, dt, D, N)[:, :, -1]
tans = np.array([[0.       , 0.       , 0.       , 0.       , 0.       , 0.       ,
        0.       , 0.       , 0.       , 0.       , 0.       , 0.       ,
        0.       , 0.       , 0.       , 0.       , 0.       , 0.       ,
        0.       , 0.       , 0.       ],
       [0.       , 0.0005143, 0.0010158, 0.0014924, 0.0019322, 0.0023244,
        0.0026593, 0.0029288, 0.0031261, 0.0032465, 0.003287 , 0.0032465,
        0.0031261, 0.0029288, 0.0026593, 0.0023244, 0.0019322, 0.0014924,
        0.0010158, 0.0005143, 0.       ],
       [0.       , 0.0010158, 0.0020067, 0.002948 , 0.0038168, 0.0045915,
        0.0052531, 0.0057854, 0.0061753, 0.0064131, 0.006493 , 0.0064131,
        0.0061753, 0.0057854, 0.0052531, 0.0045915, 0.0038168, 0.002948 ,
        0.0020067, 0.0010158, 0.       ],
       [0.       , 0.0014924, 0.002948 , 0.004331 , 0.0056073, 0.0067455,
        0.0077175, 0.0084995, 0.0090722, 0.0094216, 0.009539 , 0.0094216,
        0.0090722, 0.0084995, 0.0077175, 0.0067455, 0.0056073, 0.004331 ,
        0.002948 , 0.0014924, 0.       ],
       [0.       , 0.0019322, 0.0038168, 0.0056073, 0.0072597, 0.0087333,
        0.0099918, 0.0110042, 0.0117457, 0.012198 , 0.01235  , 0.012198 ,
        0.0117457, 0.0110042, 0.0099918, 0.0087333, 0.0072597, 0.0056073,
        0.0038168, 0.0019322, 0.       ],
       [0.       , 0.0023244, 0.0045915, 0.0067455, 0.0087333, 0.010506 ,
        0.0120199, 0.0132379, 0.0141299, 0.014674 , 0.0148569, 0.014674 ,
        0.0141299, 0.0132379, 0.0120199, 0.010506 , 0.0087333, 0.0067455,
        0.0045915, 0.0023244, 0.       ],
       [0.       , 0.0026593, 0.0052531, 0.0077175, 0.0099918, 0.0120199,
        0.013752 , 0.0151455, 0.016166 , 0.0167886, 0.0169978, 0.0167886,
        0.016166 , 0.0151455, 0.013752 , 0.0120199, 0.0099918, 0.0077175,
        0.0052531, 0.0026593, 0.       ],
       [0.       , 0.0029288, 0.0057854, 0.0084995, 0.0110042, 0.0132379,
        0.0151455, 0.0166801, 0.0178041, 0.0184897, 0.0187201, 0.0184897,
        0.0178041, 0.0166801, 0.0151455, 0.0132379, 0.0110042, 0.0084995,
        0.0057854, 0.0029288, 0.       ],
       [0.       , 0.0031261, 0.0061753, 0.0090722, 0.0117457, 0.0141299,
        0.016166 , 0.0178041, 0.0190038, 0.0197356, 0.0199815, 0.0197356,
        0.0190038, 0.0178041, 0.016166 , 0.0141299, 0.0117457, 0.0090722,
        0.0061753, 0.0031261, 0.       ],
       [0.       , 0.0032465, 0.0064131, 0.0094216, 0.012198 , 0.014674 ,
        0.0167886, 0.0184897, 0.0197356, 0.0204956, 0.020751 , 0.0204956,
        0.0197356, 0.0184897, 0.0167886, 0.014674 , 0.012198 , 0.0094216,
        0.0064131, 0.0032465, 0.       ],
       [0.       , 0.003287 , 0.006493 , 0.009539 , 0.01235  , 0.0148569,
        0.0169978, 0.0187201, 0.0199815, 0.020751 , 0.0210096, 0.020751 ,
        0.0199815, 0.0187201, 0.0169978, 0.0148569, 0.01235  , 0.009539 ,
        0.006493 , 0.003287 , 0.       ],
       [0.       , 0.0032465, 0.0064131, 0.0094216, 0.012198 , 0.014674 ,
        0.0167886, 0.0184897, 0.0197356, 0.0204956, 0.020751 , 0.0204956,
        0.0197356, 0.0184897, 0.0167886, 0.014674 , 0.012198 , 0.0094216,
        0.0064131, 0.0032465, 0.       ],
       [0.       , 0.0031261, 0.0061753, 0.0090722, 0.0117457, 0.0141299,
        0.016166 , 0.0178041, 0.0190038, 0.0197356, 0.0199815, 0.0197356,
        0.0190038, 0.0178041, 0.016166 , 0.0141299, 0.0117457, 0.0090722,
        0.0061753, 0.0031261, 0.       ],
       [0.       , 0.0029288, 0.0057854, 0.0084995, 0.0110042, 0.0132379,
        0.0151455, 0.0166801, 0.0178041, 0.0184897, 0.0187201, 0.0184897,
        0.0178041, 0.0166801, 0.0151455, 0.0132379, 0.0110042, 0.0084995,
        0.0057854, 0.0029288, 0.       ],
       [0.       , 0.0026593, 0.0052531, 0.0077175, 0.0099918, 0.0120199,
        0.013752 , 0.0151455, 0.016166 , 0.0167886, 0.0169978, 0.0167886,
        0.016166 , 0.0151455, 0.013752 , 0.0120199, 0.0099918, 0.0077175,
        0.0052531, 0.0026593, 0.       ],
       [0.       , 0.0023244, 0.0045915, 0.0067455, 0.0087333, 0.010506 ,
        0.0120199, 0.0132379, 0.0141299, 0.014674 , 0.0148569, 0.014674 ,
        0.0141299, 0.0132379, 0.0120199, 0.010506 , 0.0087333, 0.0067455,
        0.0045915, 0.0023244, 0.       ],
       [0.       , 0.0019322, 0.0038168, 0.0056073, 0.0072597, 0.0087333,
        0.0099918, 0.0110042, 0.0117457, 0.012198 , 0.01235  , 0.012198 ,
        0.0117457, 0.0110042, 0.0099918, 0.0087333, 0.0072597, 0.0056073,
        0.0038168, 0.0019322, 0.       ],
       [0.       , 0.0014924, 0.002948 , 0.004331 , 0.0056073, 0.0067455,
        0.0077175, 0.0084995, 0.0090722, 0.0094216, 0.009539 , 0.0094216,
        0.0090722, 0.0084995, 0.0077175, 0.0067455, 0.0056073, 0.004331 ,
        0.002948 , 0.0014924, 0.       ],
       [0.       , 0.0010158, 0.0020067, 0.002948 , 0.0038168, 0.0045915,
        0.0052531, 0.0057854, 0.0061753, 0.0064131, 0.006493 , 0.0064131,
        0.0061753, 0.0057854, 0.0052531, 0.0045915, 0.0038168, 0.002948 ,
        0.0020067, 0.0010158, 0.       ],
       [0.       , 0.0005143, 0.0010158, 0.0014924, 0.0019322, 0.0023244,
        0.0026593, 0.0029288, 0.0031261, 0.0032465, 0.003287 , 0.0032465,
        0.0031261, 0.0029288, 0.0026593, 0.0023244, 0.0019322, 0.0014924,
        0.0010158, 0.0005143, 0.       ],
       [0.       , 0.       , 0.       , 0.       , 0.       , 0.       ,
        0.       , 0.       , 0.       , 0.       , 0.       , 0.       ,
        0.       , 0.       , 0.       , 0.       , 0.       , 0.       ,
        0.       , 0.       , 0.       ]])

nt.assert_almost_equal(test, tans)
print('Test case passed!!!')
# Hidden test
# No output will be produced
# 9 marks<jupyter_output><empty_output><jupyter_text>Question 5 - BonusContinuing with Question 3. Make a simulation/animation of your results.<jupyter_code># YOUR CODE HERE
raise NotImplementedError()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1499030799052337, 'fraction_numerical': 0.2907602842989446, 'mean_word_length': 2.135974786132373, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '102904', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 8208, 'n_tokens_neox': 5635, 'n_words': 1450, 'path': '2019/release/Test2/test2.ipynb', 'repo_name': 'mattw89/APPM4057-Computational-Differential-Equations', 'size': '13929'}