How do I simulate the following partial differential equation using Low level FEM in Mathematica?

D[u[x,y], x] - D[u[x,y], y] = x Sin[x y] - y Sin[x y]

The answer is Cos[x y]


The region is [-1,1] for x and [-1,1] for y.

The boundary condition is u[-1,y]=Cos[-y]

  • $\begingroup$ In what sense is this a differential equation? Normally you are looking for an unknown function that is defined through a differential equation. What is the unknown function? $\endgroup$
    – Hugh
    Jan 16, 2019 at 12:38
  • $\begingroup$ my code was wrong.at first,I did used Cos(xy) for creating PDE and solve by FEM. the unknown function is u[x,y] $\endgroup$
    – Xminer
    Jan 16, 2019 at 12:41
  • $\begingroup$ Do you have a region on which you wish to solve the equation and boundary conditions? $\endgroup$
    – Hugh
    Jan 16, 2019 at 12:43
  • $\begingroup$ region is {x,-1,1} and{y,-1,1}. Boundary Condition is u[-1,y]=Cos[-y] $\endgroup$
    – Xminer
    Jan 16, 2019 at 12:45
  • 1
    $\begingroup$ Can you specify what problem you encountered? Otherwise there is an entire tutorial on FiniteElementProgramming. $\endgroup$
    – user21
    Jan 16, 2019 at 12:50

1 Answer 1


I am not a 100% sure I understand your question, here is what I think could help you.

The finite element method is, strictly speaking, not a method to solve PDEs. What it does it takes a continuous PDE and converts it to an approximate equivalent matrix and vector. The matrix and vector are discrete representations of the PDE. When you solve this set of equations you will get an approximate solution to the PDE.

We set up an equation:

{state} = 
  NDSolve`ProcessEquations[{Laplacian[u[x, y], {x, y}] == 1, 
    DirichletCondition[u[x, y] == 0, True]}, u, {x, 0, 1}, {y, 0, 1}, 
   Method -> {"FiniteElement"}];

Now we extract some data from the NDSolve state data object.

femdata = state["FiniteElementData"]
methodData = femdata["FEMMethodData"];
bcData = femdata["BoundaryConditionData"];
pdeData = femdata["PDECoefficientData"];
variableData = state["VariableData"];
solutionData = state["SolutionData"][[1]];

If you do not want to / can not use NDSolve`ProcessEquations you may need to look at InitializePDECoefficients and such functions.

If you look at pdeData, that now contains the coefficients of the equations given in NDSolve:


{{{{1}}, {{{{0}, {0}}}}}, {{{{{-1, 
      0}, {0, -1}}}}, {{{{0}, {0}}}}, {{{{0, 
      0}}}}, {{0}}}, {{{0}}}, {{{0}}}}

Now, the finite element method is applied. This converts the continuous PDE into a system of discrete matrices:

discretePDE = DiscretizePDE[pdeData, methodData, solutionData]
{load, stiffness, damping, mass} = discretePDE["SystemMatrices"]

You can look at the matrices:


The same conversion is done for the boundary conditions:

discreteBCs = 
  DiscretizeBoundaryConditions[bcData, methodData, solutionData];

All the finite element method does is done now. The rest is linear algebra.

We now put the discrete boundary conditions into the discrete matrices:

DeployBoundaryConditions[{load, stiffness}, discreteBCs]


solution = LinearSolve[stiffness, load];

Generate an Interpolating function:

mesh = methodData["ElementMesh"];
ifun = ElementMeshInterpolation[{mesh}, solution]

And visualize:

Plot3D[ifun[x, y], {x, y} \[Element] mesh]

Also, there is a tutorial about Finite Element Programming that has much more information on this topic.

Hope this helps a bit.

  • $\begingroup$ How can I customize/change mesh data using this method? $\endgroup$
    – Stratus
    Feb 6, 2023 at 14:56
  • 1
    $\begingroup$ @Diogo, you could use for example: {state} = NDSolveProcessEquations[{Laplacian[u[x, y], {x, y}] == 1, DirichletCondition[u[x, y] == 0, True]}, u, Element[{x,y}, myMesh]]` $\endgroup$
    – user21
    Feb 6, 2023 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.