not_a_red_panda

Relaxation Example 1 MATLAB

Jun 10th, 2019
1,381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. %Clear the command window and any stored variables
  2. clear
  3. clc
  4. %Boundary conditions
  5. PHI1 = 20;
  6. PHI2 = 0;
  7.  
  8. %Initial value of every grid point
  9. INITPHI = 5;
  10.  
  11. %Number of iterations
  12. iterations = 200;
  13.  
  14. %Side length of the square area being modeled in centimeters
  15. len=4;
  16.  
  17. %Desired grid point separation, in this case, 0.125cm
  18. sep=0.125;
  19. K=len/sep +1;
  20.  
  21. %Element of array corresopnding to the top left corner of the inner square
  22. %and its side length on the grid. This corner is 2cm to the right of the top left corner
  23. %of the outer square and 1 cm down, and the side length is 1cm.
  24. column_inner = ceil(K/2);
  25. row_inner = ceil(K/2);
  26. inner_len = 1/sep;
  27. %Initialize the array
  28. phi=zeros(K,K);
  29. for i = 1:K
  30. for j = 1:K
  31. phi(i,j)=INITPHI;
  32. end
  33. end
  34. size(phi)
  35.  
  36. %Impose boundary conditions
  37. for k=1:K
  38. phi(1,k) = PHI1;
  39. phi(K,k) = PHI1;
  40. phi(k,1) = PHI1;
  41. phi(k,K) = PHI1;
  42. end
  43. for k = 0:inner_len
  44. phi(row_inner+k, column_inner) = PHI2;
  45. phi(row_inner, column_inner+k) = PHI2;
  46. phi(row_inner+inner_len, column_inner+k) = PHI2;
  47. phi(row_inner+k, column_inner+inner_len) = PHI2;
  48. end
  49.  
  50. %Now we carry out the relaxation. We need to be able to tell the program to
  51. %leave the boundary points alone. We take advantage of three facts to do
  52. %this. First, we have only two prescribed boundary values, and second,
  53. %the potential function is continuous, and third, harmonic functions do not
  54. %have local maxima. The intermediate value theorem therefore tells us that
  55. %the potential on interior points is strictly between 0 and 20, so we tell
  56. %the program to simply not evaluate PHI(i,j) if PHI(i,j) is equal to 0 or
  57. %20.
  58. for iter = 1:iterations
  59. for i = 1:K
  60. for j = 1:K
  61. switch phi(i,j)
  62. case PHI1
  63. phi(i,j) = phi(i,j);
  64. case PHI2
  65. phi(i,j) = phi(i,j);
  66. otherwise
  67. phi(i,j) = 0.25*(phi(i+1,j)+phi(i-1,j)+phi(i,j+1)+phi(i,j-1));
  68. end
  69. end
  70. end
  71. end
  72. [x,y]=meshgrid(0:sep:len);
  73. %Comment out 'contourf' and uncomment meshc if you want a mesh plot, the
  74. %lines beginning with 'print' will save a high-quality image of the plot to
  75. %your home folder.
  76. contourf(x,y,phi)
  77. %meshc(x,y,phi)
  78. xlabel('x')
  79. ylabel('y')
  80. zlabel('Potential in V')
  81. %print('relaxationcontour1', '-dpng', '-r720')
  82. %print('relaxationmesh1', '-dpng', '-r720')
Advertisement
Add Comment
Please, Sign In to add comment