Matlab Code

Discussion in 'Programmer's Corner' started by Kayne, May 28, 2011.

  1. Kayne

    Thread Starter Active Member

    Mar 19, 2009
    105
    0
    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
     
  2. guitarguy12387

    Active Member

    Apr 10, 2008
    359
    12
    What are you trying to do with this code? What does it calculate?
     
  3. steveb

    Senior Member

    Jul 3, 2008
    2,433
    469
    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.
    Code ( (Unknown Language)):
    1.  
    2. Columns = 6
    3. Rows = 5
    4. r = 5 % This is where I have set up the space
    5. c = 6 % This is where I have
    6. ConVol = 10;              % This is the conductor voltage  
    7. ShieldVol = 0;            % This is the voltage around the edge of the TL
    8. Space = zeros(Rows,Columns); % The area of the transmission line
    9. B = zeros(Rows*Columns,5);% Creates 5 coloumns in B filled with zeros
    10. b = zeros(Rows*Columns,1);% Creates 1 column b filled with zeros
    11. v = zeros(Rows*Columns,1); % Creates 1 column filled with zeros
    12.  
    13.  
    14.  
    15. % Sets up d with a diagonal index vector for a sparse matrix
    16.  
    17. Columns = 6
    18. Rows = 5
    19. ConVol = 10;              % This is the conductor voltage  
    20. ShieldVol = 0;            % This is the voltage around the edge of the TL
    21.  
    22. d = [Columns 1 0 -1 -Columns];  
    23.  
    24. for r = 1:Rows         % For every row in grid
    25.     for c = 1:Columns     % For every column in grid
    26.         if Space(r,c) == 2  % The node that forms the condutor
    27.            B(mod((r-1)*Columns + c -2 + d(1), Columns*Rows)+2, 1) = 0; % Column 1 of B
    28.            B(mod((r-1)*Columns + c -2 + d(2), Columns*Rows)+2, 2) = 0; % Column 2 of B
    29.            B((r-1)*Columns + c                               , 3) = 1; % Column 3 of B
    30.            B(mod((r-1)*Columns + c -2 + d(4), Columns*Rows)+2, 4) = 0; % Column 4 of B
    31.            B(mod((r-1)*Columns + c -2 + d(5), Columns*Rows)+2, 5) = 0; % Column 5 of B
    32.            b((r-1)*Columns + c) = ConVol;
    33.         elseif Space(r,c) == 1      % Node on the edge of the TL
    34.             B(mod((r-1)*Columns + c -2 + d(1), Columns*Rows)+2, 1) = 0; % Column 1 of B
    35.             B(mod((r-1)*Columns + c -2 + d(2), Columns*Rows)+2, 2) = 0; % Column 2 of B
    36.             B((r-1)*Columns + c                               , 3) = 1; % Column 3 of B
    37.             B(mod((r-1)*Columns + c -2 + d(4), Columns*Rows)+2, 4) = 0; % Column 4 of B
    38.             B(mod((r-1)*Columns + c -2 + d(5), Columns*Rows)+2, 5) = 0; % Column 5 of B
    39.             b((r-1)*Columns +c) = ShieldVol;
    40.         elseif c ~= Columns          % The Dielectric node that donot fall not on symmetry edge
    41.             B(mod((r-1)*Columns + c -2 + d(1), Columns*Rows)+2, 1) = -1; % Column 1 of B
    42.             B(mod((r-1)*Columns + c -2 + d(2), Columns*Rows)+2, 2) = -1; % Column 2 of B
    43.             B((r-1)*Columns + c                               , 3) =  4; % Column 3 of B
    44.             B(mod((r-1)*Columns + c -2 + d(4), Columns*Rows)+2, 4) = -1; % Column 4 of B
    45.             B(mod((r-1)*Columns + c -2 + d(5), Columns*Rows)+2, 5) = -1; % Column 5 of B
    46.         else                   % The Dielectric node that falls on the symmetry edge
    47.             B(mod((r-1)*Columns + c -2 + d(1), Columns*Rows)+2, 1) = -1; % Column 1 of B
    48.             B(mod((r-1)*Columns + c -2 + d(2), Columns*Rows)+2, 2) = -1; % Column 2 of B
    49.             B((r-1)*Columns + c                               , 3) =  4; % Column 3 of B
    50.            B(mod((r-1)*Columns + c -2 + d(4), Columns*Rows)+2, 4) = -1; % Column 4 of B
    51.            B(mod((r-1)*Columns + c -2 + d(5), Columns*Rows)+2, 5) = -1; % Column 5 of B
    52.         end
    53.     end
    54. end
    55. A = spdiags(B,d,Rows*Columns,Rows*Columns);   % Create sparse matrix A
    56.  
     
  4. Kayne

    Thread Starter Active Member

    Mar 19, 2009
    105
    0
    Hi,

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

    Code ( (Unknown Language)):
    1. ConVol = 10;              % This is the conductor voltage  
    2. ShieldVol = 0;            % This is the voltage around the edge of the TL
    3.  
    4. % For a requirment of the project it was stated that there needs to be many
    5. % nodes. 'h' is going to be the cell width for the transmission like
    6.  
    7. h = 0.01;                  
    8.  
    9. % This is to specifiy how many rows and colums are required, Becuase of
    10. % symetrey only half the space is required to be worked out.  
    11.  
    12.  
    13. Rows = 2/h +1 ;                
    14. Columns = 2.5/h +1 ;          
    15. T = h;                      
    16.  
    17. Space = zeros(Rows,Columns); % The area of the transmission line
    18. B = zeros(Rows*Columns,5);% Creates 5 coloumns in B filled with zeros
    19. b = zeros(Rows*Columns,1);% Creates 1 column b filled with zeros
    20. v = zeros(Rows*Columns,1); % Creates 1 column filled with zeros
    21.  
    22. % Sets up d with a diagonal index vector for a sparse matrix
    23.  
    24. ConVol = 10;              % This is the conductor voltage  
    25. ShieldVol = 0;            % This is the voltage around the edge of the TL
    26.  
    27. % outlines the space and where the conductors are positioned.
    28. for r = 1:Rows               % For all the rows in the grid
    29. for c = 1:Columns            % For all the column in grid
    30.         x = c*h - h;         % x position of the current node
    31.         y = r*h - h;         % y position of the current node
    32. if ((x<(2+T)&&(x>(2-T)))||(x<(3+T)&&(x>(3-T))))&&((y<(1.5+T)&&(y>(0.5-T)))),
    33.              Space(r,c) = 2; % So now the conductor nodes are set to 2      
    34. elseif (x<T)||(x<(5+T)&&(x>(5-T)))||(y<T)||(y<(2+T)&&(y>(2-T))),
    35.            Space(r,c) = 1;  % So now the nodes on the edge of the TL are set to one    
    36.           end
    37.     end
    38. end
    39.  
    40.  
    41. d = [Columns 1 0 -1 -Columns];  
    42.  
    43. for r = 1:Rows         % For every row in grid
    44.     for c = 1:Columns     % For every column in grid
    45.         if Space(r,c) == 2  % The node that forms the condutor
    46.            B((r-1)*Columns + c                               , 3) = 1; % Column 3 of B
    47.                b((r-1)*Columns + c) = ConVol;
    48.         elseif Space(r,c) == 1      % Node on the edge of the TL
    49.                B((r-1)*Columns + c                               , 3) = 1; % Column 3 of B
    50.                b((r-1)*Columns +c) = ShieldVol;
    51.         elseif c ~= Columns          % The Dielectric node that donot fall not on symmetry edge
    52.             B(mod((r-1)*Columns + c -2 + d(1), Columns*Rows)+2, 1) = -1; % Column 1 of B
    53.             B(mod((r-1)*Columns + c -2 + d(2), Columns*Rows)+2, 2) = -1; % Column 2 of B
    54.             B((r-1)*Columns + c                               , 3) =  4; % Column 3 of B
    55.             B(mod((r-1)*Columns + c -2 + d(4), Columns*Rows)+2, 4) = -1; % Column 4 of B
    56.             B(mod((r-1)*Columns + c -2 + d(5), Columns*Rows)+2, 5) = -1; % Column 5 of B
    57.         else                   % The Dielectric node that falls on the symmetry edge
    58.             B(mod((r-1)*Columns + c -2 + d(1), Columns*Rows)+2, 1) = -1; % Column 1 of B
    59.             B(mod((r-1)*Columns + c -2 + d(2), Columns*Rows)+2, 2) = -1; % Column 2 of B
    60.             B((r-1)*Columns + c                               , 3) =  4; % Column 3 of B
    61.            B(mod((r-1)*Columns + c -2 + d(4), Columns*Rows)+2, 4) = -1; % Column 4 of B
    62.            B(mod((r-1)*Columns + c -2 + d(5), Columns*Rows)+2, 5) = -1; % Column 5 of B
    63.         end
    64.     end
    65. end
    66. 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)
    Code ( (Unknown Language)):
    1. B2 = zeros(size(B));
    2. B2(2:end,[1 2 4 5]) = -1;
    3. B2(1:end-1,3) = 4;
    4. isequal(B,B2)
    5. %%%%%%%%
    6. Space(r,c) == 2  % The node that forms the condutor
    7.             B((r-1)*Columns + c                               , 3) = 1; % Column 3 of B
    8.                 b((r-1)*Columns + c) = ConVol;
    9.          elseif Space(r,c) == 1      % Node on the edge of the TL
    10.                 B((r-1)*Columns + c                               , 3) = 1; % Column 3 of B
    11.                 b((r-1)*Columns +c) = ShieldVol;
    12.  
    Is this possible to do?


    Thank for the head up with the code-block steveb.
     
    Last edited: May 30, 2011
Loading...