-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvanderpol.py
executable file
·78 lines (61 loc) · 2.49 KB
/
vanderpol.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#! /usr/bin/env python
import os
import matplotlib.pyplot as plt
import numpy as np
from io import StringIO
from subprocess import check_output
from itertools import product
def call(*args, **kwargs):
return check_output(
['env'] + ['%s=%s'%(k,str(v)) for k,v in kwargs.items()] + list(args)
).decode('utf-8')
def numprog(*args, **kwargs):
return np.loadtxt(StringIO(call(*args, **kwargs)))
try:
os.makedirs('graph')
except OSError:
pass
for dt,(eps,tf) in product((0.1,0.01,0.001), ((0,30),(0.1,100),(5,30))):
print("dt=%s, eps=%s" % (dt,eps))
params = dict(dt=dt, eps=eps, p0=0.1, tf=tf)
eul = numprog('bin/euler', **params)
rk4 = numprog('bin/rk4', **params)
octave = numprog('octave', '-q', 'vandersolve.m', **params)
m = min(len(eul), len(rk4), len(octave))
# plot x(t), p(t)
figure = plt.figure()
axes = figure.add_subplot(111)
figure.suptitle(r'$\Delta t=%s$, $\epsilon=%s$' % (dt, eps))
axes.plot(eul[:,0], eul[:,1], label=r'$x_\mathrm{eul}$')
axes.plot(eul[:,0], eul[:,2], label=r'$\dot{x}_\mathrm{eul}$')
axes.plot(rk4[:,0], rk4[:,1], label=r'$x_\mathrm{rk4}$')
axes.plot(rk4[:,0], rk4[:,2], label=r'$\dot{x}_\mathrm{rk4}$')
axes.legend(loc='lower left')
figure.savefig("graph/time-evolution_dt=%s_eps=%s.png"%(dt,eps))
plt.close()
figure = plt.figure()
axes = figure.add_subplot(111)
figure.suptitle(r'$\Delta t=%s$, $\epsilon=%s$' % (dt, eps))
axes.plot(eul[:m,0], octave[:,0], label=r'$x_\mathrm{lsode}$')
axes.plot(eul[:m,0], octave[:,1], label=r'$\dot{x}_\mathrm{lsode}$')
axes.legend(loc='lower left')
figure.savefig("graph/time-evolution-lsode_dt=%s_eps=%s.png"%(dt,eps))
plt.close()
# error plot:
figure = plt.figure()
axes = figure.add_subplot(111)
figure.suptitle(r'$\Delta t=%s$, $\epsilon=%s$' % (dt, eps))
err_eul = abs(eul[:m,1] - octave[:m,0])
err_rk4 = abs(rk4[:m,1] - octave[:m,0])
axes.plot(eul[:m,0], err_eul, label=r'$\mathrm{err}_\mathrm{eul}$')
axes.plot(eul[:m,0], err_rk4, label=r'$\mathrm{err}_\mathrm{rk4}$')
axes.legend(loc='lower left')
figure.savefig("graph/error_dt=%s_eps=%s.png"%(dt,eps))
plt.close()
# phase portrait:
figure = plt.figure()
axes = figure.add_subplot(111)
figure.suptitle(r'Phase portrait for $\Delta t=%s$, $\epsilon=%s$' % (dt, eps))
axes.plot(rk4[:,1], rk4[:,2], label=r'$x_\mathrm{eul}$')
figure.savefig("graph/phase-portrait_dt=%s_eps=%s.png"%(dt,eps))
plt.close()