<jupyter_start><jupyter_text>**Problem 1**1. Solve the following ODE using a first-order Forward Euler approximation. Write out the matrix form of the solution. Find the eigenvalues of the matrix. What do you notice about the eigenvalues? Try varying values of $\Delta t$. When does the solution become unstable? Why?$$\frac{dy}{dt} = -30y, y(0) = 1 $$2. Repeat 1. using a second-, third-, fourth-, fifth-, and sixth- order approximation for the derivative. What do you notice about the behavior of higher order approximations as you vary $\Delta t$? Do they become unstable?3. Plot the relative accuracy of y(0.3) for each case in 1 and 2 as a function of $\Delta t$, as well as the time of execution as a function of $\Delta t$. At what point does using a higher order approximation lose its value, in terms of both accuracy and computational cost?<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
# Define the discretization
N = 40 # Number of steps
t = np.linspace(0, 1, N+1) # Define time vector
dt = t[1] # Define delta t
y0 = 1 # Define initial condition

b = 0*t # Define b vector
b[0] = y0 # Set initial condition
A = np.eye(N=N+1)/dt + (30 - 1/dt)*np.eye(N=N+1, k=-1)
A[0,0] = 1

# Second order A matrix
A2 = 2*np.eye(N=N+1, k=0)/dt + (30 - 1.5/dt)*np.eye(N=N+1, k=-1) + (-0.5/dt)*np.eye(N=N+1, k=1)
#A[0,0] = 1
A2[0:2,:] = A[0:2,:]

y1 = np.matmul(np.linalg.inv(A), b)
y2 = np.matmul(np.linalg.inv(A2), b)
A2 = 2*np.eye(N=N+1, k=0)/dt + (30 - 1.5/dt)*np.eye(N=N+1, k=-1) + (-0.5/dt)*np.eye(N=N+1, k=1)
A2[0:2,:] = A[0:2,:]
A2
y = np.exp(-30*t)

fig, ax = plt.subplots(figsize=(6, 4))

ax.semilogy(t, y, label='exact')
ax.semilogy(t, y1, label='1st')
ax.semilogy(t, y2, label='2nd')

ax.legend()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1619047619047619, 'fraction_numerical': 0.048179271708683476, 'mean_word_length': 4.1469740634005765, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '614624', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 744, 'n_tokens_neox': 693, 'n_words': 239, 'path': 'notebooks/HW4_computational_P1.ipynb', 'repo_name': 'uw-cheme512/uw-cheme512.github.io', 'size': '1785'}