<jupyter_start><jupyter_text>Euler MethodThe Euler method works by assuming the gradient of a solution, $y'=f(t_n,y_n)$ can be approximated by a linear gradient between $y_n$ and $y_{n+1}$, i.e., $$ \frac{y_{n+1}-y_n}{\Delta t} = f(t_n,y_n)$$so $y_{n+1}$ can be expressed as,$$ y_{n+1} = y_n+\Delta t \; f(t_n,y_n)$$The example below shows the Euler method solution for $y'=y+t$, change the slider to increase the number of solution points (thereby decreasing $\Delta t$) and watch what happens to the root-mean-square (RMS) error of the numerical solution compared to the analytic solution.<jupyter_code>%matplotlib inline

# import packages
from matplotlib import pyplot as plt
import numpy as np
import time
from scipy import integrate
from ipywidgets import interact

# define ODE
def ode(y, t):
    return y+t

# define plot function
def plotter(n):
      
    # dt, time
    dt = 1/(n-1);
    t = np.linspace(0, 1, n)

    # solution loop
    y = np.zeros([n])
    y_a = y;
    y[0] = 0;
    err = 0;
    
    for i in range(1,n):
        f_e = ode(y[i-1],t[i-1])
        y_e = y[i-1]+dt*f_e
        #y[i] = y[i-1]+dt*(ode(y_e,t[i])+f_e)/2
        y[i] = y[i-1]+dt*ode(y[i-1],t[i-1])
        err = err+(abs(y[i]-(np.exp(t[i])-t[i]-1)))**2
    
    err = (err/n)**0.5;
    
    #analytic solution
    t_a = np.linspace(0,1,101)
    y_a = np.zeros(len(t_a))
    for j in range(1,len(t_a)):
        y_a[j] = np.exp(t_a[j])-t_a[j]-1
        
    f,ax = plt.subplots(1,1, figsize=(14,8))
    
    ax.plot(t_a,y_a, color='b', label='Analytic')
    ax.plot(t,y, 'o',color='r', label='Euler')
    plt.title('RMS error %.4f' %err)
    legend = ax.legend(loc='upper left', shadow=False)
    
    #plt.yticks(np.arange(-1.2, 1.3, 0.4))
    plt.xlabel('t')
    plt.ylabel('y')

    #ax.plot([t_i],y[1,0],marker='o', markersize=3, color="red")
    plt.show() 
    
interact(plotter, n =(2,21,1))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18248175182481752, 'fraction_numerical': 0.026068821689259645, 'mean_word_length': 3.1357758620689653, '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': '489605', 'license': 'MIT', 'n_tokens_mistral': 837, 'n_tokens_neox': 806, 'n_words': 188, 'path': 'engsci211_ode3_euler.ipynb', 'repo_name': 'csim456/notebooks211', 'size': '1918'}