# Lagrange Multipliers and Adaptive Meshing

**URL:** <https://community.freefem.org/t/lagrange-multipliers-and-adaptive-meshing/582>\
**Category:** General Discussion\
**Created:** [September 16, 2020, 12:14am UTC](https://community.freefem.org/t/lagrange-multipliers-and-adaptive-meshing/582 "2020-09-16T00:14:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevinmo08](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/kevinmo08/32/217_2.png) [@kevinmo08](https://community.freefem.org/u/kevinmo08)\
**Post date:** [September 16, 2020, 12:14am UTC](https://community.freefem.org/t/lagrange-multipliers-and-adaptive-meshing/582/1 "2020-09-16T00:14:32Z")

</div>

Hi all,

I am confused as to how to implement adaptive meshing for a lagrange multiplier problem. The code example for the lagrange multiplier is here [https://doc.freefem.org/examples/finite-element.html#examplelagrangemultipliers](https://doc.freefem.org/examples/finite-element.html#examplelagrangemultipliers)

Most examples, and in the documentation it describes how to use adaptmesh using a loop.

```
// Adaptmesh loop
for (int i = 0; i < 4; i++){
   Problem;
   Th = adaptmesh(Th, function_to_adapt_by, err=error);
   error = error/2;
}

```

Where the “Problem” is defined earlier, like the poisson problem [https://doc.freefem.org/examples/mesh-generation.html#mesh-adaptation-for-the-poisson-s-problem](https://doc.freefem.org/examples/mesh-generation.html#mesh-adaptation-for-the-poisson-s-problem)

But for the Lagrange multiplier examples, there is not a problem variable that we set. How would I go about using the block matrices we make the lagrange example work with this?

Best,  
Kevin

Example Lagrange Code

```
// Parameters
func f = 1 + x - y;

// Mesh
mesh Th = square(10, 10);

// Fespace
fespace Vh(Th, P1);
int n = Vh.ndof;
int n1 = n+1;
Vh uh, vh;

// Problem
varf va (uh, vh)
   = int2d(Th)(
      dx(uh)*dx(vh)
      + dy(uh)*dy(vh)
)
;

varf vL (uh, vh) = int2d(Th)(f*vh);
varf vb (uh, vh) = int2d(Th)(1.*vh);

matrix A = va(Vh, Vh);
real[int] b = vL(0, Vh);
real[int] B = vb(0, Vh);
// Block matrix
matrix AA = [[ A, B], [B', 0] ];
set(AA, solver=sparsesolver);

real[int] bb(n+1), xx(n+1), b1(1), l(1);
b1 = 0;
// Builds the right hand side block
bb = [b, b1];

// Solve
xx = AA^-1 * bb;

// Set values
[uh[],l] = xx;

// Display
cout << " l = " << l(0) << " , b(u, 1) =" << B'*uh[] << endl;

// Plot
plot(uh);
```

---

<div class="post-metadata">

**Author:** ![julienG](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/julieng/32/222_2.png) [@julienG](https://community.freefem.org/u/julienG)\
**Post date:** [September 16, 2020, 4:53am UTC](https://community.freefem.org/t/lagrange-multipliers-and-adaptive-meshing/582/2 "2020-09-16T04:53:07Z")

</div>

The equivalent of `problem` here is in matrix construction+the execution `xx = AA^-1 * bb;`  
So I guess you should do the mesh adaptation after this line.  
I guess you start the loop before first matrix construction: `matrix A = va(Vh, Vh)`.  
Indeed we can suppose the number of elements change after the mesh adaptation, I think the matrices have to be re-allocated …

check this [Laplace-lagrange-mult.edp](https://community.freefem.org/uploads/short-url/ehAyU1Gy9Pu9MJkJZZgq4hPvVjX.edp) (1.3 KB)
