Hi All,
I have some code that I would like to know if it can be smiplified if anyway to reduce the amount of lines I am using. When I spoke to the lecutre he thinks that there is a way. My skills in this are not the best but managed to plow my way though.
I used the modulus command in Matlab to get the following but wanted to know if this can be simplified using another command?
%I have just added these into here in my program they are automatic but if i can work it out for these then I should be ok with the rest.
Columns = 6
Rows = 5
r = 5 % This is where I have set up the space
c = 6 % This is where I have
ConVol = 10; % This is the conductor voltage
ShieldVol = 0; % This is the voltage around the edge of the TL
Space = zeros(Rows,Columns); % The area of the transmission line
B = zeros(Rows*Columns,5);% Creates 5 coloumns in B filled with zeros
b = zeros(Rows*Columns,1);% Creates 1 column b filled with zeros
v = zeros(Rows*Columns,1); % Creates 1 column filled with zeros
% Sets up d with a diagonal index vector for a sparse matrix
Columns = 6
Rows = 5
ConVol = 10; % This is the conductor voltage
ShieldVol = 0; % This is the voltage around the edge of the TL
d = [Columns 1 0 -1 -Columns];
for r = 1:Rows % For every row in grid
for c = 1:Columns % For every column in grid
if Space(r,c) == 2 % The node that forms the condutor
B(mod((r-1)*Columns + c -2 + d(1), Columns*Rows)+2, 1) = 0; % Column 1 of B
B(mod((r-1)*Columns + c -2 + d(2), Columns*Rows)+2, 2) = 0; % Column 2 of B
B((r-1)*Columns + c , 3) = 1; % Column 3 of B
B(mod((r-1)*Columns + c -2 + d(4), Columns*Rows)+2, 4) = 0; % Column 4 of B
B(mod((r-1)*Columns + c -2 + d(5), Columns*Rows)+2, 5) = 0; % Column 5 of B
b((r-1)*Columns + c) = ConVol;
elseif Space(r,c) == 1 % Node on the edge of the TL
B(mod((r-1)*Columns + c -2 + d(1), Columns*Rows)+2, 1) = 0; % Column 1 of B
B(mod((r-1)*Columns + c -2 + d(2), Columns*Rows)+2, 2) = 0; % Column 2 of B
B((r-1)*Columns + c , 3) = 1; % Column 3 of B
B(mod((r-1)*Columns + c -2 + d(4), Columns*Rows)+2, 4) = 0; % Column 4 of B
B(mod((r-1)*Columns + c -2 + d(5), Columns*Rows)+2, 5) = 0; % Column 5 of B
b((r-1)*Columns +c) = ShieldVol;
elseif c ~= Columns % The Dielectric node that donot fall not on symmetry edge
B(mod((r-1)*Columns + c -2 + d(1), Columns*Rows)+2, 1) = -1; % Column 1 of B
B(mod((r-1)*Columns + c -2 + d(2), Columns*Rows)+2, 2) = -1; % Column 2 of B
B((r-1)*Columns + c , 3) = 4; % Column 3 of B
B(mod((r-1)*Columns + c -2 + d(4), Columns*Rows)+2, 4) = -1; % Column 4 of B
B(mod((r-1)*Columns + c -2 + d(5), Columns*Rows)+2, 5) = -1; % Column 5 of B
else % The Dielectric node that falls on the symmetry edge
B(mod((r-1)*Columns + c -2 + d(1), Columns*Rows)+2, 1) = -1; % Column 1 of B
B(mod((r-1)*Columns + c -2 + d(2), Columns*Rows)+2, 2) = -1; % Column 2 of B
B((r-1)*Columns + c , 3) = 4; % Column 3 of B
B(mod((r-1)*Columns + c -2 + d(4), Columns*Rows)+2, 4) = -1; % Column 4 of B
B(mod((r-1)*Columns + c -2 + d(5), Columns*Rows)+2, 5) = -1; % Column 5 of B
end
end
end
A = spdiags(B,d,Rows*Columns,Rows*Columns); % Create sparse matrix A
Hopefully this question will make sense I am not sure how else I can post it.
Thanks
I have some code that I would like to know if it can be smiplified if anyway to reduce the amount of lines I am using. When I spoke to the lecutre he thinks that there is a way. My skills in this are not the best but managed to plow my way though.
I used the modulus command in Matlab to get the following but wanted to know if this can be simplified using another command?
%I have just added these into here in my program they are automatic but if i can work it out for these then I should be ok with the rest.
Columns = 6
Rows = 5
r = 5 % This is where I have set up the space
c = 6 % This is where I have
ConVol = 10; % This is the conductor voltage
ShieldVol = 0; % This is the voltage around the edge of the TL
Space = zeros(Rows,Columns); % The area of the transmission line
B = zeros(Rows*Columns,5);% Creates 5 coloumns in B filled with zeros
b = zeros(Rows*Columns,1);% Creates 1 column b filled with zeros
v = zeros(Rows*Columns,1); % Creates 1 column filled with zeros
% Sets up d with a diagonal index vector for a sparse matrix
Columns = 6
Rows = 5
ConVol = 10; % This is the conductor voltage
ShieldVol = 0; % This is the voltage around the edge of the TL
d = [Columns 1 0 -1 -Columns];
for r = 1:Rows % For every row in grid
for c = 1:Columns % For every column in grid
if Space(r,c) == 2 % The node that forms the condutor
B(mod((r-1)*Columns + c -2 + d(1), Columns*Rows)+2, 1) = 0; % Column 1 of B
B(mod((r-1)*Columns + c -2 + d(2), Columns*Rows)+2, 2) = 0; % Column 2 of B
B((r-1)*Columns + c , 3) = 1; % Column 3 of B
B(mod((r-1)*Columns + c -2 + d(4), Columns*Rows)+2, 4) = 0; % Column 4 of B
B(mod((r-1)*Columns + c -2 + d(5), Columns*Rows)+2, 5) = 0; % Column 5 of B
b((r-1)*Columns + c) = ConVol;
elseif Space(r,c) == 1 % Node on the edge of the TL
B(mod((r-1)*Columns + c -2 + d(1), Columns*Rows)+2, 1) = 0; % Column 1 of B
B(mod((r-1)*Columns + c -2 + d(2), Columns*Rows)+2, 2) = 0; % Column 2 of B
B((r-1)*Columns + c , 3) = 1; % Column 3 of B
B(mod((r-1)*Columns + c -2 + d(4), Columns*Rows)+2, 4) = 0; % Column 4 of B
B(mod((r-1)*Columns + c -2 + d(5), Columns*Rows)+2, 5) = 0; % Column 5 of B
b((r-1)*Columns +c) = ShieldVol;
elseif c ~= Columns % The Dielectric node that donot fall not on symmetry edge
B(mod((r-1)*Columns + c -2 + d(1), Columns*Rows)+2, 1) = -1; % Column 1 of B
B(mod((r-1)*Columns + c -2 + d(2), Columns*Rows)+2, 2) = -1; % Column 2 of B
B((r-1)*Columns + c , 3) = 4; % Column 3 of B
B(mod((r-1)*Columns + c -2 + d(4), Columns*Rows)+2, 4) = -1; % Column 4 of B
B(mod((r-1)*Columns + c -2 + d(5), Columns*Rows)+2, 5) = -1; % Column 5 of B
else % The Dielectric node that falls on the symmetry edge
B(mod((r-1)*Columns + c -2 + d(1), Columns*Rows)+2, 1) = -1; % Column 1 of B
B(mod((r-1)*Columns + c -2 + d(2), Columns*Rows)+2, 2) = -1; % Column 2 of B
B((r-1)*Columns + c , 3) = 4; % Column 3 of B
B(mod((r-1)*Columns + c -2 + d(4), Columns*Rows)+2, 4) = -1; % Column 4 of B
B(mod((r-1)*Columns + c -2 + d(5), Columns*Rows)+2, 5) = -1; % Column 5 of B
end
end
end
A = spdiags(B,d,Rows*Columns,Rows*Columns); % Create sparse matrix A
Hopefully this question will make sense I am not sure how else I can post it.
Thanks