# Obtain the shape function and its derivative at each element

**URL:** <https://community.freefem.org/t/obtain-the-shape-function-and-its-derivative-at-each-element/1755>\
**Category:** General Discussion\
**Created:** [May 19, 2022, 3:43am UTC](https://community.freefem.org/t/obtain-the-shape-function-and-its-derivative-at-each-element/1755 "2022-05-19T03:43:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hezl09](https://avatars.discourse-cdn.com/v4/letter/h/f07891/32.png) [@hezl09](https://community.freefem.org/u/hezl09)\
**Post date:** [May 19, 2022, 3:43am UTC](https://community.freefem.org/t/obtain-the-shape-function-and-its-derivative-at-each-element/1755/1 "2022-05-19T03:43:58Z")

</div>

Hello,

I would like to get the shape function (or characteristic function) together with its derivative at each element. Is there a quick way in freefem to do so? Many thanks!

Note: I understand that I can use varf to get the shape function of each node in an integral sense. However, what I need is the shape function of each element separately, without summing up the contribution from surrounding elements. For example, for the following mesh, I would like to get the shape function N1 (and its derivative) of element 0 on node 0, and the shape function N1 (and its derivative) of element 1 on node 0, instead of the sum of two N1 (and their derivatives) from two elements on node 0.

 ![1](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/c/c7a8b77de23fd0ac286bab60233067d0e117a55b.jpeg)

---

<div class="post-metadata">

**Author:** ![stergopilot](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@stergopilot](https://community.freefem.org/u/stergopilot)\
**Post date:** [September 15, 2025, 2:22pm UTC](https://community.freefem.org/t/obtain-the-shape-function-and-its-derivative-at-each-element/1755/2 "2025-09-15T14:22:48Z")

</div>

I would also like to calculate the element length in FreeFem++ using the following expression: h\_e=(\sum\_{i=1}^{n\_{e n}}\|\nabla N\_i\|)^{-1}, where N\_i is the shape function associated with node i. Does anyone know how to compute this in FreeFem++? Thanks.

---

<div class="post-metadata">

**Author:** ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)\
**Post date:** [September 16, 2025, 1:10pm UTC](https://community.freefem.org/t/obtain-the-shape-function-and-its-derivative-at-each-element/1755/3 "2025-09-16T13:10:05Z")

</div>

Here is a try. I don’t know if your norm \|\nabla N\_i\| means L^2 norm.

```auto
mesh Th=square(2,2);

fespace Vh(Th,P1);
fespace Ph(Th,P0);

Vh u;
Ph derxu,deryu;

real he=0.;
for (int i=0;i<Vh.ndof;i++){
 u[]=0.;
 u[][i]=1.;//basis function i
 derxu=dx(u);deryu=dy(u);
 real xx=Th(0).x,yy=Th(0).y;
 cout << "i=" << i << endl;
 cout << "u(xx,yy) " << u(xx,yy) << endl;
 cout << "dxu,dyu triangle 0: " << derxu[][0] << " " << deryu[][0] << endl;
 cout << "dxu,dyu triangle 1: " << derxu[][1] << " " << deryu[][1] << endl;

 real normi=sqrt(int2d(Th)(derxu^2+deryu^2));
 he=he+normi;
}
he=1./he;
cout << "he = " << he << endl;

```

---

<div class="post-metadata">

**Author:** ![stergopilot](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@stergopilot](https://community.freefem.org/u/stergopilot)\
**Post date:** [September 20, 2025, 7:07am UTC](https://community.freefem.org/t/obtain-the-shape-function-and-its-derivative-at-each-element/1755/4 "2025-09-20T07:07:43Z")

</div>

Thank you so much for the help!
