# Min/Max over a domain

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.

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.

Hello,
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.

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.

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

``````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.

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

``````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);
``````

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.