# Min/Max over a domain

**URL:** <https://community.freefem.org/t/min-max-over-a-domain/2013>\
**Category:** General Discussion\
**Created:** [September 18, 2022, 11:17am UTC](https://community.freefem.org/t/min-max-over-a-domain/2013 "2022-09-18T11:17:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrbenson](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mrbenson](https://community.freefem.org/u/mrbenson)\
**Post date:** [September 18, 2022, 11:17am UTC](https://community.freefem.org/t/min-max-over-a-domain/2013/1 "2022-09-18T11:17:45Z")

</div>

Hello,

Is there an easy way to get the min/max values of a scalar field on a specific sub-domain defined by a list of tags ? Something like int1d : Min(Th,tags)(u) ?

Thanks.

---

<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:** [September 20, 2022, 3:46pm UTC](https://community.freefem.org/t/min-max-over-a-domain/2013/2 "2022-09-20T15:46:08Z")

</div>

Ok, this no trivial,  
because min, max are not integral, and le max of P2 function is on the node.

So the compute this first build the sub-domain with trunc., when build a finite element function on this mesh interpolate and take the min, max of dof array.

---

<div class="post-metadata">

**Author:** ![mrbenson](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mrbenson](https://community.freefem.org/u/mrbenson)\
**Post date:** [September 20, 2022, 7:39pm UTC](https://community.freefem.org/t/min-max-over-a-domain/2013/3 "2022-09-20T19:39:46Z")

</div>

Hello,  
Thanks for your answer.  
Unfortunately trunc seems not working using a edge label ; I always get the entire mesh (i check using the restriction mesh on the border label through nbe and nt (must be 0)).  
Any idea ?  
Moreover I need to get the min/max coordinates reached…

Thansk for helping Pr.

---

<div class="post-metadata">

**Author:** ![mrbenson](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mrbenson](https://community.freefem.org/u/mrbenson)\
**Post date:** [November 28, 2022, 9:35pm UTC](https://community.freefem.org/t/min-max-over-a-domain/2013/4 "2022-11-28T21:35:27Z")

</div>

It’ll be great to have a argmin/argmax function which returns a double[4] with the min/max and the coordinates x,y,z and with a int[int] as argument.

---

<div class="post-metadata">

**Author:** ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)\
**Post date:** [December 8, 2022, 4:12pm UTC](https://community.freefem.org/t/min-max-over-a-domain/2013/5 "2022-12-08T16:12:57Z")

</div>

One way to get an approximation of the min/max based on nodal values would be to do the following:

```auto
fespace Vh(Th,P2);
Vh u, filteru, restu;
// solve problem for u
// define filteru to be a binary function that is 1 in desired subdomain or 0 outside
restu = u*filteru;
real minu = restu[].min;
real maxu = restu[].max;

```

I still think it would be better to use the approach suggested by Frederic.

---

<div class="post-metadata">

**Author:** ![vlaude](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/vlaude/32/939_2.png) [@vlaude](https://community.freefem.org/u/vlaude)\
**Post date:** [December 8, 2022, 5:21pm UTC](https://community.freefem.org/t/min-max-over-a-domain/2013/6 "2022-12-08T17:21:38Z")

</div>

Still not a solution but an approximation, you can get the argmin/argmax on the dof array as in the following example.

```auto
int numnode = 50;
mesh Th = square(numnode,numnode);
fespace Vh(Th,P2);
Vh f=sin(pi*sqrt(x))*sin(pi*y*y)-x+y*y;
Vh posx=x;
Vh posy=y;
real minu = f[].min;
int imin = f[].imin;
real maxu = f[].max;
int imax = f[].imax;
cout << "Mininum: " << minu << " at (" << posx[][imin] << "," << posy[][imin] << ")" << endl;
cout << "Maximum: " << maxu << " at (" << posx[][imax] << "," << posy[][imax] << ")" << endl;
plot(f,fill=true,value=true,wait=true);

```

---

<div class="post-metadata">

**Author:** ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)\
**Post date:** [December 9, 2022, 10:53am UTC](https://community.freefem.org/t/min-max-over-a-domain/2013/7 "2022-12-09T10:53:01Z")

</div>

The true max/min could be found using `nloptNelderMead`, for which you need a `func` that return the value of the function at a given (x,y) coordinate.
