# On defining functions with func

**URL:** <https://community.freefem.org/t/on-defining-functions-with-func/2165>\
**Category:** General Discussion\
**Created:** [December 4, 2022, 11:04am UTC](https://community.freefem.org/t/on-defining-functions-with-func/2165 "2022-12-04T11:04:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yangshen](https://avatars.discourse-cdn.com/v4/letter/y/838e76/32.png) [@Yangshen](https://community.freefem.org/u/Yangshen)\
**Post date:** [December 4, 2022, 11:04am UTC](https://community.freefem.org/t/on-defining-functions-with-func/2165/1 "2022-12-04T11:04:47Z")

</div>

Hello everyone  
In the following example, some optimization algorithms in NLOPT are called to solve the extreme value problem. There is a piece of code that I don’t understand.  
int NN = 6;  
load"medit"  
mesh Th = square(NN,NN);  
func f1=1.;  
func f2=-1.;  
func g1=0.;  
func g2=0.1;  
int iter=0;  
int nadapt=10;  
real starttol=1e-6,bctol=6.e-12;  
fespace Vh(Th,P1);  
Vh oldu1,oldu2;  
varf BVF(v,w) = int2d(Th)(0.5_dx(v)dx(w) + 0.5dy(v)dy(w));  
varf LVF1(v,w) = int2d(Th)(f1w);  
varf LVF2(v,w) = int2d(Th)(f2_w);  
matrix A = BVF(Vh,Vh);  
real[int] b1 = LVF1(0,Vh),b2 = LVF2(0,Vh);

varf Vbord(v,w) = on(1,2,3,4,v=1);  
Vh In,Bord;  
Bord = Vbord(0,Vh,tgv=1);  
In = Bord ? 0:1;  
Vh gh1=Bord_g1,gh2=Bord_g2;  
func real J(real[int] &X)  
{  
Vh u1,u2;  
u1 = X(0:Vh.ndof-1);  
u2 = X(Vh.ndof:2_Vh.ndof-1);  
iter++;  
real[int] Au1 = A_u1, Au2 = A\*u2;  
Au1 -= b1;  
Au2 -= b2;  
real val = u1‘\*Au1 + u2’\*Au2;  
if(iter %10==9)  
plot(u1,u2,nbiso=30,fill=1,dim=3,cmm="adapt level " + al + " - iteration " + iter + " - J = " + val,value=1);  
cout\<\<“a=”\<\<u1\<\<endl;  
}

I don’t understand the X in line 24. I hope someone can give me some answers.

```
                                                                                                                                                     thank you

```

---

<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 4, 2022, 5:18pm UTC](https://community.freefem.org/t/on-defining-functions-with-func/2165/2 "2022-12-04T17:18:13Z")

</div>

What is your question precisely? I suppose the reason for the coding is that nlopt needs a `real[int]` as an input.

---

<div class="post-metadata">

**Author:** ![Yangshen](https://avatars.discourse-cdn.com/v4/letter/y/838e76/32.png) [@Yangshen](https://community.freefem.org/u/Yangshen)\
**Post date:** [December 5, 2022, 1:39am UTC](https://community.freefem.org/t/on-defining-functions-with-func/2165/3 "2022-12-05T01:39:56Z")

</div>

Thank you very much.This is an official example. FreeFEM++is used to call the optimization algorithm in NLOPT to solve the problem. This example is used to solve the maximum value of a function, and J is the target function to be minimized. The complete tutorial is here:

> <https://github.com/FreeFem/FreeFem-sources/blob/master/examples/plugin/VarIneq2.edp>

---

<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, 3:59pm UTC](https://community.freefem.org/t/on-defining-functions-with-func/2165/4 "2022-12-08T15:59:14Z")

</div>

`X` is just a pointer to the `real[int]` that is used as an argument to the `J` function.
