# Issues with Implementing a Nonlinear Boundary Condition in FreeFem

**URL:** https://community.freefem.org/t/issues-with-implementing-a-nonlinear-boundary-condition-in-freefem/3648
**Category:** General Discussion
**Created:** [December 18, 2024, 12:43pm UTC](https://community.freefem.org/t/issues-with-implementing-a-nonlinear-boundary-condition-in-freefem/3648 "2024-12-18T12:43:25Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![romie090](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/romie090/32/2743_2.png) [@romie090](https://community.freefem.org/u/romie090)
#### Post date: [December 18, 2024, 12:43pm UTC](https://community.freefem.org/t/issues-with-implementing-a-nonlinear-boundary-condition-in-freefem/3648/1 "2024-12-18T12:43:25Z")

</div>

Hello Everyone,

I am currently working on a simulation using FreeFem, and I am facing difficulties in implementing a nonlinear boundary condition for a PDE I am solving. Specifically, I am trying to impose a boundary condition that is dependent on the solution to the equation at the boundary, which makes the problem nonlinear.

Here’s the outline of the problem:

1. I am solving a second-order elliptic PDE for an unknown function `u` in a 2D domain using the finite element method.
2. The boundary condition I need to implement is of the form:  
`∂u/∂n = f(u)` on the boundary, where `f(u)` is a known nonlinear function of the solution `u`.

In my current approach, I am trying to define the boundary condition using the `on` keyword for the boundary and then applying the nonlinear term. However, I am unsure about the correct syntax and handling of the nonlinear boundary condition, especially in terms of how to update the boundary condition during each iteration of the solver.

> [@The impedance boundary condition in the FreeFem++](https://community.freefem.org/t/the-impedance-boundary-condition-in-the-freefem/2681):
>
> Dear all, I am trying to make the impedance boundary condition in the FreeFem++ to simulate the acoustic wave. I have found it a little puzzle when I solve the linear Euler equations for (ux,uy,p,rho) and use such a boundary condition on the outlet like p = i \* omega \* Z \* ux . I add int1d(th, outlet)(1e30 \* (p - i \* omega \* Z \* ux) \* vx) to my problem ( vx is my test function), but the results are not OK. Is there any suggestion or example for such problem? Thanks a lot.

I have tried a few variations of the boundary condition and reformulations of the problem, but I am not getting the expected results. My guess is that I might be misinterpreting how to handle nonlinear [tableau](https://www.igmguru.com/data-science-bi/tableau-training) boundary conditions within FreeFem.

Has anyone worked with nonlinear boundary conditions in FreeFem before? Any suggestions or examples would be greatly appreciated. Additionally, if there’s a better approach for implementing this kind of condition in FreeFem, I would love to hear about it.

Thank you in advance for your help!

Regards  
Romie

---

<div class="post-metadata">

### Author: ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)
#### Post date: [December 19, 2024, 8:55am UTC](https://community.freefem.org/t/issues-with-implementing-a-nonlinear-boundary-condition-in-freefem/3648/2 "2024-12-19T08:55:03Z")

</div>

Hello,  
Here is a basic example for a problem with nonhomogeneous Neuman BC:  
u-\Delta u=f in \Omega,  
\partial u/\partial n=g on \partial\Omega,  
with \Omega=(-1,1)\times(-1,1), f=xy, g=|x+y|-1 and the exact solution u\_{ex}=xy.  
The variational formulation is  
\int\_\Omega uv+\int\_\Omega\nabla u\cdot\nabla v-\int\_\Omega fv-\int\_{\partial\Omega}gv=0, for all test functions v.

```auto
int nn=30;
mesh Th=square(nn,nn,[2.*x-1.,2.*y-1.]);

fespace Vh(Th,P1);
Vh u,v;// u: unknown, v: test function
Vh f,g;// f: rhs, g:nonhommogeneous Neumann BC
f=x*y;
g=abs(x+y)-1.;

Vh uex;
uex=x*y;

solve laplacenhN(u,v)=
           int2d(Th)(u*v)
           +int2d(Th)(dx(u)*dx(v)+dy(u)*dy(v))
           -int2d(Th)(f*v)
           -int1d(Th)(g*v)
           ;

real error=sqrt(int2d(Th)((u-uex)^2));
cout << "error " << error << endl;
plot(u,wait=1,value =1);

```

Then for your problem you could consider a sequence of approximations u\_k,  
and solve the problem for u\_{k+1} with boundary condition \partial u\_{k+1}/\partial n=F(u\_k). I hope that F is nonincreasing in order to have a well-posed problem.  
If you want to use the Newton method you have to solve  
\partial u\_{k+1}/\partial n=F(u\_k)+F'(u\_k)(u\_{k+1}-u\_k)

See also the formulation of Robin BC in  
[https://doc.freefem.org/documentation/finite-element.html](https://doc.freefem.org/documentation/finite-element.html)  
section “Weak Form and Boundary Condition”
