-
Notifications
You must be signed in to change notification settings - Fork 72
/
euler_method.py
54 lines (39 loc) · 931 Bytes
/
euler_method.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#! /usr/bin/env python
"""
@author Rafiul
@require numpy
"""
from numpy import arange
from numpy import array
"""
Function of euler method
@param f = function
@param xr = x range or x interval
@param y0 = x0 corresponding y0
@param h = constant difference between 2 corresponding x
@return x array and y array
"""
def euler(f, xr, y0, h):
# create an empty list of y values
y = []
# append y0 into y as fitst value
y.append(y0)
# x array using constant difference in interval
x = arange(xr[0], xr[1]+h, h)
# loop through for xi -> yi
for i in range(1, len(x)):
# append yi into y list
y.append(y[i-1]+h*f(y[i-1], x[i-1]))
# return x and y values in numpy array format
return x, array(y)
# driver
def main():
# define the function
f = lambda x, y : x+y
# call euler with params
ans = euler(f, [0,1], 1, 0.1)
# print x and y array
print(ans[0])
print(ans[1])
if __name__ == '__main__':
main()