Adding matrices in a for loop

8 views (last 30 days)
Fatima Yusuf
Fatima Yusuf on 16 Mar 2021
Edited: Fatima Yusuf on 16 Mar 2021
I have a for loop that has # of iterations ('n'), and want to add the 1st matrix values with the 2nd matrix, then add with the 3rd matrix, till n matrix.
I initialized a matrix as zero matrix before the for loop.
I did not get the right result, could someone please fix the code?
Here is it :
ybus = zeros (busses,busses)
for i=1:lines
ybus(fbus(i),fbus(i)) = 1/(r(i)+(1i*x(i))) ;
ybus(tbus(i),tbus(i)) = ybus(fbus(i),fbus(i));
ybus(tbus(i),fbus(i)) = -1/(r(i)+(1i*x(i)));
ybus(fbus(i),tbus(i)) = -1/(r(i)+(1i*x(i))) ;
ybus=ybus+ybus
end
  2 Comments
Geoff Hayes
Geoff Hayes on 16 Mar 2021
Fatima - I think we would need to more about fbus and tbus and how their values are used as indices into ybus. How are lines related to bussess? What are the dimensions of each matrix supposed to be? Is each a column array/vector or something else? Please clarify.
Fatima Yusuf
Fatima Yusuf on 16 Mar 2021
Edited: Fatima Yusuf on 16 Mar 2021
% | From | To | R | X | gsh | B | T|ysh
% | Bus | Bus | pu | pu | pu | pu | ph-sh
linedata = [1 2 1 10 0 0 0
1 3 0 2 0 2 0];
fbus=linedata(:,1); % Reading from bus
tbus=linedata(:,2); % Reading to bus
r = linedata(:,3); % Resistance, R...
x = linedata(:,4); % Reactance, X...
gsh=linedata(:,5);
B = 1i*linedata(:,6); % Total Ground Admittance
% Eg Xg Zd
BusData=[1 1 inf
0 0 inf
0 0 10]
%1) search for maximum # of bus
busses=max(max(fbus),max(tbus));
ybus = zeros (busses,busses) ;%2)initialization
--------------------------------------------------------
fbus = [ 1 1 ] , tbus = [2 3]
Since the 1st line is connected between Bus#1 and Bus#2, so 11 , 12 , 21, and 22 elements of ybus matrix will change.
I calculated the values of those elements, and updated ybus matrix ( only these elements are changed and the rest are 0's)
Then, I read the 2nd line (from Linedata matrix), which is connected between Bus#1 and Bus#3, so element 11, 13, 31, and 33 will change.
I calculated the values of those elements, and want to update the ybus matrix again.
I want to add the updated ybus matrix after reading the 1st line to the updated one after reading the 2nd line.
-------------------------------------------------------
ybus matrix is 3*3 in this example, as the maximum value of fbus and tbus is 3.

Sign in to comment.

Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!