not_a_red_panda

Relaxation Example 3 MATLAB

Jun 11th, 2019
852
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. %Clear command window, saved variables, and any active figures.
  2. clc
  3. clear
  4. clf
  5. %Parameters
  6. phi1=200;
  7. phi2=-phi1;
  8. iterations=5000;
  9. %Outer length, width, and grid spacing in centimeters
  10. len=300;
  11. sep=5;
  12. K=len/sep+1;
  13.  
  14. %Initialize the array.
  15. phi=zeros(K,K);
  16.  
  17. %Position on grid of left end of outlet and its length
  18. X_out = 200/sep+1;
  19. Y_out = 1;
  20. out_len = 100/sep-1;
  21.  
  22. %Position on grid of upper end of inlet and its length
  23. X_in = 1;
  24. Y_in = 200/sep+1;
  25. in_len=100/sep-1;
  26.  
  27. %Impose boundary conditions
  28. for k = X_out:(X_out+out_len)
  29. phi(k,Y_out) = phi1;
  30. end
  31. for k = Y_in:(Y_in+in_len)
  32. phi(X_in,k) = phi2;
  33. end
  34. %Now carry out the relaxation.
  35. for iter=1:iterations
  36. for i=1:K
  37. for j=1:K
  38. if i==K || j==K || i==1 || j==1
  39. phi(i,j) = phi(i,j);
  40. elseif i==(X_out-1) && j<Y_in
  41. phi(i,j) = phi(i,j);
  42. elseif j==(Y_in-1) && i<X_out
  43. phi(i,j) = phi(i,j);
  44. else
  45. phi(i,j)=0.25*(phi(i+1,j)+phi(i-1,j)+phi(i,j+1)+phi(i,j-1));
  46. end
  47. end
  48. end
  49. end
  50.  
  51. %We only want to graph the flow in the pipe so we'll set the grid elements
  52. %outside the pipe to NaN
  53. for i=1:(X_out-2);
  54. for j=1:(Y_in-2);
  55. phi(i,j) = NaN;
  56. end
  57. end
  58. phi=flip(phi');
  59. [x y] = meshgrid(0:sep:len);
  60. [gx gy] = gradient(phi);
  61. startx = 200:10:300;
  62. starty = 100:-10:0;
  63. %Uncomment this if you want a contour and/or streamline plot
  64. % hold on
  65. % contourf(x,y,phi);
  66. % streamline(x,y,gx,gy,startx,starty)
  67. % streamline(x,y,-gx,-gy,startx, starty)
  68. % hold off
  69. mesh(x,y,phi)
  70. zlabel('Potential in m^2/s')
  71. xlabel('x')
  72. ylabel('y')
Advertisement
Add Comment
Please, Sign In to add comment