# Not getting accurate order of convergence

**URL:** <https://community.freefem.org/t/not-getting-accurate-order-of-convergence/3928>\
**Category:** General Discussion\
**Created:** [May 22, 2025, 10:10am UTC](https://community.freefem.org/t/not-getting-accurate-order-of-convergence/3928 "2025-05-22T10:10:09Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![xavier1](https://avatars.discourse-cdn.com/v4/letter/x/f04885/32.png) [@xavier1](https://community.freefem.org/u/xavier1)\
**Post date:** [May 22, 2025, 10:10am UTC](https://community.freefem.org/t/not-getting-accurate-order-of-convergence/3928/1 "2025-05-22T10:10:09Z")

</div>

I compute the solution for several spatial mesh sizes and measure the L2 error with respect to the exact solution. My goal is to study the order of convergence, but the computed rates seem inconsistent or lower than expected.

verbosity=0;  
macro dn(u) (N.x_dx(u)+N.y_dy(u)) // def the normal derivative  
// parameter  
real dt, h, T= 1;  
int nref =4;  
real [int] L2error ( nref ); // initialize the L2 error array  
real [int] Dx( nref ); // initialize the Space discretization array  
real [int] DT( nref ); // initialize the Time discretization array  
real L2err;  
real eorr;

macro Grad(u)[dx(u),dy(u)]//  
macro uex(t)(exp(-t)_(x^2 - y^2)) //  
macro g(t) (exp(-3_(t))\*(x^2 - y^2)^3) //

//Mesh  
for ( int n=0; n\< nref ;n++) {  
int M =5_2^(n) ;  
h =1./ M;  
Dx[n]=h;  
dt=1./256;[//0.4](https://0.4)_h;[//0.4](https://0.4)_h^2;//  
DT[n]= dt;  
// border bottom(t=0,1){x = 0 + t_2 - 1; y = -1; label = 1;}  
// border right(t=0,1){x = 1; y = -1 + t_2; label = 1;}  
// border top(t=0,1){x = 1 - t_2; y = 1; label = 1;}  
// border left(t=0,1){x = -1; y = 1 - t\*2; label = 1;}

mesh Th = square(M,M,[2_x-1,2_y-1]);//buildmesh(bottom(M) + right(M) + top(M) + left(M));  
cout \<\< "level M = " \<\< M \<\< endl;  
//plot(Th, wait=false);

//Fespace  
fespace Vh(Th,P1);  
fespace Vh2(Th,P1);  
//fespace Vh2(Th,[P1dc,P1dc]);  
//fespace Vh2(Th,[P1,P1]);

// Function  
func real df(real u){return u^3; }  
func real ddf(real u){return 3\*u^2; }

Vh u, uold, uoldd, auxv, vv;  
Vh2 dfalpha;  
Vh2 ddfalpha;

uoldd = x^2 - y^2;  
uold = uoldd - dt\*(x^2 - y^2);

plot(u,wait=0,cmm=“Klein-Gordan Exact Solution” ,value=true);

for (real t=0.;t\<T;t+=dt){  
uold=uoldd;  
u=uold;  
real res;  
int iter=0;  
for (int k=0;k\<50;k++) // Newton loop  
{  
dfalpha=df(u);  
ddfalpha= ddf(u);

```
varf Fv(v, phi)=int2d(Th)

```

((u-2_uold+uoldd)phi1./(dt_dt)+(dx(u)\*dx(phi)+dy(u)\*dy(phi))_1./(2_dt)  
-(dx(uoldd)\*dx(phi)+dy(uoldd)\*dy(phi))_1./(2_dt)+(dx(u)\*dx(phi)+dy(u)\*dy(phi))_1./2+(dx(uoldd)dx(phi)+dy(uoldd)dy(phi))1./2+ (u-uoldd)phi1./(2dt) + (dfalpha)phi1./4 +((uoldduoldd)u)phi1./4 +((uu)uoldd)phi1./4+((uoldduoldduoldd)phi1.4))// dfalpha_phi)  
+int2d(Th)(-g(t)\*phi);// + on(1,2,3,4, u=0);

auxv=Fv(0,Vh); // nabla J  
res= sqrt(auxv'_auxv[]); // Residual  
// cout \<\< " residu = " \<\< res \<\< endl;  
if (res\< 1e-6) break;  
varf dFv(v, phi) = int2d(Th)(  
v_phi\*(1./(dt\*dt))

- (dx(v)_dx(phi) + dy(v)dy(phi))(1./(2_dt) + 1./2)
- v_phi_(1./(2\*dt))
- ddfalpha_v_phi // df/du = 3u^2 \* v  
+(u_v_uoldd)_phi_1./2
- (1./4)_uoldd^2_v\*phi // df/du = 3u^2 \* v  
);

// varf dFv(v,phi)=int2d(Th)  
// (v_phi_1./(dt_dt) + (dx(v)dx(phi)+dy(v)dy(phi))1./(2dt) +(dx(v)dx(phi)+dy(v)dy(phi))1./(2)+ vphi1./(2dt)+((vv)phi3./4)+((uoldduoldd)phi1./4)+((u_uoldd)_phi_1./2));

matrix H=dFv(Vh,Vh, factorize=1,solver=LU); // HJ  
vv=H^-1\*auxv;  
u-=vv;  
iter++;  
}// end of Newton loop

// uold=2.\*u-uold;  
plot(u,wait=0,cmm=“Klein-Gordan Approximate Solution”, value=true);  
eorr= abs(u-uex(t+dt))^2;  
L2err=sqrt(int2d(Th)(abs(u-uex(t+dt))^2));  
cout \<\< "time = " \<\< t+dt \<\< " iter = " \<\< iter \<\< " residu = " \<\< res \<\< " L2 error = " \<\< L2err \<\< " eror = "\<\< eorr \<\< endl;  
}//end of time loop  
cout \<\< “final time T is reached” \<\< endl;

L2error [n]= L2err;  
}//end of loop on n\< nref

```
for ( int n =0;n< nref ;n ++)
cout << " L2error " << n << " = " << L2error [n] <<endl;
for ( int n =1; n< nref; n ++){
cout <<" Space convergence rate = " << log ( L2error [n-1]/L2error
[n])/log(Dx[n-1]/Dx[n]) <<endl;
}

```

---

<div class="post-metadata">

**Author:** ![Monirul25](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/monirul25/32/3287_2.png) [@Monirul25](https://community.freefem.org/u/Monirul25)\
**Post date:** [May 22, 2025, 6:41pm UTC](https://community.freefem.org/t/not-getting-accurate-order-of-convergence/3928/2 "2025-05-22T18:41:39Z")

</div>

Please upload your code in .edp format here so that it can run directly.

---

<div class="post-metadata">

**Author:** ![xavier1](https://avatars.discourse-cdn.com/v4/letter/x/f04885/32.png) [@xavier1](https://community.freefem.org/u/xavier1)\
**Post date:** [May 23, 2025, 1:49pm UTC](https://community.freefem.org/t/not-getting-accurate-order-of-convergence/3928/3 "2025-05-23T13:49:00Z")

</div>

Here is the code. getting error while imposing dirichlet boundary condition

[ad1.edp](https://community.freefem.org/uploads/short-url/njlH0rD8w6ZLawLYAhNSQCJzJ5t.edp) (5.9 KB)

---

<div class="post-metadata">

**Author:** ![Monirul25](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/monirul25/32/3287_2.png) [@Monirul25](https://community.freefem.org/u/Monirul25)\
**Post date:** [May 23, 2025, 2:37pm UTC](https://community.freefem.org/t/not-getting-accurate-order-of-convergence/3928/4 "2025-05-23T14:37:03Z")

</div>

Kindly please share your full scheme that you want to implement in FF++ code. It will help me to check the code.

---

<div class="post-metadata">

**Author:** ![xavier1](https://avatars.discourse-cdn.com/v4/letter/x/f04885/32.png) [@xavier1](https://community.freefem.org/u/xavier1)\
**Post date:** [May 23, 2025, 2:55pm UTC](https://community.freefem.org/t/not-getting-accurate-order-of-convergence/3928/5 "2025-05-23T14:55:34Z")

</div>

I am trying to solve numerical experiment of  
[https://www.tandfonline.com/doi/full/10.1080/00036811.2024.2426227#:~:text=In%20this%20work%2C%20we%20consider,semidiscrete%20and%20fully%20discrete%20schemes](https://www.tandfonline.com/doi/full/10.1080/00036811.2024.2426227#:~:text=In%20this%20work%2C%20we%20consider,semidiscrete%20and%20fully%20discrete%20schemes).

---

<div class="post-metadata">

**Author:** ![Monirul25](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/monirul25/32/3287_2.png) [@Monirul25](https://community.freefem.org/u/Monirul25)\
**Post date:** [May 23, 2025, 4:26pm UTC](https://community.freefem.org/t/not-getting-accurate-order-of-convergence/3928/6 "2025-05-23T16:26:10Z")

</div>

I will check and let you know.

---

<div class="post-metadata">

**Author:** ![Monirul25](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/monirul25/32/3287_2.png) [@Monirul25](https://community.freefem.org/u/Monirul25)\
**Post date:** [May 24, 2025, 7:14am UTC](https://community.freefem.org/t/not-getting-accurate-order-of-convergence/3928/7 "2025-05-24T07:14:33Z")

</div>

Here, is your code with several modifications but still order is coming initially good but reducing as nref increases. I will know you if i get any better results in this. But approximate solution figure and values coming fine. **I hope there are some mistakes in code that reducing the order of converegence.** If that fixed the i hope you will get optimal results.

[Ad1\_MI.edp](https://community.freefem.org/uploads/short-url/b25X425nxzvssNoNxhKT0UvdQfc.edp) (3.3 KB)

Results for h=1/4, 1/8, 1/16, 1/32

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/e/eef688522868bd8eecd55ac2ccfcc01acece1f7b.png)

---

<div class="post-metadata">

**Author:** ![xavier1](https://avatars.discourse-cdn.com/v4/letter/x/f04885/32.png) [@xavier1](https://community.freefem.org/u/xavier1)\
**Post date:** [May 24, 2025, 2:25pm UTC](https://community.freefem.org/t/not-getting-accurate-order-of-convergence/3928/8 "2025-05-24T14:25:25Z")

</div>

Thank you for your response…i think second initial condition will be calculated using weak formulation.

---

<div class="post-metadata">

**Author:** ![Monirul25](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/monirul25/32/3287_2.png) [@Monirul25](https://community.freefem.org/u/Monirul25)\
**Post date:** [May 25, 2025, 4:42am UTC](https://community.freefem.org/t/not-getting-accurate-order-of-convergence/3928/9 "2025-05-25T04:42:46Z")

</div>

[AD1\_L2.edp](https://community.freefem.org/uploads/short-url/4PQKpHjvWLmnqJZ1Iaaon7c5PzM.edp) (3.5 KB)

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/3/3647ca7de9b69190d62a073e57a114a9371629a2.png)  
(With dt=h^2 using P1 is giving optimal in space. Issue is comming for time. Since, you are using CN, so optimal order should be 2). I will check in future again for time.

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/e/e491b0d59b4b13d1e30efa48ea8954e33e164070.png)  
(With dt=h)(It should give 2 in space and 2 in time but it giving less this means there is a problem in code)

---

<div class="post-metadata">

**Author:** ![Monirul25](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/monirul25/32/3287_2.png) [@Monirul25](https://community.freefem.org/u/Monirul25)\
**Post date:** [May 25, 2025, 5:54am UTC](https://community.freefem.org/t/not-getting-accurate-order-of-convergence/3928/10 "2025-05-25T05:54:20Z")

</div>

You can run the last code for h=1/128, 1/256, 1/512, 1/1024 (That is M=2^(n+7), where n varies from 0 to 3). I hope order will come good. Let me know after doing getting results.

---

<div class="post-metadata">

**Author:** ![xavier1](https://avatars.discourse-cdn.com/v4/letter/x/f04885/32.png) [@xavier1](https://community.freefem.org/u/xavier1)\
**Post date:** [May 25, 2025, 6:01am UTC](https://community.freefem.org/t/not-getting-accurate-order-of-convergence/3928/11 "2025-05-25T06:01:13Z")

</div>

I have fixed error in Fv and DFv function. But according to paper we have to fix k and h varying from 1/5, 1/10, 1/20, 1/40, 1/80. While adding dirichlet boundary condition using exact solution, it is showing error.

---

<div class="post-metadata">

**Author:** ![Monirul25](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/monirul25/32/3287_2.png) [@Monirul25](https://community.freefem.org/u/Monirul25)\
**Post date:** [May 25, 2025, 6:46am UTC](https://community.freefem.org/t/not-getting-accurate-order-of-convergence/3928/12 "2025-05-25T06:46:49Z")

</div>

Please upload your code here.

---

<div class="post-metadata">

**Author:** ![xavier1](https://avatars.discourse-cdn.com/v4/letter/x/f04885/32.png) [@xavier1](https://community.freefem.org/u/xavier1)\
**Post date:** [May 25, 2025, 7:16am UTC](https://community.freefem.org/t/not-getting-accurate-order-of-convergence/3928/13 "2025-05-25T07:16:22Z")

</div>

[AD1\_L2 (1).edp](https://community.freefem.org/uploads/short-url/gaTRoR8nEuQwFeZvVILIVF4Bz1s.edp) (4.0 KB)  
Error wil be calculated in H1 norm.

---

<div class="post-metadata">

**Author:** ![Monirul25](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/monirul25/32/3287_2.png) [@Monirul25](https://community.freefem.org/u/Monirul25)\
**Post date:** [May 25, 2025, 7:40am UTC](https://community.freefem.org/t/not-getting-accurate-order-of-convergence/3928/14 "2025-05-25T07:40:36Z")

</div>

Okay. Thanks. I will check.

---

<div class="post-metadata">

**Author:** ![Monirul25](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/monirul25/32/3287_2.png) [@Monirul25](https://community.freefem.org/u/Monirul25)\
**Post date:** [May 26, 2025, 3:49am UTC](https://community.freefem.org/t/not-getting-accurate-order-of-convergence/3928/15 "2025-05-26T03:49:28Z")

</div>

In your code,

> Blockquote  
> uoldd = x^2 - y^2;  
> uold = uoldd - dt\*(x^2 - y^2);  
> Blockquote

This ensures that you are computing U^1 from backward-euler. But you have to use equation (37) to compute U^1 when U^0 is known. After that you can compute rest of the solutions U^2,…, U^n, U^{n+1}.

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/7/7ed058a630cbb5b31d950ec5037d33d76d45b4b3.png)

I think due to that it is giving first order in time.

---

<div class="post-metadata">

**Author:** ![xavier1](https://avatars.discourse-cdn.com/v4/letter/x/f04885/32.png) [@xavier1](https://community.freefem.org/u/xavier1)\
**Post date:** [May 26, 2025, 10:40am UTC](https://community.freefem.org/t/not-getting-accurate-order-of-convergence/3928/16 "2025-05-26T10:40:35Z")

</div>

[AD1\_L2 (1).edp](https://community.freefem.org/uploads/short-url/tFfm5iQfmhMe5Giw2QssQuFBtUl.edp) (5.3 KB)  
I have added that but not sure is it correct way to implement, because order of convergence is still not accurate.

---

<div class="post-metadata">

**Author:** ![Monirul25](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/monirul25/32/3287_2.png) [@Monirul25](https://community.freefem.org/u/Monirul25)\
**Post date:** [May 26, 2025, 11:02am UTC](https://community.freefem.org/t/not-getting-accurate-order-of-convergence/3928/17 "2025-05-26T11:02:40Z")

</div>

Okay, i will check soon.
