0% found this document useful (0 votes)
6 views10 pages

Codes

Uploaded by

Hu Untalan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

Codes

Uploaded by

Hu Untalan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

% Enter your script below

% Define symbolic variable for s


s=tf('s')

% Transfer functions
G1 = 1/(s+7);
G2 = 1/(s^2 + 2*s + 3);
G3 = 1/(s+4);
G4 = 1/s;
G5 = 1/(s+7);
G6 = 1/(s^2 + 5*s + 10);
G7 = 3/(s+2);
H1 = 1/(s+6);
H2 = 1/s;
H3 = 1/(s+5);

% Forward paths
P1 = G1 * G2 * G6 * G7;
P2 = G1 * G3 * G5 * G7;
P3 = G1 * G3 * G4 * G6 * G7;
t
% Loop gains
L1 = G2 * G6 * H1;
L2 = G4 * H2;
L3 = G5 * H3;
L4 = G2 * G5 * H3;
L5 = G3 * G6 * H1;
L6 = G4 * G6 * H1 * H2;

% Calculate delta (Δ)


Delta = 1 - (L1 + L2 + L3 + L4 + L5 + L6) + (L1*L3 + L2*L5 + L3*L5 +
L4*L6);

% Calculate deltas for each path


Delta_P1 = 1 - (L3 + L5);
Delta_P2 = 1 - (L1 + L2 + L6);
Delta_P3 = 1 - (L1 + L3);

% Use Mason's Gain Formula


Ts = (P1 * Delta_P1 + P2 * Delta_P2 + P3 * Delta_P3) / Delta;
%'Numerator variable num';
% Define the transfer function G(s)
num = [8]; % Correct numerator

%'Denominator variable den';

%'G(s)';

%'T(s)';

%'Poles of T(s) use variable P';

% Place your answer inside the variables leftHand and rightHand


% Note: for and if should be used in obtaining the values.

% Correct denominator coefficients


den = [1 -2 -1 2 4 -8 -4 0];

% Create the transfer function G(s)


G = tf(num, den);

% Calculate the closed-loop transfer function T(s)


T = feedback(G, 1);

% Find the poles of T(s)


P = pole(T);

% Initialize counters for poles in RHP and LHP


leftHand = 0;
rightHand = 0;

% Check the real parts of the poles to determine their locations


for i = 1:length(P)
if real(P(i)) > 0
rightHand = rightHand + 1; % Pole in RHP
elseif real(P(i)) < 0
leftHand = leftHand + 1; % Pole in LHP
end
end

% Display the results


disp(['Number of poles in the Right Hand Plane: ', num2str(rightHand)]);
disp(['Number of poles in the Left Hand Plane: ', num2str(leftHand)]);
disp('Poles of T(s):');
disp(P);
% Code By
% Farzad Sagharchi ,Iran
% 2007/11/12
% Define the transfer function G(s) denominator coefficients
coeffVector = [1 2 3 4 5 -6 -7]; % Coefficients of the denominator
polynomial

ceoffLength = length(coeffVector);
rhTableColumn = ceil(ceoffLength / 2); % Correct column calculation

% Initialize the Routh-Hurwitz table


rhTable = zeros(ceoffLength, rhTableColumn);

% First two rows of the Routh-Hurwitz table


rhTable(1, :) = coeffVector(1:2:ceoffLength);

if rem(ceoffLength, 2) ~= 0
rhTable(2, 1:rhTableColumn - 1) = coeffVector(2:2:ceoffLength);
else
rhTable(2, :) = coeffVector(2:2:ceoffLength);
end

% Small perturbation value


epss = 0.01;

% Fill out the Routh-Hurwitz table


for i = 3:ceoffLength
% Handle special case where the entire row is zero
if all(rhTable(i-1, :) == 0)
order = ceoffLength - i;
cnt1 = 0;
cnt2 = 1;
for j = 1:rhTableColumn - 1
rhTable(i-1, j) = (order - cnt1) * rhTable(i-2, cnt2);
cnt2 = cnt2 + 1;
cnt1 = cnt1 + 2;
end
end

% Compute each element in the current row


