-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.py
43 lines (35 loc) · 857 Bytes
/
test.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
# -*- coding: utf-8 -*-
from rk4 import *
from math import e
def main():
# For 1st Order ODE
def f(t, y):
return 2 * t - 3 * y + 1
y = firstorder(f)
ti = 1.0
yi = 5.0
t = 1.5
h = 0.01
r = y.solve(ti, yi, t, h)
print('ODE first order solution: y\' = 2t - 3y + 1')
print('y({}) = {}'.format(t, r))
print('Close window...')
y.graph('r--', label="Function y")
# For 2nd Order ODE
def g(u):
return u
def f(t, y, u):
return - t * u - y
rk = secondorder(f, g)
ti = 0.0
yi = 1.0
ui = 2.0
t = 0.2
y, u = rk.solve(ti, yi, ui, t, h)
print('ODE second order solution: y\'\' + ty\' + y = 0')
print('y({}) = {}'.format(t, y))
print('y\'({}) = {}'.format(t, u))
print('Close window...')
rk.graph()
if __name__ == '__main__':
main()