CodeCodeCode

ENGR132: HW06 - plate_update_name

May 6th, 2012
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.73 KB | None | 0 0
  1. function [temp_update] = plate_update_name(temp_matrix)
  2.  
  3. % FUNCTION: Updates the temperatures of a given matrix.
  4. %
  5. % INPUTS:
  6. % 1) temp_matrix: The original matrix containing the original temperature.
  7. %
  8. % OUTPUTS:
  9. % 1) temp_update: The final, updated temperature.
  10.  
  11. %Gets the dimensions of the given matrix.
  12. [row, column] = size(temp_matrix);
  13. %Fills new matrix with zeros according to given matrix dimensions.
  14. temp_update = zeros(row, column);
  15.  
  16. %Checks to see if given matrix is at least 2x2.
  17. if row < 2 || column < 2
  18.     error('Grid must be at least 2 by 2 values.');
  19. end
  20.  
  21. %Counts along the columns of the user's matrix
  22. for count_column = 1:1:column
  23.     %Counts along the rows of the user's matrix
  24.     for count_row = 1:1:row
  25.         %If value is in a corner of the grid.
  26.         if (count_row == 1 || count_row == row) && (count_column == 1 || count_column == column)
  27.             %If value is on the top-left corner of the grid.
  28.             if count_row == 1 && count_column == 1
  29.                 temp_update(count_row, count_column) = (temp_matrix(count_row, count_column + 1) + temp_matrix(count_row + 1, count_column)) / 2;
  30.             %If value is on the bottom-left corner of the grid.
  31.             elseif count_row == row && count_column == 1
  32.                 temp_update(count_row, count_column) = (temp_matrix(count_row, count_column + 1) + temp_matrix(count_row - 1, count_column)) / 2;
  33.             %If value is on the top-right corner of the grid.
  34.             elseif count_row == 1 && count_column == column
  35.                 temp_update(count_row, count_column) = (temp_matrix(count_row, count_column - 1) + temp_matrix(count_row + 1, count_column)) / 2;
  36.             %If value is on the bottom-left corner of the grid.
  37.             elseif count_row == row && count_column == column
  38.                 temp_update(count_row, count_column) = (temp_matrix(count_row, count_column - 1) + temp_matrix(count_row - 1, count_column)) / 2;
  39.             end
  40.         %If value is on the left or right side of the grid.
  41.         elseif count_column == 1 || count_column == column
  42.             %If value is on the left of the grid.
  43.             if count_column == 1
  44.                 temp_update(count_row, count_column) = (temp_matrix(count_row, count_column + 1) * 2 + temp_matrix(count_row + 1, count_column) + temp_matrix(count_row - 1, count_column)) / 4;
  45.             %If value is on the right of the grid.
  46.             elseif count_column == column
  47.                 temp_update(count_row, count_column) = (temp_matrix(count_row, count_column - 1) * 2 + temp_matrix(count_row + 1, count_column) + temp_matrix(count_row - 1, count_column)) / 4;
  48.             end
  49.         %If value is on the top or bottom of the grid.
  50.         elseif count_row == 1 || count_row == row
  51.             %If value is on the top of grid.
  52.             if count_row == 1
  53.                 temp_update(count_row, count_column) =(temp_matrix(count_row + 1, count_column) * 2 + temp_matrix(count_row, count_column + 1) + temp_matrix(count_row, count_column - 1)) / 4;
  54.             %If value is on the bottom of grid.
  55.             elseif count_row == row
  56.                 temp_update(count_row, count_column) =(temp_matrix(count_row - 1, count_column) * 2 + temp_matrix(count_row, count_column + 1) + temp_matrix(count_row, count_column - 1)) / 4;
  57.             end
  58.         %If value is in the middle section of the grid.    
  59.         else
  60.             temp_update(count_row, count_column) = (temp_matrix(count_row - 1, count_column) + temp_matrix(count_row + 1, count_column) + temp_matrix(count_row, count_column - 1) + temp_matrix(count_row, count_column + 1)) / 4;
  61.         end
  62.     end %For (row) end
  63. end %For (column) end
  64.  
  65. % [temp_update] = plate_update_name([100,100,100,100;100,50,50,100;100,37.5,37.5,100;50,50,50,50])
  66. %
  67. % temp_update =
  68. %
  69. %   100.0000   75.0000   75.0000  100.0000
  70. %    75.0000   71.8750   71.8750   75.0000
  71. %    56.2500   59.3750   59.3750   56.2500
  72. %    75.0000   43.7500   43.7500   75.0000
  73. %
  74. % [temp_update] = plate_update_name([78,67,48; 65,53,40;60,55,46;55,45,37])
  75. %
  76. % temp_update =
  77. %
  78. %    66.0000   58.0000   53.5000
  79. %    61.0000   56.7500   50.0000
  80. %    57.5000   51.0000   46.7500
  81. %    52.5000   50.5000   45.5000
  82. %
  83. % [temp_update] = plate_update_name([100,100,100,99,97,91;100,78,67,60,48,41;99,65,61,53,40,36;97,60,55,46,30,27;96,55,45,37,29,19;92,50,42,29,25,23])
  84. %
  85. % temp_update =
  86. %
  87. %   100.0000   89.0000   83.2500   79.2500   71.5000   69.0000
  88. %    88.7500   83.0000   74.7500   66.7500   59.5000   55.7500
  89. %    81.7500   74.5000   60.0000   51.7500   41.7500   37.0000
  90. %    78.7500   68.0000   53.0000   43.7500   35.5000   28.7500
  91. %    74.7500   62.7500   47.2500   37.2500   27.7500   27.0000
  92. %    73.0000   61.0000   42.2500   35.2500   27.5000   22.0000
Advertisement
Add Comment
Please, Sign In to add comment