Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Understanding existing matlab code for .m file There is an existing MATLAB .m co

ID: 670234 • Letter: U

Question

Understanding existing matlab code for .m file

There is an existing MATLAB .m code that exists for creating an admittance matrix based on user input. Ignoring the transformer tap part, I just want your help in understanding the code in the formation of off diagonal elements and diagonal elements section, I understand the rest. For a 5/5 rating with full points, I need a step-by-step logic walkthrough

% Program to form Admittance Bus Formation with Transformer Tap setting..

clc
clear all
nbranch=input('enter total no. of lines in network');
disp('enter line data')
for p=1:1:nbranch
fb=input('from bus no. :');
tb=input('to bus no. :');
r=input('resistance of line =');
x=input('reactance of line =');
B=input('line charging admittance"b/2"=');
T=input('tap=');
z = r + i*x; % Z matrix...
y = 1./z; % To get inverse of each element...
Ldata(p,:)=[fb tb r x B T y];
end
fb = Ldata(:,1); % From bus number...
tb = Ldata(:,2); % To bus number...
r = Ldata(:,3); % Resistance, R...
x = Ldata(:,4); % Reactance, X...
b = Ldata(:,5); % Ground Admittance, B/2...
a = Ldata(:,6); % Tap setting value..
y = Ldata(:,7);
b = i*b; % Make B imaginary...

nbus = max(max(fb),max(tb)); % no. of buses...
Y = zeros(nbus,nbus); % Initialise YBus...

% Formation of the Off Diagonal Elements...
for k=1:nbranch
Y(fb(k),tb(k)) = Y(fb(k),tb(k))-y(k);
Y(tb(k),fb(k)) = Y(fb(k),tb(k));
end

% Formation of Diagonal Elements....
for m =1:nbus
for n =1:nbranch
if fb(n) == m
Y(m,m) = Y(m,m) + y(n) + b(n);
elseif tb(n) == m
Y(m,m) = Y(m,m) + y(n) + b(n);
end
end
end
Y % Bus Admittance Matrix..

Explanation / Answer

clear all %clear all previous variables

nbranch=input('enter total no. of lines in network'); % take number of branch as input and store in nbranch

disp('enter line data') %display the text "enter line data"

for p=1:1:nbranch   % take input for each branch , loop will run nbranch times

fb=input('from bus no. :'); %take source bus no. and store in fb

tb=input('to bus no. :');   %take destination bus no. and store in tb

r=input('resistance of line ='); %take resistance of line as input and store in r

x=input('reactance of line ='); %take reactance of line as input and store in x

B=input('line charging admittance"b/2"='); % take line charging as input and store in B

T=input('tap='); % take tap as input and store in T

z = r + i*x % Z matrix where r is the real part and x is imagenary part

y = 1./z; % To get inverse of each element...

Ldata(p,:)=[fb tb r x B T y];

end

fb = Ldata(:,1); % From bus number...

tb = Ldata(:,2); % To bus number...

r = Ldata(:,3); % Resistance, R...

x = Ldata(:,4); % Reactance, X...

b = Ldata(:,5); % Ground Admittance, B/2...

a = Ldata(:,6); % Tap setting value..

y = Ldata(:,7);

b = i*b; % Make B imaginary...

nbus = max(max(fb),max(tb)); % no. of buses...

Y = zeros(nbus,nbus); % Initialise YBus...

% Formation of the Off Diagonal Elements...

for k=1:nbranch   % loop run for all the branch

Y(fb(k),tb(k)) = Y(fb(k),tb(k))-y(k);  

Y(tb(k),fb(k)) = Y(fb(k),tb(k));

end

% Formation of Diagonal Elements....

for m =1:nbus % loop run for each bus

for n =1:nbranch %loop run for each branch

if fb(n) == m % check fb value of nth branch

Y(m,m) = Y(m,m) + y(n) + b(n); %update ybus value by the condition

elseif tb(n) == m %compare tb value of nth buranch

Y(m,m) = Y(m,m) + y(n) + b(n); %update ybus value by the condition

end

end

end

Y % Bus Admittance Matrix..