%% Experiment 2.1
%% Name: Aidan Sharpe
%% Objectives
% To learn to use MATLAB to
% Generate Polynomials
% Manipulate Polynomials
% Generate Transfer Functions
% Manipulate Transfer Functions
% Perform Partial-Fraction Expansions
%% Minimum Required Software Packages
% MATLAB and the Control System Toolbox.
%
%
%% Prelab
%% Problem 1
% Calculate the following by hand or with a calculator:
%% 1.a
% The roots of P_1 = s^6 + 7s^5 + 2s^4 + 9s^3 + 10s^2 + 12s + 15
% Answer:
% s = {
%     -6.873,
%      0.7632 + j1.0822,
%     0.7632 - j1.0822,
%     -1,
%     -0.3266 + j1.0667,
%     -0.3266 - j1.0667
% }
%
%
%
%% 1.b
% The roots of P_2 = s^6 + 9s^5 + 8s^4 + 9s^3 + 12s^2 + 15s^2
% Answer:
% s = {
%     0,
%     -8.0986,
%     -1.6736,
%     0.3861 + j1.3576,
%     0.3861 - j1.3576
% }
%
%
%
%% 1.c
% P_3 = P_1 + P_2
% P_4 = P_1 - P_2
% P_5 = P_1 P_2
% Answer:
% P_3 =2s^6 +16s^5 +10s^4 +18s^3 +37s^2 +12s+15
% P_4 =-2s^5 -6s^4 -17s^2 +12s+15
% P_5 =s^{12} +16s^{11} +73s^{10} +92s^9 +197s^8 +381s^7 +338s^6 +564s^5 +498s^4 +459s^3 +405s^2
%
%% Problem 2
% Calculate by hand or with a calculator the polynomial
% P_6 = (s+7)(s+8)(s+3)(s+5)(s+9)(s+10)
% Answer:
% P_6 =s^6 +42s^5 +718s^4 +6372s^3 +30817s^2 +76530s+75600
%
%% Problem 3
% Calculate by hand or with a calculator the following transfer functions:
%% 3.a
% G_1(s) = \frac{20(s+2)(s+3)(s+6)(s+8)}{s(s+7)(s+9)(s+10)(s+15)}
% represented as a numerator polynomial divided by a denominator polynomial
% Answer:
% G_1 \left(s\right)=\frac{20s^4 +380s^3 +2480s^2 +6480s+5760}{s^5 +41s^4 +613s^3 +3975s^2 +9450s}
%
%% 3.b
% G_2 = \frac{s^4 + 17s^3 + 99s^2 + 223s + 140}{s^5 + 32s^4 + 363s^3 + 2092s^2 + 5052s + 4320}
% expressed as factors in the numerator divided by the factors in the denominator, similar to the form of G_1(s)in Prelab 3a.
% Answer:
% G_2 =\frac{\left(s+5\right)\left(s+4\right)\left(s+7\right)\left(s+1\right)}{\left(s+16\ldotp 785\right)\left(s+5\ldotp 559+\mathrm{j5}\ldotp 167\right)\left(s+5\ldotp 559-\mathrm{j5}\ldotp 167\right)\left(s+2\ldotp 048+\mathrm{j0}\ldotp 522\right)\left(s+2\ldotp 048-\mathrm{j0}\ldotp 522\right)}
%
%% 3.c
% G_3(s) = G_1(s) + G_2(s)
% G_4(s) = G_1(s) - G_2(s)
% G_5(s) = G_1(s) G_2(s)
% expressed as factors divided by factors and expressed as polynomials divided by polynomials.
% Answers:
%
% G_3 \left(s\right)=\frac{21s^9 +1078s^8 +23309s^7 +28498s^6 +2156355s^5 +10427494s^4 +31725395s^3 +58164330s^2 +58416120s+10427494}{s^{10} +73s^9 +2288s^8 +40566s^7 +449993s^6 +3239173s^5 +15020046s^4 +42499260s^3 +{64913400}^2 +40824000s}
%
%
% ans
%% Problem 4
% Calculate by hand or with a calculator the partial-fraction expansion of the following transfer functions:
%% 4.a
% G_6 = \frac{5(s+2)}{s(s^2 + 8s + 15)}
% Answer:
% G_6 =\frac{5}{6\left(s+3\right)}-\frac{3}{2\left(s+5\right)}+\frac{2}{3s}
%
%% 4.b
% G_7 = \frac{5(s+2)}{s(s^2 + 6s + 9)}
% Answer:
% G_7 =\frac{5}{3*{\left(s+3\right)}^2 }-\frac{10}{9\left(s+3\right)}+\frac{10}{9s}
%
%% 4.c
% G_8 = \frac{5(s+2)}{s(s^2 + 6s + 34)}
% Answer:
% G_8 =\frac{5}{17s}-\frac{\frac{5s}{17}-\frac{55}{17}}{s^2 +6s+34}
%
%
%
%
%% Lab
%
%% Problem 1
% Use MATLAB to find P_3, P_4, and P_5 in Prelab 1

