<jupyter_start><jupyter_text>Forward Euler Method ExampleHere we'll look at a simple implementation of the forward Euler method applied to a 1-dimensional function in order to see some if its properties. Our example will be the initial value problem defined as:$$ f^\prime(t) = -0.1f(t), \quad f(0) = 10 $$Of course, we can integrate this function to see that the solution is$$ f(t) = 10e^{-0.1t} $$We'll use this to compare the numerical solutions from the Euler method with the exact solution at $t=50$. The forward Euler method is:$$ f(t + \Delta t) = f(t) + f^\prime(t)\,\Delta t $$<jupyter_code>import numpy as np
from matplotlib import pyplot as plt

#f' = -0.1*f
def euler_step(f,dt):
    return f - 0.1*f*dt

##play with the step size number, observe how f changes
dt = 1
tf = 50.

f0 = 10.
t = 0.

#put first value in array
f = [f0]
tv = [t]

while t + dt <= tf:
    f.append(euler_step(f[-1],dt))
    t += dt
    tv.append(t)

f
#plot the exact solution and the computed one

tt = np.linspace(0,50,1000)

fig,(ax1,ax2) = plt.subplots(1,2,figsize=(15,7))

for ax in [ax1,ax2]:
    for dt in [5,1,0.2]:
        f0 = 10.
        t = 0.

        #put first value in array
        f = [f0]
        tv = [t]

        while t + dt <= tf:
            f.append(euler_step(f[-1],dt))
            t += dt
            tv.append(t)

        error = f[-1] - 10*np.exp(-0.1*50)
        ax.plot(tv,f,label=f'dt = {dt:.1f}, error = {error:.2e}',marker='o')

    ax.plot(tt,10*np.exp(-0.1*tt),'k-',lw=2,label=r'$f(t) = 10e^{-0.1t}$')
    ax.set_ylabel('f(t)')
    ax.set_xlabel('t')
    ax.legend()
ax2.set_yscale('log')
#fig.savefig('euler.png',bbox_inches='tight',transparent=True)<jupyter_output><empty_output><jupyter_text>When the step size is too large, sometimes there can be catastrophic error. Consider this example, where$$ f^\prime(t) = -f(t) $$<jupyter_code>#f' = -f
def euler_step_2(f,dt):
    return f -f*dt

tt = np.linspace(0,50,1000)

fig,ax = plt.subplots(figsize=(12,10))


for dt in [0.5,2,2.1]:
    f0 = 10.
    t = 0.

    #put first value in array
    f = [f0]
    tv = [t]

    while t + dt <= tf:
        f.append(euler_step_2(f[-1],dt))
        t += dt
        tv.append(t)
    
    
    error = f[-1] - 10*np.exp(-50)
    ax.plot(tv,f,label=f'dt = {dt:.1f}, error = {error:.2e}',marker='o')

ax.plot(tt,10*np.exp(-tt),'k-',lw=2,label='Exact')
ax.set_ylabel('f(t)')
ax.set_xlabel('t')
ax.legend()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17553191489361702, 'fraction_numerical': 0.0425531914893617, 'mean_word_length': 2.9950980392156863, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '555682', 'license': 'MIT', 'n_tokens_mistral': 1049, 'n_tokens_neox': 988, 'n_words': 260, 'path': '_notebooks/week5/euler.ipynb', 'repo_name': 'leeping/che155', 'size': '2444'}