# Tutorial and example about approx. ODE with Euler Method


```python
import numpy as np
import math
```

- __define__ $f=(t,y)$
- __input__ $t_{0}$ and $y_{0}$
- __input__ step size, $h$ and number of steps $n$
- __for__ $j$ from 1 to $n$:
    - $m=f(t_{0},y_{0})$
    - $y_{1}$ = $y_{0}+h*m$
    - $t_{1}=t_{0}+h$
    - Print $t_{1}$ and $y_{1}$
    - $t_{0}=t_{1}$
    - $y_{0}=y_{1}$



```python
def euler(givenfunction,t0,y0,h,n,i):
    m=givenfunction(t0,y0)
    y1=y0+h*m
    t1=t0+h
    i=i+1
    if i==n:
        print('t= %.2f'%t1,'approx= %.6f'%y1)
        times.append(round(t1,2))
        results.append(round(y1,5))
        return (t1,y1)
    else:
        print('t= %.2f'%t1,'approx= %.6f'%y1)
        times.append(round(t1,2))
        results.append(round(y1,5))
        return euler(givenfunction,t1,y1,h,n,i)
    
```

For the given equation and initial condition
\begin{equation}
y^{\prime}+2 y=2-\mathbf{e}^{-4 t} \quad y(0)=1
\end{equation}
Pick a $h$ and values of $t$,


```python
def func(t,y):
    y_prime = 2-math.exp(-4*t)-2*y
    return y_prime
```


```python
h=0.1
t0=0
y0=1
n=50
times=[t0]
results=[y0]

```


```python
x=euler(func,t0,y0,h,n,i=1)
```

    t= 0.10 approx= 0.900000
    t= 0.20 approx= 0.852968
    t= 0.30 approx= 0.837441
    t= 0.40 approx= 0.839834
    t= 0.50 approx= 0.851677
    t= 0.60 approx= 0.867808
    t= 0.70 approx= 0.885175
    t= 0.80 approx= 0.902059
    t= 0.90 approx= 0.917571
    t= 1.00 approx= 0.931324
    t= 1.10 approx= 0.943228
    t= 1.20 approx= 0.953355
    t= 1.30 approx= 0.961861
    t= 1.40 approx= 0.968937
    t= 1.50 approx= 0.974780
    t= 1.60 approx= 0.979576
    t= 1.70 approx= 0.983495
    t= 1.80 approx= 0.986684
    t= 1.90 approx= 0.989273
    t= 2.00 approx= 0.991368
    t= 2.10 approx= 0.993061
    t= 2.20 approx= 0.994426
    t= 2.30 approx= 0.995526
    t= 2.40 approx= 0.996411
    t= 2.50 approx= 0.997122
    t= 2.60 approx= 0.997693
    t= 2.70 approx= 0.998151
    t= 2.80 approx= 0.998519
    t= 2.90 approx= 0.998814
    t= 3.00 approx= 0.999050
    t= 3.10 approx= 0.999239
    t= 3.20 approx= 0.999391
    t= 3.30 approx= 0.999513
    t= 3.40 approx= 0.999610
    t= 3.50 approx= 0.999688
    t= 3.60 approx= 0.999750
    t= 3.70 approx= 0.999800
    t= 3.80 approx= 0.999840
    t= 3.90 approx= 0.999872
    t= 4.00 approx= 0.999898
    t= 4.10 approx= 0.999918
    t= 4.20 approx= 0.999934
    t= 4.30 approx= 0.999948
    t= 4.40 approx= 0.999958
    t= 4.50 approx= 0.999966
    t= 4.60 approx= 0.999973
    t= 4.70 approx= 0.999979
    t= 4.80 approx= 0.999983
    t= 4.90 approx= 0.999986


Analytical solution
\begin{equation}
y(t)=1+\frac{1}{2} \mathbf{e}^{-4 t}-\frac{1}{2} \mathbf{e}^{-2 t}
\end{equation}


```python
def solution(t):
    y=1+0.5*math.exp(-4*t)-0.5*math.exp(-2*t)
    return y

```


```python
a_results=[]
time_list=np.arange(0,0.6,0.01)
for t in time_list:
    a_results.append(solution(t))
    
    
```


```python

```


```python
import numpy as np
import matplotlib.pyplot as plt

def f(t):
    y=1+0.5*np.exp(-4*t)-0.5*np.exp(-2*t)
    return y

t2 = np.arange(0.0, 6.0, 0.02)

plt.figure(1)
plt.plot(times,results,'bo',t2, f(t2), 'k')
plt.show()

```

Blue dots are numerical approximations with Euler Method


```python

```

8b396639c1eebb43c3cb616fdac6cee1ca0f17d4
ipynb
Jupyter Notebook
euler_method_ode.ipynb
bkoyuncu/euler_ODE
d639bc3d14da1c215f4d24ea3a12e7a1727519e4
MIT
euler_method_ode.ipynb
bkoyuncu/euler_ODE
d639bc3d14da1c215f4d24ea3a12e7a1727519e4
MIT
euler_method_ode.ipynb
bkoyuncu/euler_ODE
d639bc3d14da1c215f4d24ea3a12e7a1727519e4
MIT
Qwen/Qwen-72B
1. YES
2. YES