Let's define the problem using sympy
.
import sympy as sp
# variables
x, t = sp.symbols('x t')
# constants
omega, m, k, A = sp.symbols('omega m k A')
# analytical solution of equation of motion
x = A * sp.cos(omega * t)
x_dot = sp.diff(x, t)
# kinetic and elastic energy
T = 1/2 * m * x_dot**2
V = 1/2 * k * x**2
x
x_dot
T
V
Let's do some plots.
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('bmh')
Tnum = sp.lambdify(t, T.subs({A: 1, m: 1, omega:1}))
Vnum = sp.lambdify(t, V.subs({A: 1, k: 1, omega:1}))
tt = np.linspace(0, 3)
fig, ax = plt.subplots()
ax.plot(tt, Vnum(tt), label='V')
ax.plot(tt, Tnum(tt), label='T')
ax.plot(tt, Tnum(tt) + Vnum(tt), label='T+V')
ax.legend()
ax.set_xlabel('t');
fig, ax = plt.subplots()
ax.plot(Tnum(tt), Vnum(tt), '-', label='V')
ax.plot(Tnum(tt), Tnum(tt), '-', label='T')
ax.legend()
ax.set_xlabel('T');