Convergence error for a 1D problem with Neumann BC when augmenting the order of the FEM space

Hi, I’m trying solve this problem using FEniCSx:
-u''(x) + u(x) = x^2 - x -2 \\ -u'(0) = u(1) = 1
I implemented it like this on my code:

def u1D_ex(x):
    return x[0]**2 - x[0]
dimV = 1
errL2_1D = []
errH1_1D = []
taille_1D = []
for N in [5, 11, 21, 41, 81, 100, 200, 400, 800]:
    mesh1D = mesh.create_unit_interval(comm=MPI.COMM_WORLD, nx=N)

    V1D = fem.functionspace(mesh1D, ("Lagrange", dimV))

    #Solving 1D problem
    u, v = ufl.TrialFunction(V1D), ufl.TestFunction(V1D)
    x = ufl.SpatialCoordinate(mesh1D)
    f = x[0]**2 - x[0] - 2
    u1D_sol = fem.Function(V1D)
    u1D_sol.interpolate(u1D_ex)
    alpha1, beta1 = 1, 1

    a = ufl.Dx(u,0)*ufl.Dx(v,0)*dx + beta1*u*v*dx
    l = f*v*dx + alpha1*v*ds
    
    problem1D = LinearProblem(a, l, petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
    uh_1D = problem1D.solve()


    #Calcul L2 error
    comm = uh_1D.function_space.mesh.comm
    errorL2  = fem.form((uh_1D-u1D_sol)**2*dx)
    EL2 = np.sqrt(comm.allreduce(fem.assemble_scalar(errorL2), MPI.SUM))
    errL2_1D.append(EL2)

    #Calcul H1 error
    errorH1 = fem.form(ufl.Dx(uh_1D - u1D_sol, 0)**2*dx + (uh_1D-u1D_sol)**2*dx)
    EH1 = np.sqrt(comm.allreduce(fem.assemble_scalar(errorH1), MPI.SUM))
    errH1_1D.append(EH1)

    #Calcul mesh size
    taille_1D.append(1/N)

When I set dimV = 1 and I plot the errors I get this:


but when I set dimV=2 or higher it looks something like this:

I’m pretty new using FEniCSx so any help on pointing what I’m doing wrong or misunderstanding is really appreciated.

Look at the magnitude of the error. You are getting an L2 error of 10^(-11) at the max. As your solution is x^2 - x, it is in the space of second order Lagrange elements, and your L2 error is machine precision (remember that you have taken the square root to get this number, i.e the integral is of order 10^{-22} which is way beyond machine tolerance for doubles.

1 Like

Thank you very much. I was so sure I was doing something wrong in the program that it didn’t occur to me to check for the machine precision.