# Derivative of P1 function

**URL:** <https://community.freefem.org/t/derivative-of-p1-function/3849>\
**Category:** General Discussion\
**Created:** [April 10, 2025, 5:46am UTC](https://community.freefem.org/t/derivative-of-p1-function/3849 "2025-04-10T05:46:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Takehiro](https://avatars.discourse-cdn.com/v4/letter/t/3ec8ea/32.png) [@Takehiro](https://community.freefem.org/u/Takehiro)\
**Post date:** [April 10, 2025, 5:46am UTC](https://community.freefem.org/t/derivative-of-p1-function/3849/1 "2025-04-10T05:46:32Z")

</div>

Dear FreeFem users,

I would like to ask please.  
According to the documentation,

```auto
mesh Th=square(5,5);
fespace Vh(Th,P1);
Vh F = x*x;
Vh f = dx(F);

```

then the function f is returned as a P1 function.  
For example, for node i, which is a vertex with multiple triangles, how is the value at node i of dx(F) calculated?  
Derivatives are obtained on each element, but how can it be done at the nodes?

More simply, how is dx(F) computed at node i in the 1D case?  
From my experiments, it seems to be determined by dx(F)(x\_i)= (F(x\_{i+1})-F(x\_i))/(x\_{i+1}-x\_i).  
It does not seem to be (F(x\_{i+1})-F(x\_{i-1}))/(x\_{i+1}-x\_{i-1}).

Thank you in advance,

Takehiro Matsumoto.

---

<div class="post-metadata">

**Author:** ![Takehiro](https://avatars.discourse-cdn.com/v4/letter/t/3ec8ea/32.png) [@Takehiro](https://community.freefem.org/u/Takehiro)\
**Post date:** [April 10, 2025, 6:20am UTC](https://community.freefem.org/t/derivative-of-p1-function/3849/2 "2025-04-10T06:20:53Z")

</div>

```auto
mesh Th=square(4,4); 
fespace Vh(Th,P1);
plot(Th,wait=1,ps="Th.eps");
Vh F = x*x;
Vh f = dx(F);
Vh fIdeal = 2*x;

{
  ofstream file("0calc_dx.txt");
  file << "F" << endl;
  file << F[] << endl;
  file << "f" << endl;
  file << f[] << endl;
  file << "fIdeal" << endl;
  file << fIdeal[] << endl;
}

```

Running this code, we can get the following result

```auto
F
25	
	  0	0.0625	0.25	0.5625 1
	  0	0.0625	0.25	0.5625 1
	  0	0.0625	0.25	0.5625 1
	  0	0.0625	0.25	0.5625 1
	  0	0.0625	0.25	0.5625 1
	
f
25	
	0.25	0.75	1.25	1.75	1.75
	0.25	0.75	1.25	1.75	1.75
	0.25	0.75	1.25	1.75	1.75
	0.25	0.75	1.25	1.75	1.75
	0.25	0.75	1.25	1.75	1.75
	
fIdeal
25	
	  0	0.5 1	1.5 2
	  0	0.5 1	1.5 2
	  0	0.5 1	1.5 2
	  0	0.5 1	1.5 2
	  0	0.5 1	1.5 2

```

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [April 10, 2025, 2:56pm UTC](https://community.freefem.org/t/derivative-of-p1-function/3849/3 "2025-04-10T14:56:10Z")

</div>

Remember freefem is not magic,

`dx(F)` is discontinus, so the value of `dx(F)(x,y)` at discontinuity point  
not well defined, the algorithm find a element containing (x,y) dans give the value in this triangle, you can compute a L^2 projection of dx(F) on  
P1 `fespace `if you want, but be careful.

---

<div class="post-metadata">

**Author:** ![Takehiro](https://avatars.discourse-cdn.com/v4/letter/t/3ec8ea/32.png) [@Takehiro](https://community.freefem.org/u/Takehiro)\
**Post date:** [April 11, 2025, 4:33am UTC](https://community.freefem.org/t/derivative-of-p1-function/3849/4 "2025-04-11T04:33:57Z")

</div>

Thank you so much.  
Your answer have helped me a lot!
