Guest User

Untitled

a guest
Mar 18th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. % Poisson FEM based on MIT lectures by Qiqi Wang
  2. % Export the mesh data [p,e,t] in the wokspace
  3.  
  4. % Compute the gradient of the basis functions
  5. grad = zeros(size(t,2),3,2);
  6.  
  7. % loop over number of triangles
  8. for k = 1: size(t,2)
  9. % Store the coordinates of the triangles
  10. p1 = p(:,t(1,k));
  11. p2 = p(:,t(2,k));
  12. p3 = p(:,t(3,k));
  13.  
  14. % Compute the gradient
  15. % the coefficient matrix: differences of coordinates
  16. C1 = [(p1-p2)';(p1-p3)'];
  17. % The RHS. Note that the basis function has the value 1 at p1 (say)
  18. r1 = [1;1];
  19. grad(k,1,:) = C1 r1;
  20. % Do it for the other nodes in a similar fashion
  21. C2 = [(p2-p1)';(p2-p3)'];
  22. grad(k,2,:) = C2 r1;
  23. C3 = [(p3-p1)';(p3-p2)'];
  24. grad(k,3,:) = C3 r1;
  25. end
  26. % Define the coefficient matrix
  27. C = zeros(size(p,2));
  28. % Note: the size of A must be the number of nodes. Ideally...
  29. % number of interior nodes.
  30.  
  31. % Compute the area of each finite elements (triangles)
  32. % loop over number of triangles
  33. A = zeros (size(t,2),1);
  34. for k = 1:size(t,2)
  35. % Store the coordinates of the triangles
  36. p1 = p(:,t(1,k));
  37. p2 = p(:,t(2,k));
  38. p3 = p(:,t(3,k));
  39. % Area(k) = det (coefficient matrix of basis) /2
  40. A(k) = abs(det([(p1-p2)';(p1-p3)']))/2;
  41. end
  42.  
  43. % Compute the stiffness matrix
  44. for k = 1:size(t,2)
  45. cr = t(1:3,k)';
  46. for i = 1:3
  47. for j = 1:3
  48. C(cr(i) , cr(j)) = C(cr(i) , cr(j)) +...
  49. A(k) * dot(grad(k,i,:),grad(k,j,:));
  50. end
  51. end
  52. end
  53.  
  54. % Compute the global load vector (RHS)
  55. b = zeros(size(p,2),1);
  56. % Define the RHS function
  57. f=1;
  58. for i=1:size(t,2)
  59. xcell=num2cell(t(1:3,i));
  60. [i1,i2,i3] = deal(xcell{:});
  61. % Compute the centroid of the triangle
  62. centroid = (p(:,i1)+p(:,i2)+p(:,i3))/3;
  63. % The integral is appoximated by f(centroid)*area/3
  64. b(i1) = b(i1)+A(i)/3;
  65. b(i2) = b(i2)+A(i)/3;
  66. b(i3) = b(i3)+A(i)/3;
  67. end
  68.  
  69. M=size(p,2);
  70. % Define the solution matrix
  71. sol = diag(zeros(M));
  72. % Get the number of interior nodes
  73. dof=setdiff(1:M,e);
  74. % Get the solution (Finally !)
  75. sol(dof) = C(dof,dof)b(dof);
  76. % Plot the solution
  77. figure
  78. a=trisurf(t',p(1,:)',p(2,:)',sol);
Add Comment
Please, Sign In to add comment