Oct 06, 10 8:33 program1.m Page 1/2 Oct 06, 10 8:33 program1.m Page 2/2
% defines a scalar field U over a mesh (XY,Elm) quiver(EXY(:,1),EXY(:,2),scale*EGradU0(:,1),scale*EGradU0(:,2),0,b);
% calculates and visualizes the gradient of U % superpose the finite element gradient, in red color:
% hold on
quiver(EXY(:,1),EXY(:,2),scale*EGradU(:,1),scale*EGradU(:,2),0,r);
%*** get the mesh: circle view(2); % < 2D view (from above)
% [XY,Elm,PXY] = getcircle4(15,40,2.1); axis equal;
% nnod = size(XY,1);
% nelm = size(Elm,1); %*** visualize dU/dx (ie. EGrad(:,1)) in Figure 4:
figure(4); clf
%*** get the 2D mesh: square domain (1,1) x (1,1): ts4 = trisurf(Elm,XY(:,1),XY(:,2),0.*XY(:,1),EGradU(:,1));
[XY,Elm] = getdrapeau4(40,40,0); view(2);
nnod = size(XY,1); axis equal
nelm = size(Elm,1);
%*** define the scalar field U(x,y) = sin(pi/2*x)*cos(pi*y):
U = sin(pi/2*XY(:,1)).*cos(pi*XY(:,2));
%*** calculate the gradient on finite elements: constant on triangle:
EGradU = getgrad2D(XY,Elm,U);
%*** barycenters of triangles:
EXY = (XY(Elm(:,1),:)+XY(Elm(:,2),:)+XY(Elm(:,3),:))/3;
%*** to compare: exact gradient (on nodes):
GradU0 = [pi/2*cos(pi/2*XY(:,1)).*cos(pi*XY(:,2)),
pi*sin(pi/2*XY(:,1)).*sin(pi*XY(:,2))];
%*** to compare: exact gradient (on elements):
EGradU0 = [pi/2*cos(pi/2*EXY(:,1)).*cos(pi*EXY(:,2)),
pi*sin(pi/2*EXY(:,1)).*sin(pi*EXY(:,2))];
%
% subfunction: elmgrad2D
%*** parameters
% XY(3,2) in nodal coords of 1 triangle, XY(i,:) = [xi yi]
% EU(3,1) in nodal value (scalar field, U(i) = ui)
% EGrad(1,2) out gradient of U on the element, [dU/dx, dU/dy]
%
function EGrad = elmgrad2D(XY,U)
% calculate the basis functions on the 3 nodes:
A = [ones(3,1), XY];
A = inv(A);
% Phi_i(x) = ai + bi*x + ci*y:
ai = A(1,1); aj = A(1,2); ak = A(1,3);
bi = A(2,1); bj = A(2,2); bk = A(2,3);
ci = A(3,1); cj = A(3,2); ck = A(3,3);
% gradient "operator":
Grad = [ bi bj bk % < d/dx
ci cj ck]; % < d/dy
% apply the gradient "operator" on nodal values U:
EGrad = (Grad*U);
return;
Oct 06, 10 8:29 program2.m Page 1/2 Oct 06, 10 8:29 program2.m Page 2/2
% A rectangular beam (aspect,aspect) x (1,1), stored as a mesh (XY,Elm), figure(3); clf
% is bent into a circular arc of radius (radius), stored in (UXY,Elm). scale = 0.2;
% Based on the resulting displacement vector field U, quiver(XY(:,1),XY(:,2),scale*U(:,1),scale*U(:,2),0,b);
% assemble and visualize Green, Cauchy and/or Almansi strain tensors view(2);
% on every finite element triangle axis equal;
% title(vector field of displacements);
%*** get the mesh: circle
% [XY,Elm,PXY] = getcircle4(15,40,2.1);
% nnod = size(XY,1);
% nelm = size(Elm,1);