forked from agezerlis/NumericalMethodsPhysicsWithPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathivp_one.py
More file actions
38 lines (31 loc) · 871 Bytes
/
ivp_one.py
File metadata and controls
38 lines (31 loc) · 871 Bytes
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
# Author: Alex Gezerlis
# Numerical Methods in Physics with Python (CUP, 2020)
import numpy as np
def f(x,y):
return - (30/(1-x**2)) + ((2*x)/(1-x**2))*y - y**2
def euler(f,a,b,n,yinit):
h = (b-a)/(n-1)
xs = a + np.arange(n)*h
ys = np.zeros(n)
y = yinit
for j,x in enumerate(xs):
ys[j] = y
y += h*f(x, y)
return xs, ys
def rk4(f,a,b,n,yinit):
h = (b-a)/(n-1)
xs = a + np.arange(n)*h
ys = np.zeros(n)
y = yinit
for j,x in enumerate(xs):
ys[j] = y
k0 = h*f(x, y)
k1 = h*f(x+h/2, y+k0/2)
k2 = h*f(x+h/2, y+k1/2)
k3 = h*f(x+h, y+k2)
y += (k0 + 2*k1 + 2*k2 + k3)/6
return xs, ys
if __name__ == '__main__':
a, b, n, yinit = 0.05, 0.49, 12, 19.53
xs, ys = euler(f,a,b,n,yinit); print(ys)
xs, ys = rk4(f,a,b,n,yinit); print(ys)