How to define a CoefficientFunction from a discrete data


I’d like to define a CoefficientFunction from a discrete data like an array of numbers {1,2,3,4,5,6,…}. Is it possible in NGSolve? I was wondering if someone could possibly share her/his knowledge with me in this regard. Thanks.


Hi Danial,

where do your values relate to ? If every value corresponds to one element, you can define an L2-gridfunction, and set its coefficients to your values:

fespace = L2(mesh, order=0)
gf = GridFunction(fespace)
for i in range(fes.ndof):
  gf.vec[i| = value[i] 

You can plot the GridFunction, and you can use it where you want to use your coefficient. A GridFunction is a CoefficientFunction, too.

The basis-functions of the L2(order=0) space are exactly the characteristic functions of every element.
If your values correspond to vertices, you use an H1-space (with order=1) instead.

Best, Joachim

Hi Joachim,
Thank you very much for your complete explanation. According to your question, the values were defined over a domain. Take a one dimensional problem, for example. I have 100 values over the domain. Now, I’d like to define a function using these values that I can use it in the variational form to solve an ODE. Note that, there is no relationship between the values and the elements. Would that be possible to apply the same way for all domain? Thanks.