# What can I do to calculate the L2 norm between the exact solution and my numerical solution?

**URL:** https://discourse.julialang.org/t/what-can-i-do-to-calculate-the-l2-norm-between-the-exact-solution-and-my-numerical-solution/114359
**Category:** Numerics
**Tags:** question, fem, diferential-equation
**Created:** [May 16, 2024, 1:47pm UTC](https://discourse.julialang.org/t/what-can-i-do-to-calculate-the-l2-norm-between-the-exact-solution-and-my-numerical-solution/114359 "2024-05-16T13:47:13Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![mcr.92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcr.92/32/209285_2.png) [@mcr.92](https://discourse.julialang.org/u/mcr.92)
#### Post date: [May 16, 2024, 1:47pm UTC](https://discourse.julialang.org/t/what-can-i-do-to-calculate-the-l2-norm-between-the-exact-solution-and-my-numerical-solution/114359/1 "2024-05-16T13:47:13Z")

</div>

I am solving a differential equation by the MOL method, it has an exact solution so I want to calculate the error in the L2 norm. I made this code

```julia
F1(x) = A*(cosh(B*x))
F2(y) = A*(cosh(B*y))
FC = -(2*(B^2)*K - W)/(ρ*C_s)    
F3(t::Real) = t > t_c ? E * exp(FC * t) : 1 + E * exp(t)
T(t::Real)= x -> F1(x[1])*F2(x[2])*F3(t)
(...)
uₕₜ = solve(ode_solver,op_Af,u₀,t_0,t_f)

  error_L2 = Vector{Float64}(undef, 22)
for (uₕ,t) in uₕₜ
      error_L2[i] = √(integrate((T(t) - uₕ)^2, dΩ))
end

```

but when trying to up 2 the difference between the exact solution and my approximate solution it gives me an error. What can I do to calculate this norm?

---

<div class="post-metadata">

### Author: ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)
#### Post date: [May 16, 2024, 1:52pm UTC](https://discourse.julialang.org/t/what-can-i-do-to-calculate-the-l2-norm-between-the-exact-solution-and-my-numerical-solution/114359/2 "2024-05-16T13:52:00Z")

</div>

Hi,

welcome to the community 🙂

It makes it easier for us to help, if you post a fully runnable snippet of your code, including dependencies.

And can you provide the error message too?

---

<div class="post-metadata">

### Author: ![mcr.92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcr.92/32/209285_2.png) [@mcr.92](https://discourse.julialang.org/u/mcr.92)
#### Post date: [May 16, 2024, 2:18pm UTC](https://discourse.julialang.org/t/what-can-i-do-to-calculate-the-l2-norm-between-the-exact-solution-and-my-numerical-solution/114359/3 "2024-05-16T14:18:10Z")

</div>

Thanks for answering, of course I’ll put the code:

```julia
using GeometryBasics
using Gridap,
        Gridap.Algebra,
        Gridap.FESpaces,
        Gridap.ReferenceFEs,
        Gridap.Arrays,
        Gridap.Geometry,
        Gridap.Fields,
        Gridap.CellData,
        GridapGmsh
using IterativeSolvers
using IncompleteLU
using SparseArrays
using BenchmarkTools 
using Plots
using CPUTime
using LinearAlgebra

output_path = "output_file/"
t_f = 1.
t_c = 0.2
Δt = 0.05
ρ = 1000            
C_s = 4200        
K = 84030       

F1(x) = A*(cosh(B*x))
F2(y) = A*(cosh(B*y))
FC = -(2*(B^2)*K - W)/(ρ*C_s)    
F3(t::Real) = t > t_c ? E * exp(FC * t) : 1 + E * exp(t)  
Q(x, t::Real) = t > t_c ? 0 : -C_s * F1(x[1]) * F2(x[2])
T(t::Real)= x -> F1(x[1])*F2(x[2])*F3(t)
T_0(x, t::Real) = t > t_c ? (1 + E * exp(t_c)) * F1(x[1]) * F2(x[2]) : (1 + E) * F1(x[1]) * F2(x[2])
T_in(t::Real) = x -> T_0(x,t)
g(x, t::Real) = F1(x[1]) * F2(x[2]) * F3(t)
g(t::Real) = x -> g(x,t)

model = CartesianDiscreteModel((0,1,0,1),(20,20))
Ω = Interior(model)
order = 2;
reffe = ReferenceFE(lagrangian, Float64, order)
dΩ = Measure(Ω, 2*order)
V = TestFESpace(model, reffe, conformity = :H1, dirichlet_tags = ["Gamma_D", "Gamma_N", "Gamma_R"], vector_type = Vector{Float64})
U = TransientTrialFESpace(V,g)

Qsource(t::Real) = x -> Q(x, t)
K_bil(t,u,v) = ∫(K*(∇(u) ⋅ ∇(v)) + W*(u*v))*dΩ 
M_bil(t,u,v) = ∫(ρ*C_s*(u*v))*dΩ
b_lin(t,v) = ∫( Qsource(t)*v )*dΩ

op_Af = TransientAffineFEOperator(M_bil, K_bil, b_lin, U, V)
linear_solver = LUSolver()
ode_solver = ThetaMethod(linear_solver,Δt,1)

t_0 = 0.0
u₀ = interpolate(T_in(t_0),U(t_0))
uₕₜ = solve(ode_solver,op_Af,u₀,t_0,t_f)

error_L2 = Vector{Float64}(undef, 22)
for (uₕ,t) in uₕₜ
      error_L2[i] = √(integrate((T(t) - uₕ)^2, dΩ))
end

```

In the part of calculating the error in the L2 norm it gives me the following error:

> ERROR: MethodError: no method matching ^(::Gridap.CellData.OperationCellField{ReferenceDomain}, ::Int64)  
> Closest candidates are:  
> ^(::Union{AbstractChar, AbstractString}, ::Integer) at strings/basic.jl:718  
> ^(::AbstractGray{T} where T, ::Integer) at C:\Users\Manuel.julia\packages\ColorVectorSpace\QI5vM\src\ColorVectorSpace.jl:333  
> ^(::AbstractGray{T} where T, ::Real) at C:\Users\Manuel.julia\packages\ColorVectorSpace\QI5vM\src\ColorVectorSpace.jl:334

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [May 16, 2024, 2:23pm UTC](https://discourse.julialang.org/t/what-can-i-do-to-calculate-the-l2-norm-between-the-exact-solution-and-my-numerical-solution/114359/4 "2024-05-16T14:23:52Z")

</div>

An example is given [at the end of this tutorial](https://gridap.github.io/Tutorials/dev/pages/t012_emscatter/#Compare-the-difference-in-the-%22center%22-region-1).

I think you need something like `sum(abs2(T - uₕ) * dΩ)`, though in the tutorial it first projects the analytical solution into the FEM basis. In general, if you want to compose an arbitrary function `f` with a cell field `u`, you need to use `f ∘ u`, e.g. `(x -> x^2) ∘ uₕ`. But a specialized `CellField` method is [already defined](https://github.com/gridap/Gridap.jl/blob/9079dd1ac56da9ca5f6d8d3952d69caa6c014fa4/src/CellData/CellFields.jl#L619-L623) for [`abs2`](https://docs.julialang.org/en/v1/base/math/#Base.abs2).
