Download this page as a Matlab LiveScript
Table of contents
Let's say we want to compute the gradient of some function
Derivative
so what is ?
Using the chain rule we can derive
From which we identify the Jacobian.
Now the multidimensional case
Let's say we have a 2D element with coordinates
and we have a corresponding solution field
We now want to compute the partial, spatial derivatives of this field, i.e., the gradient:
where the gradient of the basis functions is this matrix
We shall introduce the concept using the linear triangle element as an example, starting with a map from the spatial space to the parametric.
The coordinates of a linear triangle in x- and y-coordinates can be interpolated using the linear basis function by a linear combination:
with
thus we have
Now in order to compute this gradient we need to utilize the Iso-Parametric map, compute the gradient of the basis function with respect to the parametric coordinates and map these to the spatial coordinates using the Jacobian.
We begin be deriving this relation, using the chain rule
It's straightforward to compute the - and -derivatives but we do not have access to the partial derivatives of with respect to , but vice versa. So using the chain rule we can compute
So we can get
Thus our gradient of the solution field is approximated using the derivatives of our basis functions
Now, for the linear triangle example we get specifically (using the map in (8))
or using
From multivariable calculus we know that the determinant of the Jacobian scales the area of the transformation.
We use the determinant of the Jacobian to scale the reference element area to the physical element by
so that we avoid integrating over the -coordinates
were is the parametric element and .
On a triangle element with nodes given by below, we want to compute the gradient of in the point given that the nodal values of are given below. Plot the directions of the gradient vector at that point.
P = [-1 0
2 0
1, 2]
u = [-1; 2; 3]
figure
patch(P(:,1),P(:,2),'w','FaceColor','interp','CData',u); hold on
plot(P(:,1),P(:,2),'ok','MarkerFaceColor','k','MarkerSize',8,'DisplayName','Nodes')
xlabel('x')
ylabel('y')
np = size(P,1);
xm = mean(P,1);
Tp = P+(xm-P)*0.2;
text(Tp(:,1),Tp(:,2),cellstr(num2str((1:np)')),'FontSize',14)
colorbar
plot(0.5,0.8,'*b')
Let's compute the corresponding parametric point by solving
syms xi eta phi
phi(xi,eta) = [1-xi-eta, xi, eta]
xy = phi*P
eq = [0.5,0.8] == xy;
eq.'
[xi1, eta1] = solve(eq,[xi, eta])
Now we can compute the gradient. We'll start with the Jacobian matrix.
Bh = [diff(phi,xi)
diff(phi,eta)]
We see that this is a constant matrix, independent of where we evaluate it in .
J = Bh*P
Then we can compute the gradient of the basis functions
B = J\Bh
And finally the gradient of the solution field
gradU = double(B*u)
gradU = 2x1
1
1
Let's plot the direction
quiver(0.5,0.8,gradU(1),gradU(2),0.8,'k')
Note that no matter which point in the triangle we look at the gradient is the same everywhere. This is always the case with linear triangles. The gradient is always constant.
On a quadrilateral element with nodes given by below, we want to compute the gradient of in the point given that the nodal values of are given below. Plot the directions of the gradient vector at that point.
clear;
P = [-1 -2
6 -2
7 8
-2 6];
u = [5
50
40
28]
figure
patch(P(:,1),P(:,2),'w','FaceColor','interp','CData',u); hold on
plot(P(:,1),P(:,2),'ok','MarkerFaceColor','k','MarkerSize',8,'DisplayName','Nodes')
xlabel('x')
ylabel('y')
np = size(P,1);
xm = mean(P,1);
Tp = P+(xm-P)*0.2;
text(Tp(:,1),Tp(:,2),cellstr(num2str((1:np)')),'FontSize',14)
axis equal
Let's compute the corresponding parametric point
phi = @(xi,eta)[(eta-1.0).*(xi-1.0),-xi.*(eta-1.0),eta.*xi,-eta.*(xi-1.0)]
syms xi eta
phi = sym(phi)
xy = simplify(phi*P).'
eq = [3;4] == xy
[xi1, eta1] = solve(eq,[xi, eta]);
xi1 = double(xi1)
xi1 = 2x1
-4.8458
0.5601
eta1 = double(eta1)
eta1 = 2x1
-3.5468
0.6579
We are getting two solutions, one is false. We know that , se we select the second solution.
xi1 = xi1(2)
xi1 = 0.5601
eta1 = eta1(2)
eta1 = 0.6579
Now we can compute the gradient. We'll start with the Jacobian matrix.
Bh(xi,eta) = [diff(phi,xi)
diff(phi,eta)]
J = Bh*P
We see that is not constant and as such the Jacobian is not constant. From now on we will evaluate everything using our parametric point.
Bh = matlabFunction(Bh)
Bh =
@(xi,eta)reshape([eta-1.0,xi-1.0,-eta+1.0,-xi,eta,xi,-eta,-xi+1.0],[2,4])
J = Bh(xi1,eta1)*P
J = 2x2
8.3158 1.3158
0.1203 9.1203
Then we can compute the gradient of the basis functions
B = J\Bh(xi1,eta1)
B = 2x4
-0.0336 0.0510 0.0695 -0.0869
-0.0478 -0.0621 0.0605 0.0494
And finally the gradient of the solution field
gradU = double(B*u)
gradU = 2x1
2.7281
0.4592
Let's plot the direction
quiver(3,4,gradU(1),gradU(2),0.8,'k')
Let's compute the gradient of the solution in the Gauss points
phi = matlabFunction(phi)
phi =
@(eta,xi)[(eta-1.0).*(xi-1.0),-xi.*(eta-1.0),eta.*xi,-eta.*(xi-1.0)]
[GP,GW] = GaussQuadrilateral(2);
So we're using 4 Gauss points here, for no particular reason.
This means we need to compute the Jacobian and everything in these 4 points and plot it. We will do this using a for loop.
for ig = 1:length(GW)
xi = GP(ig,1);
eta = GP(ig,2);
J=Bh(xi,eta)*P;
B = J\Bh(xi,eta);
gradU = B*u
xc = phi(xi,eta)*P;
plot(xc(1),xc(2),'kx')
quiver(xc(1),xc(2),gradU(1),gradU(2),0.6,'k')
end
gradU = 2x1
4.9952
2.2452
gradU = 2x1
1.8390
2.0288
gradU = 2x1
5.1587
-0.6270
gradU = 2x1
2.2956
-0.4544
Note how the gradient differs in each point. We are visualizing the solution field using patch and the built-in interpolate function, which is linear. In order to see this non-linear field better. We shall introduce a new way of visualizing a color field on these elements. We can use the basis functions to interpolate the solution field
N = 100;
xi = linspace(0,1,N);
eta = xi;
[Xi, Eta] = meshgrid(xi,eta);
U = phi(Xi(:),Eta(:))*u;
PP = phi(Xi(:),Eta(:))*P;
X = reshape(PP(:,1),N,N);
Y = reshape(PP(:,2),N,N);
U = reshape(U,N,N);
figure; axis equal tight; hold on
surf(X,Y,Y*0,U,'EdgeColor','none')
colormap jet
colorbar
contour(X,Y,U,5,'EdgeColor','k')
N = 20;
xi = linspace(0,1,N);
eta = xi;
[Xi, Eta] = meshgrid(xi,eta);
Xi = Xi(:); Eta = Eta(:);
PP = phi(Xi(:),Eta(:))*P;
GU = zeros(N^2,2);
for i = 1:N^2
xi = Xi(i);
eta = Eta(i);
J=Bh(xi,eta)*P;
B = J\Bh(xi,eta);
gradU = B*u;
GU(i,:) = gradU';
end
quiver(PP(:,1), PP(:,2), GU(:,1), GU(:,2), 0.6, 'k')
Here we plot the solution variable in evenly spaced parametric points and map these points to the real space. This shows that the interpolated solution field is curved. Plotting 5 black iso-curves, shows the non-linear nature of the interpolant.
The gradient of the solution is then computed times and plotted to further show the curved effect.
function [GP,GW] = GaussQuadrilateral(order)
%GaussQuadrilateral Gauss integration scheme for quadrilateral 2D element
% [x,y] in [0,1]
[gp,gw] = gauss(order,0,1);
[GPx,GPy] = meshgrid(gp,gp);
[Gw1,Gw2] = meshgrid(gw,gw);
GW = [Gw1(:), Gw2(:)];
GW = prod(GW,2);
GP = [GPx(:), GPy(:)];
end
function [x, w, A] = gauss(n, a, b)
%------------------------------------------------------------------------------
% gauss.m
%------------------------------------------------------------------------------
%
% Purpose:
%
% Generates abscissas and weigths on I = [ a, b ] (for Gaussian quadrature).
%
%
% Syntax:
%
% [x, w, A] = gauss(n, a, b);
%
%
% Input:
%
% n integer Number of quadrature points.
% a real Left endpoint of interval.
% b real Right endpoint of interval.
%
%
% Output:
%
% x real Quadrature points.
% w real Weigths.
% A real Area of domain.
%------------------------------------------------------------------------------
% 3-term recurrence coefficients:
n = 1:(n - 1);
beta = 1 ./ sqrt(4 - 1 ./ (n .* n));
% Jacobi matrix:
J = diag(beta, 1) + diag(beta, -1);
% Eigenvalue decomposition:
%
% e-values are used for abscissas, whereas e-vectors determine weights.
%
[V, D] = eig(J);
x = diag(D);
% Size of domain:
A = b - a;
% Change of interval:
%
% Initally we have I0 = [ -1, 1 ]; now one gets I = [ a, b ] instead.
%
% The abscissas are Legendre points.
%
if ~(a == -1 && b == 1)
x = 0.5 * A * x + 0.5 * (b + a);
end
% Weigths:
w = V(1, :) .* V(1, :);
end