# Lagrange multipliers

**URL:** <https://community.freefem.org/t/lagrange-multipliers/1541>\
**Category:** General Discussion\
**Created:** [February 21, 2022, 3:02pm UTC](https://community.freefem.org/t/lagrange-multipliers/1541 "2022-02-21T15:02:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![larismartins](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/larismartins/32/2712_2.png) [@larismartins](https://community.freefem.org/u/larismartins)\
**Post date:** [February 21, 2022, 3:02pm UTC](https://community.freefem.org/t/lagrange-multipliers/1541/1 "2022-02-21T15:02:15Z")

</div>

Is there an example to solve the Laplace equantion with Neumann boundary conditions using lagrange multipliers but in a scenario that it’s not 1D? I did the follwing but I’m getting errors.

```auto
fespace VhK(Th[i], Pk);
  fespace V0K(Th[i], P0);
  VhK etaK, etaf, phiK, varphi;
  V0K vj, vf;
  int ndofVhK = VhK.ndof;
  int ndofV0K = V0K.ndof;
 
 varf ah(etaf,varphi)
    = int2d(Th[i])(kappa*grad(etaf)'*grad(varphi));

  varf fh(etaf,varphi) 
   = int2d(Th[i])(f * varphi);
 
  varf bh(vf, varphi) 
   = int2d(Th[i])(1.*varphi);

  matrix A = ah(VhK,VhK); //stiffness matrix

  //real[int] B = bh(0,VhK);
  matrix B = bh(V0K,VhK);

  real[int] F(ndofVhK);
  F = fh(0,VhK); 

  //the block matrix
  matrix AA = [[ A , B] ,
                [B', 0] ] ;

  real[int] bb(ndofVhK+ndofV0K),xx(ndofVhK+ndofV0K),b1(ndofV0K),l(ndofV0K);
  b1=0;

  // build the block rhs 
  bb = [F, b1];
  
  set(AA,solver=UMFPACK);
  xx = AA^-1*bb; // solve the linear system

  [etaf[],l] = xx; // set the value

  plot(etaf,wait=1, fill=1);

```

---

<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:** [February 21, 2022, 4:54pm UTC](https://community.freefem.org/t/lagrange-multipliers/1541/3 "2022-02-21T16:54:39Z")

</div>

Before to build a FreeFem++ script write the mathematical formulation.

In yours exemple your matrix B == 0 because bh is a linear form.  
To help you I need the Lagrangian with find the matrix AA

if the use the vector B it is OK , the number of unknown is ndofVhK+1 because a vector is see a a matrix with one column.

---

<div class="post-metadata">

**Author:** ![larismartins](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/larismartins/32/2712_2.png) [@larismartins](https://community.freefem.org/u/larismartins)\
**Post date:** [February 21, 2022, 7:22pm UTC](https://community.freefem.org/t/lagrange-multipliers/1541/4 "2022-02-21T19:22:13Z")

</div>

Those are the problems I want to implement on freefem

![formulation](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/6/6d420bb37c90c9e2ff80fc14f5b227a48fa48581.png)

---

<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:** [February 22, 2022, 7:57pm UTC](https://community.freefem.org/t/lagrange-multipliers/1541/5 "2022-02-22T19:57:33Z")

</div>

I are no problem, this like

```auto
mesh Th=square(10,10);
macro grad(u) [dx(u),dy(u)]//
fespace VhK(Th, P2);
fespace V0K(Th, P0);
VhK eta,phi;
V0K v,psi;
func f= 16.*x^(1-x)*x^(1-y)*y; 
solve Pb([eta,v],[phi,psi]) =
int2d(Th)( grad(eta)'*grad(phi) + v*psi + eta*phi )
- int2d(Th)(f*phi) + on(1,2,3,4, eta = 0);
plot(eta,wait=1);
plot(v,wait=1);

```

---

<div class="post-metadata">

**Author:** ![larismartins](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/larismartins/32/2712_2.png) [@larismartins](https://community.freefem.org/u/larismartins)\
**Post date:** [February 23, 2022, 10:01pm UTC](https://community.freefem.org/t/lagrange-multipliers/1541/6 "2022-02-23T22:01:31Z")

</div>

So, I have a global mesh

```auto
int globalH = 2;
mesh P = square(globalH,globalH);

int NbTriangles = P.nt; //number of triangles

```

and in each triangle I created a local mesh, currently I’m not refining the local mesh, so it’s only one triangle

```auto
mesh[int] Th(NbTriangles);
int localh = 1;

for(int i = 0; i < NbTriangles; i++){
 Th[i] = trunc(P, abs(u0 -i) < 1e-5, split=localh);

```

and used this to number the edges

```auto
 fespace Lambda0(Th[i], P0edge);
 int[int,int] edgeNumberLocal(Th[i].nt, Lambda0.ndofK);
 for (int t=0; t < Th[i].nt; ++t){
  for(int i=0; i< 3; ++i){ // in 2d 
    int nu = Lambda0(t,i) ;
    edgeNumberLocal(t,i) = nu;

```

now in each local mesh I want to compute the problems that I sent you. I want so solve for eta\_j^K and eta\_f^K. I did what you suggested

```auto
 fespace VhK(Th[i], Pk);
  fespace V0K(Th[i], P0);
  VhK etaK, etafK, phiK, varphi;
  V0K vj, vf;
  int ndofVhK = VhK.ndof;
  int ndofV0K = V0K.ndof;

  solve localProblemetafK([etafK, vf], [varphi, phiK])
    = int2d(Th[i])(kappa*grad(etafK)'*grad(varphi) + vf*phiK + etafK*varphi)
      - int2d(Th[i])(f*varphi) + on(0,1,2, etafK = 0);

  plot(etafK, wait=1);
  plot(vf, wait = 1);

```

and when I do this, I get the following error

```auto
 current line = 186
Internal error : Methode de Galerkine (a faire)
	line :11400, in file problem.cpp
Internal error : Methode de Galerkine (a faire)
	line :11400, in file problem.cpp
 err code 7 , mpirank 0

```

---

<div class="post-metadata">

**Author:** ![larismartins](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/larismartins/32/2712_2.png) [@larismartins](https://community.freefem.org/u/larismartins)\
**Post date:** [February 23, 2022, 10:15pm UTC](https://community.freefem.org/t/lagrange-multipliers/1541/7 "2022-02-23T22:15:19Z")

</div>

Also, is it right to do this?

```auto
 int[int] labelChange = [0,0, 1,1, 2,2];
  Th[i] = change(Th[i], label = labelChange);

```

---

<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:** [March 1, 2022, 8:46pm UTC](https://community.freefem.org/t/lagrange-multipliers/1541/8 "2022-03-01T20:46:29Z")

</div>

Sorry, some fonction are no implemented.  
I will try to do rapidly, but ii is not sURE;
