-
Notifications
You must be signed in to change notification settings - Fork 0
/
euler_method.py
35 lines (30 loc) · 1.05 KB
/
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
from decimal import Decimal
from math import exp
class EulerMethod:
def __init__(self, f, g, start, end, iv):
self.f = f
self.g = g
self.start = start
self.end = end
self.iv = iv
def approximate(self, inc):
curY = self.iv
curT = self.start
while curT <= self.end:
gEval = self.g(curT)
print(f"y({curT}) = {curY}; Φ({curT}) = {gEval}; Difference: {gEval - curY}")
curY = curY + self.f(curT, curY) * (inc)
curT += inc
ranges = [Decimal(i) for i in (".1", ".05", ".025")]
print("For y' = 3 + t - y")
f = lambda t, y: 3 + t - y
g = lambda t: t + Decimal('2') - (-t).exp()
for r in ranges:
print("Increment of", r)
EulerMethod(f, g, Decimal('0'), Decimal('0.4'), Decimal('1')).approximate(r)
print("\nFor y' = 0.5 - t + 2y")
f = lambda t, y: Decimal('0.5') - t + 2 * y
g = lambda t: Decimal('0.5') * t + Decimal.exp(2 * t)
for r in ranges:
print("Increment of", r)
EulerMethod(f, g, Decimal('0'), Decimal('0.4'), Decimal('1')).approximate(r)