Matlab Code

Thread Starter

Kayne

Joined Mar 19, 2009
105
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
 

steveb

Joined Jul 3, 2008
2,436
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.
It would take me some time to try and figure out what you are trying to do before I could make specific suggestions. If I get some time later tonight, I might give an attempt to make some suggestions. However, generally in Matlab we try to avoid explicit "for loops". This is because Matlab has so many vector based commands that allow you to do those tasks in one or two lines. The main benefit of this is speed of execution which can be 100s of time faster when done the "correct" way. For what you are doing, the run-time should be very fast, so it's not really a big deal. Readability and ease of accurate implementation may be better goals to have in your case. Still, this is a good learning opportunity.

Note, to make it easier for others to help you, I copied your code using the "code-block" command. This makes it easier to read.
Rich (BB code):
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
 

Thread Starter

Kayne

Joined Mar 19, 2009
105
Hi,

The code that I have done is to calculate the Finite Difference of a transverse electromagnetic(TEM) transmission line.

Rich (BB code):
ConVol = 10;              % This is the conductor voltage  
ShieldVol = 0;            % This is the voltage around the edge of the TL

% For a requirment of the project it was stated that there needs to be many
% nodes. 'h' is going to be the cell width for the transmission like 

h = 0.01;                  

% This is to specifiy how many rows and colums are required, Becuase of 
% symetrey only half the space is required to be worked out.  


Rows = 2/h +1 ;                
Columns = 2.5/h +1 ;           
T = h;                      

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

ConVol = 10;              % This is the conductor voltage  
ShieldVol = 0;            % This is the voltage around the edge of the TL

% outlines the space and where the conductors are positioned.
for r = 1:Rows               % For all the rows in the grid
for c = 1:Columns            % For all the column in grid
        x = c*h - h;         % x position of the current node
        y = r*h - h;         % y position of the current node
if ((x<(2+T)&&(x>(2-T)))||(x<(3+T)&&(x>(3-T))))&&((y<(1.5+T)&&(y>(0.5-T)))), 
             Space(r,c) = 2; % So now the conductor nodes are set to 2      
elseif (x<T)||(x<(5+T)&&(x>(5-T)))||(y<T)||(y<(2+T)&&(y>(2-T))), 
           Space(r,c) = 1;  % So now the nodes on the edge of the TL are set to one    
          end
    end
end


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((r-1)*Columns + c                               , 3) = 1; % Column 3 of B
               b((r-1)*Columns + c) = ConVol;
        elseif Space(r,c) == 1      % Node on the edge of the TL
               B((r-1)*Columns + c                               , 3) = 1; % Column 3 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
Something that I notice was that because my B matrix was zero there were no need for then in the 'if' or 'for' that assigned 0's to matrix B. But now I dont know how to reduce the code into a simpler form.

This little piece of code(B2 below)sort of does what I need but but I still dont know how to incorporate it into my code so I still can get the values assigned to CONVOLT and SHIELDVOL (which is after the %%%%% below)
Rich (BB code):
B2 = zeros(size(B)); 
B2(2:end,[1 2 4 5]) = -1; 
B2(1:end-1,3) = 4; 
isequal(B,B2)
%%%%%%%%
Space(r,c) == 2  % The node that forms the condutor
            B((r-1)*Columns + c                               , 3) = 1; % Column 3 of B
                b((r-1)*Columns + c) = ConVol;
         elseif Space(r,c) == 1      % Node on the edge of the TL
                B((r-1)*Columns + c                               , 3) = 1; % Column 3 of B
                b((r-1)*Columns +c) = ShieldVol;
Is this possible to do?


Thank for the head up with the code-block steveb.
 
Last edited:
Top