# How to solve Possion equation have a parameter h for different values of h using loop

**URL:** https://community.freefem.org/t/how-to-solve-possion-equation-have-a-parameter-h-for-different-values-of-h-using-loop/2716
**Category:** General Discussion
**Created:** [September 20, 2023, 3:55am UTC](https://community.freefem.org/t/how-to-solve-possion-equation-have-a-parameter-h-for-different-values-of-h-using-loop/2716 "2023-09-20T03:55:03Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![M\_Usman](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@M\_Usman](https://community.freefem.org/u/M_Usman)
#### Post date: [September 20, 2023, 3:55am UTC](https://community.freefem.org/t/how-to-solve-possion-equation-have-a-parameter-h-for-different-values-of-h-using-loop/2716/1 "2023-09-20T03:55:03Z")

</div>

Dear, I want to solve the Poisson equation involving h for different values of h given in Pram (in the following code). Then, I used a loop to pick each value from Pram as a value for h and then solve the problem. I want to compute AvgNu at different values of h. Kindly see my code and guide me to achieve the required results.

real Mesh=60;  
real ra=0.1;  
border C(t=0,1){x=0.5+ra_cos(2_pi_t);y=0.2_sqrt(3)+ra_sin(2_pi_t);}  
mesh Th=buildmesh(C(Mesh));  
plot(Th,wait=1);  
real Pram=[1,2,3];  
for(int i;i\<=3;i++)  
{  
real h=Pram[i];  
fespace Vh(Th,P2);  
func f= x_y;  
Vh U,V;  
problem Poisson(U,V,solver=LU)=  
int2d(Th)(dx(U)\*dx(V) + dy(U)\*dy(V))

- h_int2d(Th)( f_V )

- 

on(C,U=1) ;  
Poisson;  
real AvgNu;  
macro grad(u) [dx(u),dy(u)]//  
AvgNu[i]=-int1d(Th,C)((grad(U))'\*[N.x,N.y]);  
cout\<\<"Avg = "\<\<AvgNu[i]\<\<endl;  
}

---

<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: [September 20, 2023, 9:27am UTC](https://community.freefem.org/t/how-to-solve-possion-equation-have-a-parameter-h-for-different-values-of-h-using-loop/2716/2 "2023-09-20T09:27:22Z")

</div>

They is no problem

use the following writing:

```auto
problem Poisson(U,V,solver=LU)=
int2d(Th)(dx(U)*dx(V) + dy(U)*dy(V))
- int2d(Th)( h* f * V )
on(C,U=1) ;

```

in Poisson problem

---

<div class="post-metadata">

### Author: ![M\_Usman](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@M\_Usman](https://community.freefem.org/u/M_Usman)
#### Post date: [September 20, 2023, 12:51pm UTC](https://community.freefem.org/t/how-to-solve-possion-equation-have-a-parameter-h-for-different-values-of-h-using-loop/2716/3 "2023-09-20T12:51:00Z")

</div>

Dear Professor @frederichecht thanks for your response. I think you don’t understand my point. I want to solve the Poisson equation for different values of h using the loop. For example,  
for i=1, the code should solve the Poisson problem for h = 1 and compute the value for AvgNu,  
for i=2, the code should solve the Poisson problem for h = 2 and compute the value for AvgNu, and  
for i=3, the code should solve the Poisson problem for h = 3 and compute the value for AvgNu.

---

<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: [September 20, 2023, 1:26pm UTC](https://community.freefem.org/t/how-to-solve-possion-equation-have-a-parameter-h-for-different-values-of-h-using-loop/2716/4 "2023-09-20T13:26:12Z")

</div>

No , for( i = 0; i\<3; ++i) read the doc please.

---

<div class="post-metadata">

### Author: ![M\_Usman](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@M\_Usman](https://community.freefem.org/u/M_Usman)
#### Post date: [September 20, 2023, 3:41pm UTC](https://community.freefem.org/t/how-to-solve-possion-equation-have-a-parameter-h-for-different-values-of-h-using-loop/2716/5 "2023-09-20T15:41:49Z")

</div>

That means we can’t solve using the loop.

---

<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: [September 21, 2023, 8:23am UTC](https://community.freefem.org/t/how-to-solve-possion-equation-have-a-parameter-h-for-different-values-of-h-using-loop/2716/6 "2023-09-21T08:23:26Z")

</div>

the corrected code with only factorisation (init=0) because the matrix is constante here.

```auto
// M_Usman Muhammad Usman
real Mesh=60;
real ra=0.1;
border C(t=0,1){x=0.5+ra*cos(2*pi*t);y=0.2*sqrt(3)+ra*sin(2*pi*t);}
mesh Th=buildmesh(C(Mesh));
plot(Th,wait=1);
real[int] Pram=[1,2,3],AvgNu(3);
real h;
fespace Vh(Th,P2);
func f= x*y;
Vh U,V;
int i=0;
problem Poisson(U,V,solver=LU,init=i)=
    int2d(Th)(dx(U)*dx(V) + dy(U)*dy(V))
    - int2d(Th)( h*f*V )
    + on(C,U=1) ;
    verbosity=0; 
for( i=0;i<3;i++)
{
  h=Pram[i];

  Poisson;
  plot(U,wait=1);
  macro grad(u) [dx(u),dy(u)]//
AvgNu[i]=-int1d(Th,C)((grad(U))'*[N.x,N.y]);
cout<<i << "Avg = "<<AvgNu[i]<<endl;
}

```

the output :

```auto
ff++-13/bin/FreeFem++ 'Usman.edp' -glut '/usr/local/ff++-13/bin/ffglut' ;exit;
-- FreeFem++ v4.13 (Mer 20 sep 2023 11:48:02 CEST - git v4.13-141-g86b66a0e)
   file : Usman.edp
 Load: lg_fem lg_mesh lg_mesh3 eigenvalue 
    1 : // M_Usman Muhammad Usman
    2 : real Mesh=60;
    3 : real ra=0.1;
    4 : border C(t=0,1){x=0.5+ra*cos(2*pi*t);y=0.2*sqrt(3)+ra*sin(2*pi*t);}
    5 : mesh Th=buildmesh(C(Mesh));
    6 : plot(Th,wait=1);
    7 : real[int] Pram=[1,2,3],AvgNu(3);
    8 : real h;
    9 : fespace Vh(Th,P2);
   10 : func f= x*y;
   11 : Vh U,V;
   12 : int i=0;
   13 : problem Poisson(U,V,solver=LU,init=i)=
   14 : int2d(Th)(dx(U)*dx(V) + dy(U)*dy(V))
   15 : - int2d(Th)( h*f*V )
   16 : + on(C,U=1) ;
   17 : verbosity=0; 
   18 : for( i=0;i<3;i++)
   19 : {
   20 : h=Pram[i];
   21 : 
   22 : Poisson;
   23 : plot(U,wait=1);
   24 : macro grad(u) [dx(u),dy(u)] ) //
   25 : AvgNu[i]=-int1d(Th,C)((grad(U) [dx(U),dy(U)])'*[N.x,N.y]);
   26 : cout<<i << "Avg = "<<AvgNu[i]<<endl;
   27 : } sizestack + 1024 =2168 ( 1144 )

  -- mesh: Nb of Triangles = 638, Nb of Vertices 350
0Avg = 0.00545748
1Avg = 0.010915
2Avg = 0.0163724

```

---

<div class="post-metadata">

### Author: ![M\_Usman](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@M\_Usman](https://community.freefem.org/u/M_Usman)
#### Post date: [September 21, 2023, 12:31pm UTC](https://community.freefem.org/t/how-to-solve-possion-equation-have-a-parameter-h-for-different-values-of-h-using-loop/2716/7 "2023-09-21T12:31:20Z")

</div>

> [@frederichecht](#):
>
> ```auto
> macro grad(u) [dx(u),dy(u)]//
> AvgNu[i]=-int1d(Th,C)((grad(U))'*[N.x,N.y]);
> cout<<i << "Avg = "<<AvgNu[i]<<endl;
> 
> ```

Thank you, dear Professor @frederichecht . For nonlinear PDEs, I got the same value for AvgNu. Maybe I did something wrong in the following code. Kindly have a look and guide me to overcome this error.

real Mesh=60;  
real ra=0.1;  
border C(t=0,1){x=0.5+ra_cos(2_pi_t);y=0.2_sqrt(3)+ra_sin(2_pi_t);}  
mesh Th=buildmesh(C(Mesh));  
plot(Th,wait=1);  
real a=1;  
real err=1;  
real[int] Pram=[1,2,3],AvgNu(3);  
real b;  
fespace Vh(Th,P2);  
func f= x_y;  
Vh U,U0,V;  
U0=0;  
int i=0;  
problem Poisson(U,V,solver=LU,init=i)=  
int2d(Th)(dx(U)_dx(V) + dy(U)dy(V))  
-int2d(Th)(ab_f_U0_U_V)  
+on(C,U=1) ;  
verbosity=0;  
for( i=0;i\<3;i++)  
{  
b=Pram[i];  
for(int ij=0; (ij)\<=10; ij++)  
while (err\>1e-4)  
{  
Poisson;  
err=int2d(Th)(U-U0)^2;  
U0 = U;  
endl;  
}  
macro grad(u) [dx(u),dy(u)]//  
AvgNu[i]=-int1d(Th,C)((grad(U))'_[N.x,N.y]);  
cout\<\<i \<\< "Avg = "\<\<AvgNu[i]\<\<endl;  
}

---

<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: [September 21, 2023, 2:40pm UTC](https://community.freefem.org/t/how-to-solve-possion-equation-have-a-parameter-h-for-different-values-of-h-using-loop/2716/8 "2023-09-21T14:40:34Z")

</div>

For non linear equation first you have to choose which algorithm you use to solve your problem  
fixed point , Newton, …

so you put a loop in a loop…

---

<div class="post-metadata">

### Author: ![M\_Usman](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@M\_Usman](https://community.freefem.org/u/M_Usman)
#### Post date: [September 21, 2023, 3:06pm UTC](https://community.freefem.org/t/how-to-solve-possion-equation-have-a-parameter-h-for-different-values-of-h-using-loop/2716/9 "2023-09-21T15:06:46Z")

</div>

I solved the nonlinear problem as:  
_for(int ij=0; (ij)\<=10; ij++)  
while (err\>1e-4)  
{  
Poisson;  
err=int2d(Th)(U-U0)^2;  
U0 = U;  
endl;  
}_  
and then put it in the loop to compute the solution for various values of b. The got the solution successfully but the solution is same for each value of b.

---

<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: [September 21, 2023, 3:20pm UTC](https://community.freefem.org/t/how-to-solve-possion-equation-have-a-parameter-h-for-different-values-of-h-using-loop/2716/10 "2023-09-21T15:20:06Z")

</div>

Normal, your problem do not change in the iteration process, and the err must set before big value.

---

<div class="post-metadata">

### Author: ![M\_Usman](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@M\_Usman](https://community.freefem.org/u/M_Usman)
#### Post date: [September 21, 2023, 3:49pm UTC](https://community.freefem.org/t/how-to-solve-possion-equation-have-a-parameter-h-for-different-values-of-h-using-loop/2716/11 "2023-09-21T15:49:43Z")

</div>

I already set err=1 here, then I used it in the iterative process.

---

<div class="post-metadata">

### Author: ![M\_Usman](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@M\_Usman](https://community.freefem.org/u/M_Usman)
#### Post date: [September 21, 2023, 6:01pm UTC](https://community.freefem.org/t/how-to-solve-possion-equation-have-a-parameter-h-for-different-values-of-h-using-loop/2716/12 "2023-09-21T18:01:50Z")

</div>

Dear Professor @frederichecht , I also defined err=1 just after the value b and before starting the second loop. But, I got the same values for AvgNu. Where I have to define the value err so that I get the correct results for AvgNu. I am waiting for your positive response.

---

<div class="post-metadata">

### Author: ![M\_Usman](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@M\_Usman](https://community.freefem.org/u/M_Usman)
#### Post date: [September 22, 2023, 12:16pm UTC](https://community.freefem.org/t/how-to-solve-possion-equation-have-a-parameter-h-for-different-values-of-h-using-loop/2716/13 "2023-09-22T12:16:17Z")

</div>

Dear Professor @frederichecht , what do you meant by big value.

---

<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: [September 22, 2023, 12:40pm UTC](https://community.freefem.org/t/how-to-solve-possion-equation-have-a-parameter-h-for-different-values-of-h-using-loop/2716/14 "2023-09-22T12:40:01Z")

</div>

bigger than 1e-4 to enter , in the loop. so 1 for exemple…

---

<div class="post-metadata">

### Author: ![M\_Usman](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@M\_Usman](https://community.freefem.org/u/M_Usman)
#### Post date: [September 22, 2023, 1:10pm UTC](https://community.freefem.org/t/how-to-solve-possion-equation-have-a-parameter-h-for-different-values-of-h-using-loop/2716/15 "2023-09-22T13:10:01Z")

</div>

Dear Professor @frederichecht , I followed your instructions (code given below) but found the same results for AvgNu.

real Mesh=100;  
real ra=0.1;  
border C(t=0,1){x=0.5+ra_cos(2_pi_t);y=0.2_sqrt(3)+ra_sin(2_pi_t);}  
mesh Th=buildmesh(C(Mesh));  
plot(Th,wait=1);  
real a=1;  
real[int] Pram=[1,20,30],AvgNu(3),E=[1,1,1];  
real b;  
fespace Vh(Th,P2);  
func f= x_y;  
Vh U,U0,V;  
U0=0;  
int i=0;  
problem Poisson(U,V,solver=LU,init=i)=  
int2d(Th)(dx(U)_dx(V) + dy(U)dy(V))  
-int2d(Th)(ab_f_U0_U_V)  
+on(C,U=1) ;  
verbosity=0;  
for( i=0;i\<3;i++)  
{  
b=Pram[i];  
real err=1;  
for(int ij=0; (ij)\<=10; ij++)  
while (err\>1e-7)  
{  
cout\<\<err\<\<endl;  
Poisson;  
err=int2d(Th)(U-U0)^2;  
U0 = U;  
endl;  
}  
macro grad(u) [dx(u),dy(u)]//  
AvgNu[i]=-int1d(Th,C)((grad(U))'_[N.x,N.y]);  
cout\<\<i \<\< "Avg = "\<\<AvgNu[i]\<\<endl;  
}

---

<div class="post-metadata">

### Author: ![M\_Usman](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@M\_Usman](https://community.freefem.org/u/M_Usman)
#### Post date: [September 22, 2023, 2:27pm UTC](https://community.freefem.org/t/how-to-solve-possion-equation-have-a-parameter-h-for-different-values-of-h-using-loop/2716/16 "2023-09-22T14:27:18Z")

</div>

Dear Professor @frederichecht , kindly see this and guide me to fix the query.

---

<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: [September 28, 2023, 9:09am UTC](https://community.freefem.org/t/how-to-solve-possion-equation-have-a-parameter-h-for-different-values-of-h-using-loop/2716/17 "2023-09-28T09:09:33Z")

</div>

I have no idea of your No linear problem, because you only put linear problem.  
So it is difficult to help you.
