Subject: [FEniCS-dev] How to get one function minus another function?

From: Chong Luo <[email-masked]>
Date: Fri, 9 Nov 2007 09:33:41 -0800 (PST)

Hi,

I want to compare the solution given by FEniCS to the exact solution, so that I can get the convergence rate. However, I don't know how to find the difference between two functions. It seems that the solution given by FEniCS is a DiscreteFunction, while the exact solution was defined by me via overriding "eval()", thus a UserFunction. But I don't know how to get a function whose value is the difference of these two functions.

Could you help me with this? Thank you!

Best,
Chong

      ___________________________________________________________ 
雅虎邮箱，终生伙伴！ 
http://mail.yahoo.com.cn/

---

From: Anders Logg <[email-masked]>
Date: Sat, 10 Nov 2007 12:26:51 +0100

You need to define a form for the norm of the difference. As an
example, to compute the L2 norm of the difference of two functions
u_h (approximate solution) and u (exact solution) define the following
form:

  u_h = Function(element)
  u   = Function(element)
  e   = u_h - u

  M = e*e*dx

Then assemble the value of the functional M (a scalar) and take the
square root to obtain the error in the L2 norm.

Other norms, like the H1 norm can be computed similarly:

  M = e*e*dx + dot(grad(e), grad(e))*dx

However, it's important to note here that what gets computed is the
norm of the difference between the approximate solution and the
*interpolation* of the exact solution into the finite element space.
This might not be what you want and you may be fooled when computing
convergence rates etc. So to be on the safe side, use a higher order
approximation for the exact solution:

  k  = <suitable value of k here, say k = 5>
  Pk = FiniteElement("Lagrange", shape, k)

  u = Function(Pq)

(I have added this to the FAQ now.)

/Anders

On Fri, Nov 09, 2007 at 09:33:41AM -0800, Chong Luo wrote:
> Hi,
> 
> I want to compare the solution given by FEniCS to the exact solution, so that I
> can get the convergence rate. However, I don't know how to find the difference
> between two functions. It seems that the solution given by FEniCS is a
> DiscreteFunction, while the exact solution was defined by me via overriding
> "eval()", thus a UserFunction. But I don't know how to get a function whose
> value is the difference of these two functions.
> 
> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
> @yahoo.cn 新域名、无限量，快来抢注！

> _______________________________________________
> FEniCS-dev mailing list
> [email-masked]
> http://www.fenics.org/mailman/listinfo/fenics-dev