<s>[INST] Solving differential equation with step function without using Laplace Transforms. Suppose we have the differential equation:
$$ \ddot{y} + y = H(x - \pi) - H(x - 2\pi) $$
where $ H(x)$ is the Heaviside step function with initial conditions $ y(0) = \dot{y}(0) = 0 $ as initial conditions, and $ y(x) $ and $ \dot{y}(x) $ are continuous everywhere. I know how to solve this using Laplace Transforms, but I was curious if there are any alternative approaches for solving this and differential equations involving step functions in general.
 [/INST] Without the Laplace transform the solution is as follows.
Using
$$ H(t - \pi) - H(t - 2 \pi) = \begin{cases} 0 & t < \pi \\ 1 & \pi < t < 2 \pi \\ 0 & t > 2 \pi \end{cases} $$
then
$$\begin{cases} y_{1}^{''} + a^2 \, y_{1} \\ y_{2}^{''} + a^2 \, y_{2} \\ y_{3}^{''} + a^2 \, y_{3} \end{cases} = \begin{cases} 0 & t < \pi \\ 1 & \pi < t < 2 \pi \\ 0 & t > 2 \pi \end{cases}. $$
This gives the general solutions
$$ y(t) = \begin{cases} y_{1} =  A_{0} \, \cos(a t) + A_{1} \, \sin(a t) & t < \pi \\ y_{2} =  B_{0} \, \cos(a t) + B_{1} \, \sin(a t) + \frac{1}{a^2} & \pi < t < 2 \pi \\ y_{3} =  C_{0} \, \cos(a t) + C_{1} \, \sin(a t) & t > 2 \pi \end{cases}. $$
The given boundary condition, $y(0) = y^{'}(0) = 0$ apply to $y_{1}$ and leads to
$$ y(t) = \begin{cases} y_{1} = 0 & t < \pi \\ y_{2} =  B_{0} \, \cos(a t) + B_{1} \, \sin(a t) + \frac{1}{a^2} & \pi < t < 2 \pi \\ y_{3} =  C_{0} \, \cos(a t) + C_{1} \, \sin(a t) & t > 2 \pi \end{cases}. $$
Now using $y_{1}(\pi) = y_{2}(\pi)$ and $y^{'}_{1}(\pi) = y_{2}^{'}(\pi)$ then $0 = B_{0} \, \cos(a \pi) + B_{1} \, \sin(a \pi) + \frac{1}{a^2}$ and $0 = -a \, (B_{0} \sin(a \pi) - B_{1} \, \cos(a \pi) )$ which gives
$$ y(t) = \begin{cases} y_{1} = 0 & t < \pi \\ y_{2} = \frac{2}{a^2} \, \cos^{2}\left(\frac{a t - a \pi}{2}\right) & \pi < t < 2 \pi \\ y_{3} =  C_{0} \, \cos(a t) + C_{1} \, \sin(a t) & t > 2 \pi \end{cases}. $$
The remaining conditions $y_{2}(2 \pi) = y_{3}(2 \pi)$ and $y_{2}^{'}(2 \pi) = y_{3}^{'}(2 \pi)$ lead to the final solution of
$$ y(t) = \begin{cases} 0 & t < \pi \\ \frac{2}{a^2} \, \cos^{2}\left(\frac{a t - a \pi}{2}\right) & \pi < t < 2 \pi \\ \frac{1}{a^2} \, \left( \cos(a t - a \pi) + \cos(a t - 2 a \pi) \right) & t > 2 \pi \end{cases}. $$
This can also be seen in the form
$$ y(t) = \frac{2}{a^2} \, \cos^{2}\left(\frac{a t - a \pi}{2}\right) \, H(t - \pi) - \frac{2}{a^2} \, \sin^{2}\left(\frac{a t - 2 a \pi}{2}\right) \, H(t - 2 \pi). $$
By using the Laplace transform the solution is seen as follows.
Fist note that
$$ L\left\{ H(t - a) \right\} = \int_{0}^{\infty} e^{-s t} \, H(t -a) \, dt = \int_{a}^{\infty} e^{-s t} \, dt = \frac{e^{- a s}}{s}. $$
Now,
\begin{align}
L\{ y^{''} + a^2 \, y \} &= L\left\{ H(t - \pi) - H(t - 2 \pi) \right\} \\
(s^2 + a^2) \, \overline{y} - s \, y(0) - y^{'}(0) &= \frac{e^{-\pi s}}{s} - \frac{e^{-2 \pi s}}{s} \\
\overline{y} &= \frac{e^{- pi s}}{s \, (s^2 + a^2)} - \frac{e^{- pi s}}{s \, (s^2 + a^2)} + \frac{s}{s^2 + a^2} \, y(0) + \frac{y^{'}(0)}{s^2 + a^2}. 
\end{align}
Since
$$ \frac{1}{s \, (s^2 + a^2)} = \frac{1}{a^2 \, s} - \frac{1}{a^2 \, (s^2 + a^2)} $$
then
$$ \overline{y} = \frac{1}{a^2} \left( \frac{e^{- \pi s}}{s} - \frac{s \, e^{- \pi s}}{s^2 + a^2} - \frac{e^{- 2 \pi s}}{s} + \frac{s \, e^{- 2 \pi s}}{s^2 + a^2} \right) + \frac{s}{s^2 + a^2} \, y(0) + \frac{y^{'}(0)}{s^2 + a^2}. $$
The Laplace inversion gives
\begin{align}
y(t) &= \frac{1}{a^2} \, \left( (1+\cos(a t - a \pi)) \, H(t - \pi) - (1 - \cos(a t - 2 a \pi)) \, H(t - 2 \pi) \right) \\
& \hspace{10mm} + y(0) \, \cos ( a t) + \frac{y^{'}(0)}{a} \, \sin(a t)
\end{align}
or
\begin{align}
y(t) &= \frac{2}{a^2} \, \cos^{2}\left(\frac{a t - a \pi}{2}\right) \, H(t - \pi) - \frac{2}{a^2} \, \sin^{2}\left(\frac{a t - 2 a \pi}{2}\right) \, H(t - 2 \pi)  \\
& \hspace{15mm} + y(0) \, \cos ( a t) + \frac{y^{'}(0)}{a} \, \sin(a t).
\end{align}
With the conditions $y(0) = y^{'}(0) = 0$ and making use of the Heaviside step function definition then the solution can be seen in regions as:
$$ y(t) = \begin{cases} 0 & t<\pi \\ \frac{1}{a^2} & t = \pi \\ \frac{2}{a^2} \, \cos^{2}\left(\frac{a t - a \pi}{2}\right) & \pi < t  < 2 \pi \\ \frac{2}{a^2} \, \cos^{2}\left(\frac{a \pi}{2}\right) & t = 2 \pi \\ \frac{1}{a^2} \, \left( \cos(a t - a \pi) + \cos(a t - 2 a \pi) \right) & t > 2 \pi \end{cases}. $$
</s>