not_a_red_panda

Relaxation Example 2 MATLAB

Jun 10th, 2019
680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. clc
  2. clear
  3. %Parameters
  4. phi1 = 10000;
  5. phi2 = 0;
  6. initphi = 500;
  7. iterations = 10000;
  8.  
  9. %Length of energized plate in meters.
  10. len=100;
  11.  
  12. %The grid spacing must be less than or equal to the thickness of the
  13. %'bars' of the Faraday cage.
  14. sep=1;
  15. K=len/sep+1;
  16.  
  17. %Initialize the array. We need to pad the boundary with zeros for reasons
  18. %explained in the comment above the relaxation stage.
  19. phi=zeros(K+2,K+2);
  20. for i=2:K+1
  21. for j=2:K+1
  22. phi(i,j) = initphi;
  23. end
  24. end
  25. %Separation of cage bars
  26. bar_sep = 10;
  27.  
  28. %Impose boundary conditions
  29. for k = 2:K+1
  30. phi(k,2) = phi1;
  31. end
  32. %At 50cm to the right, place evenly spaced bars separated by bar_sep
  33. %Comment out this part to remove the cage.
  34. for k = 1:bar_sep/sep:K
  35. phi(k, 50/sep) = 0;
  36. end
  37.  
  38. %See the explanation in the code for Example 1. In this example, we also
  39. %have to contend with grid points that are at the edge of the array and
  40. %where no boundary condition is given. For these points, we average over the
  41. %three nearest points. To avoid a mess of conditionals, we accomplish this
  42. %by putting an extra 'layer' of zeros on the outside of the part of the
  43. %grid we're interested in, adding up all neighboring points, but then dividing by 3 instead of 4.
  44. %While this does mean a slight loss of accuracy at those points, the actual
  45. %physics that we're interested in is taking place in the interior of the grid.
  46. for iter = 1:iterations
  47. %Uncomment this if you want to be able to track the program's progress.
  48. % if mod(iter, 1000) == 0
  49. % iter
  50. % end
  51. for i = 2:K+1
  52. for j = 2:K+1
  53. switch phi(i,j)
  54. case phi1
  55. phi(i,j) = phi(i,j);
  56. case phi2
  57. phi(i,j) = phi(i,j);
  58. otherwise
  59. if i==2 || i==K+1 || j==2 || j==K+1
  60. phi(i,j) = 0.33*(phi(i+1,j)+phi(i-1,j)+phi(i,j+1)+phi(i,j-1));
  61. else
  62. phi(i,j) = 0.25*(phi(i+1,j)+phi(i-1,j)+phi(i,j+1)+phi(i,j-1));
  63. end
  64. end
  65. end
  66. end
  67. end
  68. %Now we get rid of the layer of zeros to make plotting easier.
  69. phi(:,1) = [];
  70. phi(:,K+1) = [];
  71. phi(1,:) = [];
  72. phi(K+1,:) = [];
  73. [x,y]=meshgrid(0:sep:len);
  74. %contourf(x,y,phi);
  75. meshc(x,y,phi);
Advertisement
Add Comment
Please, Sign In to add comment