% Insert your code here
P_1 = [1 7 2 9 10 12 15];
P_2 = [1 9 8 9 12+15 0 0];
P_3 = P_1 + P_2
P_4 = P_1 - P_2
P_5 = conv(P_1, P_2)

%
%% Problem 2
% Use only one MATLAB command to find P_6 in Prelab 2.

% Insert your code here
syms s
P_6 = (s+7)*(s+8)*(s+3)*(s+5)*(s+9)*(s+10);
P_6 = expand(P_6)

%
%% Problem 3
% Use only two MATLAB commands to find G_1(s) in Prelab 3a represented as a polynomial divided by a polynomial.

% Insert your code here
syms s
G_1 = (20*(s+2)*(s+3)*(s+6)*(s+8)) / (s*(s+7)*(s+9)*(s+10)*(s+15));
G_1 = collect(G_1)

%
%% Problem 4
% Use only two MATLAB commands to find G_2(s) expressed as factors in the numerator divided by factors in the denominator.

% Insert your code here
syms s
G_2 = (s^4 + 17*s^3 + 99*s^2 + 223*s + 140)/(s^5 + 32*s^4 + 363*s^3 + 2092*s^2 + 14424);
G_2 = factor(G_2)

%
%% Problem 5
% Using various combinations of G_1(s) and G_2(s), find G_3(s), G_4(s), and G_5(s). Various combinations implies mixing and matching G_1(s) and G_2(s) expressed as factors and polynomials. For example, in finding G_3(s), G_1(s) can be expressed in a factored 5form and G_2(s) can be expressed in polynomial form. Another combination is G_1(s) and G_2(s) both expressed as polynomials. Still another combination is G_1(s) and G_2(s) both expressed in factored form.

% Insert your code here
syms s t

G_1_fact = (20*(s+2)*(s+3)*(s+6)*(s+8)) / (s*(s+7)*(s+9)*(s+10)*(s+15));
G_1_poly = collect(G_1_fact);
G_2_poly = (s^4 + 17*s^3 + 99*s^2 + 223*s + 140) / (s^5 + 32*s^4 + 363*s^3 + 2092*s^2 + 5052*2 + 4320);

G_3_1 = G_1_fact + G_2_poly;
G_3_2 = G_1_poly + G_2_poly;

G_4_1 = G_1_fact - G_2_poly;
G_4_2 = G_1_poly - G_2_poly;

G_5_1 = G_1_fact * G_2_poly;
G_5_2 = G_1_poly * G_2_poly;

%% Problem 6
% Use MATLAB to evaluate the partial fraction expansions shown in Prelab 4.

% Insert your code here
syms s
G_6 = (5*(s+2)) / (s*(s^2 + 8*s + 15));
G_7 = (5*(s+2)) / (s*(s^2 + 6*s + 9));
G_8 = (5*(s+2)) / (s*(s^2 + 6*s + 34));

G_6 = partfrac(G_6)
G_7 = partfrac(G_7)
G_8 = partfrac(G_8)

%
%
%% Postlab
%
%% Problem 1
% Discuss your findings for Lab Problem 5. What can you conclude?
% The form of the inputs do not matter since they all containt the same information. Simply telling MATLAB how to format the output is enough to get the desired results.
%
%
%% Problem 2
% Discuss the use of MATLAB to manipulate transfer functions and polynomials. Discuss any shortcomings in using MATLAB to evaluate partial fraction expansions.
% Using the partfrac function to calculate partial fractions is very useful. Unfortunately, if there is a complex root in the denominator, MATLAB is unable to put the partial fraction in a proper form.
%