for j = 1:rhTableColumn - 1
firstElemUpperRow = rhTable(i-1, 1);
rhTable(i, j) = ((rhTable(i-1, 1) * rhTable(i-2, j+1)) - ...
(rhTable(i-2, 1) * rhTable(i-1, j+1))) / firstElemUpperRow;
end

% Substitute zero value if the first element of the row is zero


if rhTable(i, 1) == 0
rhTable(i, 1) = epss;
end
end

% Calculate number of right-hand poles (unstable poles)


unstablePoles = 0;
for i = 1:ceoffLength - 1
if sign(rhTable(i, 1)) * sign(rhTable(i+1, 1)) == -1
unstablePoles = unstablePoles + 1;
end
end

% Display the Routh-Hurwitz table


fprintf('\nRouth-Hurwitz Table:\n')
disp(rhTable)

% Determine stability
if unstablePoles == 0
fprintf('~~~~~> It is a stable system! <~~~~~\n')
else
fprintf('~~~~~> It is an unstable system! <~~~~~\n')
end

% Display the number of right-hand side poles


fprintf('\nNumber of right-hand side poles = %2.0f\n', unstablePoles)

sysRoots = roots(coeffVector);
fprintf('\nGiven polynomial coefficients roots:\n')
disp(sysRoots)
format compact
syms es e2 Rs i1 e3 R2 e1 i2 i3 k R5 R1 ed R3 R4 is e0
% STEP 1 - Use circuit analysis to write equations for the circuit.

% The circuit has 4 inputs includes es, is, ed, and ke1.
% The output is eo.
% The unknown variables are i1, i2, i3, e1, e2, and e3.
% Use KCL to write an equation for i1.

% Use KCL to write an equation for i2.

% Assume the ground is at the bottom of the circuit and use KCL to write
an equation for i3.

% Use KVL to write an equation for e1. You should use KVL because you are
relating voltages in a loop.

% Use Ohm's Law to write an equation for e2. You should use Ohm's law to
relate node voltages and loop currents.

% Use KVL to write an equation for e3. You should use KVL with Ohm's law
to related the voltages around the loop in terms of current and
resistance and voltage.
% Use Ohm's Law to write an equation for eo. Use Ohm's law to relate
voltages and currents with resistance.

% STEP 2 - Use the equations to create the signal flow graph.


% Use equation (7) to draw the output of the signal-flow graph, eo, and
the gain from i3.
% Use equation (3) to connect the inputs e3 and ke1 to i3.
% Use equation (6) to connect the inputs ed, is, i2, and i3 to e3.
% Use equation (2) to connect the inputs e2 and e3 to i2.
% Use equation (5) to connect the inputs i1 and i3 to e2.
% Use equation (1) to connect the input es to i1 to complete the signal-
flow graph for the entire system.

% STEP 3 - Find eo/ed and set it equal to zero and then solve for k.
% Redraw the signal flow graph showing only the input ed, the gains and
the output, eo.
% Write the 2 forward paths from ed to eo.

% Write the one loop gain between ed to eo.

% Use the Gain Formula for SFG to write the transfer function.

% Set the transfer function equal to zero and solve for k to minimize the
effect of the disturbance voltage.

% Write your Solution below this line.


eq1 = i1 == (es-e2)/Rs

eq2 = (e2-e3)/R2 == e1/R2;

eq3 = i3==(e3-k*e1)/R5;
eq3 = i3==(e3-k*(e2-e3))/R5;
% Use KVL to write an equation for e1. You should use KVL because you are
relating voltages in a loop.

eq4 = e1==e2-e3;

% Use Ohm's Law to write an equation for e2. You should use Ohm's law to
relate node voltages and loop currents.

eq5 = e2==R1*(i1-i2)'

% Use KVL to write an equation for e3. You should use KVL with Ohm's law
to related the voltages around the loop in terms of current and
resistance and voltage.

eq6 = e3 == ed + R2*i2 - R5*i3 - k*e1;

eq5 = e0==R5*i3

P1 = 1*(1/R5)*R5;
P2 = k

% Write the one loop gain between ed to eo.

L1 = -(R2+R4)*(1/R5)

L2 = -(R3+R4)*(1/R5)

% Use the Gain Formula for SFG to write the transfer function.

eq7 = e0/ed==(P1+P2)/(1-L1)

eq8 = subs(eq7, lhs(eq7), 0)


eq9 = -1 == k

You might also like