# How to compute gradient of each node in finite element method?

I have a problem with computing gradient of each node in finite element method. I can get the value of each node. But how can I get the gradient?

I know $$u = \sum u_i \phi_i$$ where $$\phi_i$$ are the test functions. So $$\frac{\partial u}{\partial x} = \sum u_i \frac{\partial \phi_i}{\partial x}$$. But if it is one-dimensional problem and I use linear basis function. Then I think the summary of $$\frac{\partial \phi_i}{\partial x}$$ at the point $$u_i$$ is 0 because the derivative of $$\phi_i$$ is negative of each other on both sides. I am very confused.

• Why do you need to compute the gradient at a node? – Chenna K Jan 10 at 14:18
• @ChennaK I am using adjoint method. I have an object function $J$. Its derivative depends on the gradient of primal problem solution $T$ and dual problem solution $\phi$. So when I using FEM solving $T$ and $\phi$. I also need to compute their gradients. – Jimmy5haw Jan 11 at 11:20

Suppose that we have a piecewise-linear one-dimensional basis. Technically the gradient has no point value defined at the finite element nodes because the derivative is discontinuous at the nodes as you've noticed.

However, there are several techniques to approximate the gradient. One example is based on projecting the weak derivative onto a piecewise-linear basis. Let me try to explain it with the following example:

Above is a finite element solution to the problem $$-u''=1$$, $$u(0)=u(1)=0$$. Because the solution is piecewise-linear, the derivative is piecewise constant:

This derivative has no unique value at the nodes because it's discontinuous. However, we can perform $$L^2$$-projection of the above function onto the piecewise-linear basis to arrive at:

Let $$u_h \in V_h$$ denote the piecewise-linear solution. In practice, the above $$L^2$$-projection means that we solve for $$w_h \in V_h$$ satisfying $$\int_0^1 w_h v_h \,\mathrm{d}x = \int_0^1 \frac{\partial u_h}{\partial x} v_h \,\mathrm{d}x$$ for every $$v_h \in V_h$$. This reduces into a matrix system $$M \boldsymbol{w} = C \boldsymbol{u}$$ where $$M$$ is the mass matrix, $$C_{ij} = \int_0^1 \frac{\partial \phi_j}{\partial x} \phi_i \,\mathrm{d}x$$ and $$\boldsymbol{u}$$ is the original solution vector.

• Thank you for your answer. But I still have a problem. I am solving a 2D transient heat transfer equation. The initial condition is a constant. So at the time 0, gradient such as $\frac{\partial T}{\partial x}$ is zero. But if I use your method to compute gradient. $M^{-1}C$ is not a zero matrix. So $M^{-1}Cu$ or $M^{-1}CT$ at time 0 is not zero. I wonder how to handle with it? Thank you. – Jimmy5haw Jan 11 at 11:04
• If $T$ is constant then $C T = 0$ so therefore $M^{-1} C T = 0$. – knl Jan 11 at 13:14
• Let me add that this technique is dimension-independent. Just change the integrals accordingly. – knl Jan 11 at 13:21
• Can we also lump the mass matrix here? I've heard that one can and it seems to me that from the discussion [here][1] that one can. [1]: scicomp.stackexchange.com/questions/32805/… – Nachiket Feb 3 at 5:18
• Yeah, I think it will work. In some special cases it may have an effect to the quality of the approximation but in most cases it will probably work just fine. – knl Feb 4 at 13:20