# How do I use low level FEM?

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 region is [-1,1] for x and [-1,1] for y.

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

• 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? – Hugh Jan 16 '19 at 12:38
• 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] – Xminer Jan 16 '19 at 12:41
• Do you have a region on which you wish to solve the equation and boundary conditions? – Hugh Jan 16 '19 at 12:43
• region is {x,-1,1} and{y,-1,1}. Boundary Condition is u[-1,y]=Cos[-y] – Xminer Jan 16 '19 at 12:45
• Can you specify what problem you encountered? Otherwise there is an entire tutorial on FiniteElementProgramming. – user21 Jan 16 '19 at 12:50

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:

Needs["NDSolveFEM"]
{state} =
NDSolveProcessEquations[{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"]
femdata["Properties"]
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 NDSolveProcessEquations 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:

pdeData["All"]

{{{{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:

MatrixPlot[stiffness]


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]


Solve:

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.