%% Solution Exam 2 (MATH246, Spring 2024)
%% Problem 1
% Characteristic equation r^2+2r+1=0 has solutions r_1=r_2=-1.
% Particular solution for -t is A_0+A_1 t
% Particular solution for -t\cdot e^{-t} is t^2(A_2 +A_3 t)e^{-t} (since -1 is root of multiplicity 2)
% Particular solution for -t\cdot e^{-t}\cdot \sin t is (A_4+A_5 t)e^{-t}\cos t+(B_0 + B_1 t)e^{-t}\sin t
% Here are the actual particular solutions (not asked for in the exam):

clearvars
syms y(t)
syms C1 C2
for f = [-t,-t*exp(-t),-t*exp(-t)*sin(t)]
  f
  sol = dsolve(diff(y,2)+2*diff(y)+y==f);
  particular_sol = expand( subs(sol,{C1,C2},{0,0}) )
end

%% Problem 2
% Characteristic equation r^2-4=0 has solutions r_1=2,\;r_2=-2. Hence Y_1(t)=e^{2t},\;
% Y_2(t) = e^{-2t} and Wronskian
%        W(t) = \det
% \left[ \matrix{Y_1 & Y_2 \cr Y_1\prime & Y_2\prime }\right]
% = -2-2=-4
% Particular solution is Y(t) = u_1 Y_1 + u_2 Y_2 where
% u_1\prime = \frac{-Y_2}W f = \frac14 (e^{-2t }+1), hence u_1 = \frac14(-\frac12 e^{-2t}+t)
% u_2\prime = \frac{Y_1}W f= -\frac14(e^{2t}+e^{4t}), hence u_2 = -\frac14(\frac12 e^{2t}+\frac14 e^{4t})
% yielding
%        Y(t) = u_1 Y_1 + u_2 Y_2
% = -\frac18 +\frac14te^{2t}  -\frac18 -\frac1{16}e^{2t}

clearvars
syms y(t)
syms C1 C2
sol = dsolve(diff(y,2)-4*y==1+exp(2*t));
particular_sol = expand( subs(sol,{C1,C2},{0,0}) )

%% Problem 3
% We have the ODE 2y''(t)+c y'(t) + k y(t) = 2\cos(3t) or y''(t)+\frac{c}2 y'(t) + \frac{k}2 y(t) = \cos(3t).
%% Problem 3(a)
% The natural frequency is \omega_0 = \sqrt{\frac{k}m} , the external frequency is \omega=3. We want \omega_0=\omega, i.e. \frac{k}2=\omega^2=9, so k=18.
% The general solution is y(t) = Y(t) + c_1 Y_1(t) + c_2 Y_2(t)
% = t\Bigl(A \cos(3t)+B \sin(3t)\Bigr)+c_1 \cos(3t) + c_2 \sin(3t)
% (here A,B are determined using the method of undetermined coefficients).
% As t goes to infinity the particular solution  t\Bigl(A \cos(3t)+B \sin(3t)\Bigr) = t C\cos(3t -\phi) will dominate, giving oscillations with increasing amplitude.
%% Problem 3(b)
% We have the ODE y''(t)+\frac{c}m y'(t) + \frac{k}m y(t) = \cos(3t) with characteristic equation p(r)=r^2+\frac{c}m r+ \frac{k}m=0. For critical damping we have a double root, i.e., \Bigl(\frac{c}m\Bigr)^2-4\frac{k}m=0, i.e., (\frac12)^2-4\frac{k}2=0 or k=\frac18. Then we have p(r) = r^2+ \frac12 r+ \frac1{16} = (r+\frac14)^2 with roots r_1=r_2=-\frac14.
% The general solution is y(t) = Y(t) + c_1 Y_1(t) + c_2 Y_2(t)
% = A \cos(3t)+B \sin(3t)+c_1 e^{-t/4} + c_2 t e^{-t/4}
% (here A,B are determined using the method of undetermined coefficients).
% As t goes to infinity the terms c_1 e^{-t/2} + c_2 t e^{-t/2} will decay exponentially, so the solution will be very close to the particular solution A \cos(3t)+B \sin(3t) = C\cos(3t-\phi).
%% Problem 4(a)
% f(t) = t+u(t-1) \bigl(e^{-t}-t\bigr), hence F(s)=\frac1{s^2}+e^{-s} \mathcal{L}\left[e^{-(t+1)}-(t+1)\right]
% = \frac1{s^2}+
% e^{-s} \left( e^{-1}\frac1{s+1} - (\frac1{s^2} +\frac1s)
% \right)
% Y_1 = \mathcal{L}[y\prime]= sY+2, Y_2 = \mathcal{L}[y\prime\prime]=sY_1-3 = s(sY+2)-3.
% We have Y_2+2Y_1+3Y = F, i.e.
%       s^2Y + 2s -3+2sY+4+3Y = F, hence Y(s) = \frac1{s^2+2s+3}\bigl( -1-2s+F(s)\bigr) with F(s) from above.
%% Problem 4(b)
% We have s^2+2s+5 = (s+1)^2+2^2
% Let F(s)=\frac1{s(s^2+2s+5)} = \frac{A}s + \frac{B(s+1)+C}{(s+1)^2+2^2}, multiply by s(s^2+2s+5):
% 1 = A ((s+1)^2+2^2) + B s(s+1) + C s
% plugging in s=0 gives 1 = A5, so A=\frac15.
% plugging in s=-1  gives 1=4A-C, so C=-\frac15.
% plugging in s=1 gives 1=A8+B2+C, so B =-\frac15
% This gives f(t) = \frac15 -\frac15e^t\cos(2t)-\frac1{10}e^t\sin(2t)

syms s t
F = 1/(s*(s^2+2*s+5));
f = ilaplace(F)

% We then get
%       y(t) = u(t-1)f(t-1) = u(t-1) \bigl[
%  \frac15 -\frac15e^{t-1}\cos(2(t-1))-\frac1{10}e^{t-1}\sin(2(t-1))
% \bigr]