# Demo entry 2643442

euler's method

Submitted by anonymous on Sep 07, 2015 at 22:06
Language: Python. Code size: 858 Bytes.

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

#buat batasan 0.0 <= t <= 2

a = 0.0
b = 2.0

# jumlah iterasi/ steps
N = 100

# step-size
h = (b-a)/N

#nilai awal: y(0.0) = 0.5
IV = (0.0,0.5)

# membuat definisi ODE( ordinary differensial equestion)
def f(t,y):
return y - t**2 + 1

#membuat arrays untuk menyimpan t dan y

t = np.arange(a, b+h, h)
w = np.zeros((N+1,))

# setting nilai awal
t[0], w[0] = IV

#apply Euler's method

for i in range(1,N+1):
w[i] = w[i-1] + h * f(t[i-1],w[i-1])

#penyelesaian eksak

def y(t):
return (t+1.0)**2.0-0.5*np.exp(t)

plt.plot(t,w, label='pendekatan')
plt.plot(t,y(t), label='exact')
plt.title("contoh Euler's Method, N="+str(N))

plt.xlabel('t')
plt.ylabel('y(t)')

plt.legend(loc=4)
plt.grid()
plt.savefig('euler_ex.png', fmt='PNG', dpi=100)
plt.show()
```

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.