import numpy as np
import sympy
sympy.init_printing()

from sympy.abc import a, t, x, s, g, G, X
x, g, X = sympy.symbols('x, g, X', cls=sympy.Function)


x0 = 4
xd0 = 7

g = sympy.exp(-t)
g

"""
Explanation: Using the Laplace Transform to solvle a differential equation
Consider solving
$$
3 \ddot{x} + 30 \dot{x} + 63 x = 4 \dot{g(t)} + 6 g(t)
$$
where
$$ 
g(t) = 5 e^{-t}
$$
and initial conditions are
$$
x(0) = 4, \dot{x}(0) = 7
$$
End of explanation
"""


sympy.laplace_transform(g, t, s)[0]

"""
Explanation: Lets try the Laplace Transform on the input and check that it works
End of explanation
"""


eoms = sympy.Eq(3*(s**2 * X(s) - s * x0 - xd0) +
               30 * (s * X(s) - x0) +
               63 * X(s), 
               sympy.laplace_transform(4 * sympy.diff(g, t) + 6 * g, t, s)[0])
eoms

"""
Explanation: Now lets define the EOMs
sympy doesn't handle undefined functions in the Laplace transform yet, so we will input the EOMS in the Laplace domain ourselves.
https://github.com/sympy/sympy/issues/7219#issuecomment-154768904
End of explanation
"""


XofS = sympy.solve(eoms, X(s))[0]
sympy.simplify(XofS)

"""
Explanation: Solve for $ X(s)$
End of explanation
"""


sol = sympy.inverse_laplace_transform(XofS, s, t)
sol

"""
Explanation: Now just inverse Laplace to find the solution
End of explanation
"""


sympy.plot(sol, (t, 0.1, 2))

sol.evalf(subs={t: 5})

"""
Explanation: Note
The $ \theta (t) $ is simply sympy's notation for a step function. This answer is only valid for $ t > 0$
We can generate a plot or evaluate
End of explanation
"""


t = sympy.symbols('t', positive=True)
s = sympy.symbols('s', complex=True)
f = 5 * t**2 * sympy.cos(3*t + sympy.pi/4)
f

F = sympy.laplace_transform(5*t**2*sympy.cos(t), t, s, noconds=True)
F

"""
Explanation: A more complicated transformoration
$$
f(t) = 5 t^2 \cos (3 t + \frac{\pi}{4})
$$
End of explanation
"""


