%% Experiment 2.2
%% Name:
%% Objectives
% To learn to use MATLAB and the Symbolic Math Toolbox to
% Find Laplace transforms for time functions
% Find time functions from Laplace transforms
% Create LTI transfer functions from symbolic transfer functions.
% Perform solutions of symbolic simultaneous equations.
%% Minimum Required Software Packages
% MATLAB, the Symbolic Math Toolbox, and the Control System Toolbox.
%
%
%% Prelab
%% Problem 1
% Using a hand calculation, find the Laplace transform of: f(t) = 0.0075 - 0.00034 e^{-2.5t}\cos(22t) + 0.087 e^{-2.5t}\sin(22t) - 0.0072 e^{-8t}
%%
% Answer:
% F\left(s\right)=\frac{3}{400s}-\frac{9}{1250\left(s+8\right)}-\frac{34s+85}{25000\left({\left(s+\frac{5}{2}\right)}^2 +484\right)}+\frac{957}{500\left({\left(s+\frac{5}{2}\right)}^2 +484\right)}
%
%% Problem 2
% Using a hand calculation, find the inverse Laplace transform of: F(s) = \frac{2(s+3)(s+5)(s+7)}{s(s+8)(s^2 + 10s + 100)}
%
% Answer:
% \frac{5\,{\textrm{e}}^{-8\,t} }{112}+\frac{237\,{\textrm{e}}^{-5\,t} \,{\left(\cos \left(5\,\sqrt{3}\,t\right)+\frac{\sqrt{3}\,\sin \left(5\,\sqrt{3}\,t\right)}{9}\right)}}{140}+\frac{21}{80}
%
%
%% Problem 3
% Use a hand calculation to solve the circuit for the Laplace Transform of the loop currents shown in the Figure below
%
%
% Answer:
% I_1 \left(s\right)=\frac{3\left(s^4 +16s^3 +39s^2 +40s+12\right)}{s^5 +26s^4 +205s^3 +396s^2 +284s+60}
% I_2 \left(s\right)=\frac{3s\left(s^3 +15s^2 +30s+8\right)}{s^5 +26s^4 +205s^3 +396s^2 +284s+60}
% I_3 \left(s\right)=\frac{3s\left(s^3 +14s^2 +24s+15\right)}{s^5 +26s^4 +205s^3 +396s^2 +284s+60}
%
%
%
%% Lab
%
%% Problem 1
% Use MATLAB and the Symbolic Math Toolbox to
%% 1.a.
% Generate symbolically the time function f(t) shown in Prelab Problem 1
%

% Insert your code here
syms s t
f1 = 0.0075 - 0.00034*exp(-2.5*t)*cos(22*t) + 0.087*exp(-2.5*t)*sin(22*t) - 0.0072*exp(-8*t)

%
%% 1.b.
% Generate symbolically F(s) shown in Prelab Problem 2. Obtain your result symbolically in both factored and polynomial form
%

% Insert your code here
F2 = (2*(s+3)*(s+5)*(s+7)) / (s*(s+8)*(s^2 + 10*s + 100))
F2_poly = collect(F2)

%
%% 1.c.
% Find the Laplace Transform of f(t) shown in Prelab Problem 1
%

% Insert your code here
F1 = laplace(f1)

%
%% 1.d.
% Find the inverse Laplace Transform of F(s) shown in Prelab Problem 2
%

% Insert your code here
f2 = ilaplace(F2)

%
%% 1.e.
% Generate an LTI transfer function for your symbolic representation of F(s) in Prelab Problem 2 in both Polynomial form and Factored form. Start with the F(s) you generated symbolically.
%

% Insert your code here
[n,d] = numden(F2);
np = sym2poly(n);
dp = sym2poly(d);
F2_LTI = tf(np, dp)

%
%% 1.f.
% Solve for the Laplace transforms of the loop currents in Prelab Problem 3
%

% Insert your code here
syms s

% Use mesh analysis to construct an augmented matrix
circuit = [s+7+5/s -2-s -5 3/s; -2-s 2*s+4+3/s -2-s 0; -5 -2-s s+8+4/s 0];

% put the matrix in RREF
sol = rref(circuit);
I_1 = sol(1,4);
I_2 = sol(2,4);
I_3 = sol(3,4);

%
%
%% Postlab
%
%% Problem 1
% Discuss the advantages and disadvantages between the Symbolic Math Toolbox and MATLAB alone to convert a transfer function from factored form to polynomial form and vice versa.
% The symbolic math toolbox is really good for manipulating simple transfer functions. For factorable transfer functions represented as the quotient of two polynomials, the Symbolic Math Toolbox is quite useful. Unfortunately, if the transfer function has complex roots, this becomes much more difficult. In some cases, I have also seen that the output is in a difficult to interpret format using variables like \sigma_1to represent a more complex, repeated part of an answer.
%
%
%% Problem 2
% Discuss the advantages and disadvantages of using the Symbolic Math Toolbox to generate LTI transfer functions
% It is very much possible to convert symbolic expressions to transfer functions in MATLAB. However, this requires separating the numerator and denominator, converting them individually to polynomial vectors, and finally converting the two vectors to a transfer function object.
%
%% Problem 3
% Discuss the advantages of using the Symbolic Math Toolbox to solve simultaneous equations of the type generated by the electrical network shown in Prelab Problem 3. Is it possible to solve these equations via MATLAB alone? Explain.
% Since MATLAB is a matrix-centric programming language, it only makes sense to use a matrix to solve simultaneous equations. It does not make much sense to use the Symbolic Math Toolbox for this purpose.
%
%
%% Problem 4
% Discuss any other observations you had using the Symbolic Math Toolbox.
% It is often difficult to get polynomials in a desired form. I have found that using expand(), collect(), and factor() are useful for manipulating the form of the polynomial.
%
%
%