rename symbol q to symbol p
[liba.git] / python / test / trajpoly3.py
blob9ffa7771b08e7ea6c3c3c03f01319d7bde950696
1 #!/usr/bin/env python
2 import os, sys
4 sys.path.insert(0, os.getcwd())
5 prefix = os.path.join(sys.path[0], "build")
6 if not os.path.exists(prefix):
7 os.mkdir(prefix)
8 try:
9 import liba as a
10 import numpy as np
11 import matplotlib.pyplot as plt
12 except Exception as e:
13 print(e)
14 exit()
16 t = (0, 1)
17 p = (0, 1)
18 v = (0, 1)
19 data = np.arange(t[0], t[1], 0.01)
20 traj = a.trajpoly3(t[1] - t[0], p[0], p[1], v[0], v[1])
21 text_p = "p="
22 if traj.p[0]:
23 text_p += "%g" % (traj.p[0])
24 if traj.p[1]:
25 text_p += "%+g%s" % (traj.p[1], "t")
26 if traj.p[2]:
27 text_p += "%+g%s" % (traj.p[2], "t^2")
28 if traj.p[3]:
29 text_p += "%+g%s" % (traj.p[3], "t^3")
30 text_p = text_p.replace("=+", "=")
31 text_v = "v="
32 if traj.v[0]:
33 text_v += "%g" % (traj.v[0])
34 if traj.v[1]:
35 text_v += "%+g%s" % (traj.v[1], "t")
36 if traj.v[2]:
37 text_v += "%+g%s" % (traj.v[2], "t^2")
38 text_v = text_v.replace("=+", "=")
39 text_a = "a="
40 if traj.a[0]:
41 text_a += "%g" % (traj.a[0])
42 if traj.a[1]:
43 text_a += "%+g%s" % (traj.a[1], "t")
44 text_a = text_a.replace("=+", "=")
45 plt.figure("3 polynomial trajectory")
47 plt.subplot(311)
48 plt.title("cubic polynomial trajectory")
49 plt.ylabel("Position")
50 plt.plot(data, traj.pos(data), "r-", label=text_p)
51 plt.legend()
53 plt.subplot(312)
54 plt.ylabel("Velocity")
55 plt.plot(data, traj.vel(data), "b-", label=text_v)
56 plt.legend()
58 plt.subplot(313)
59 plt.ylabel("Acceleration")
60 plt.plot(data, traj.acc(data), "g-", label=text_a)
61 plt.legend()
63 plt.xlabel("t")
64 plt.savefig(os.path.join(prefix, "trajectory_polynomial_3.png"))