Solve PDEs Programmatically

When You Need Programmatic Solutions

Although the PDE app provides a convenient working environment, there are situations where the flexibility of using the command-line functions is needed. These include:

  • Geometrical shapes other than straight lines, circular arcs, and elliptical arcs

  • Nonstandard boundary conditions

  • Complicated PDE or boundary condition coefficients

  • More than two dependent variables in the system case

  • Nonlocal solution constraints

  • Special solution data processing and presentation itemize

The PDE app can still be a valuable aid in some of the situations presented previously, if part of the modeling is done using the PDE app and then made available for command-line use through the extensive data export facilities of the PDE app.

Data Structures in Partial Differential Equation Toolbox

The process of defining your problem and solving it is reflected in the design of the PDE app. A number of data structures define different aspects of the problem, and the various processing stages produce new data structures out of old ones. See the following figure.

The rectangles are functions, and ellipses are data represented by matrices or files. Arrows indicate data necessary for the functions.

As there is a definite direction in this diagram, you can cut into it by presenting the needed data sets, and then continue downward. In the following sections, we give pointers to descriptions of the precise formats of the various data structures and files.

Constructive Solid Geometry Model

A Constructive Solid Geometry (CSG) model is specified by a Geometry Description matrix, a set formula, and a Name Space matrix. For a description of these data structures, see the reference page for decsg. At this level, the problem geometry is defined by overlapping solid objects. These can be created by drawing the CSG model in the PDE app and then exporting the data using the Export Geometry Description, Set Formula, Labels option from the Draw menu.

Decomposed Geometry

A decomposed geometry is specified by either a Decomposed Geometry matrix, or by a Geometry file. Here, the geometry is described as a set of disjoint minimal regions bounded by boundary segments and border segments. A Decomposed Geometry matrix can be created from a CSG model by using the function decsg. It can also be exported from the PDE app by selecting the Export Decomposed Geometry, Boundary Cond's option from the Boundary menu. A Geometry file equivalent to a given Decomposed Geometry matrix can be created using the wgeom function. A decomposed geometry can be visualized with the pdegplot function. For descriptions of the data structures of the Decomposed Geometry matrix and Geometry file, see the respective reference pages for decsg and pdegeom.

Boundary Conditions

These are specified by either a Boundary Condition matrix, or a Boundary file. Boundary conditions are given as functions on boundary segments. A Boundary Condition matrix can be exported from the PDE app by selecting the Export Decomposed Geometry, Boundary Cond's option from the Boundary menu. For a description of the data structures of the Boundary Condition matrix and Boundary file, see the respective reference pages for assemb and pdebound.

Equation Coefficients

The PDE is specified by either a Coefficient matrix or a Coefficient file for each of the PDE coefficients c, a, f, and d. The coefficients are functions on the subdomains. Coefficients can be exported from the PDE app by selecting the Export PDE Coefficient option from the PDE menu. For the details on the equation coefficient data structures, see the reference page for assempde.


A triangular mesh is described by the mesh data which consists of a Point matrix, an Edge matrix, and a Triangle matrix. In the mesh, minimal regions are triangulated into subdomains, and border segments and boundary segments are broken up into edges. Mesh data is created from a decomposed geometry by the function initmesh and can be altered by the functions refinemesh and jigglemesh. The Export Mesh option from the Mesh menu provides another way of creating mesh data. The adaptmesh function creates mesh data as part of the solution process. The mesh may be plotted with the pdemesh function. For details on the mesh data representation, see the reference page for initmesh.


The solution of a PDE problem is represented by the solution vector. A solution gives the value at each mesh point of each dependent variable, perhaps at several points in time, or connected with different eigenvalues. Solution vectors are produced from the mesh, the boundary conditions, and the equation coefficients by assempde, pdenonlin, adaptmesh, parabolic, hyperbolic, and pdeeig. The Export Solution option from the Solve menu exports solutions to the workspace. Since the meaning of a solution vector is dependent on its corresponding mesh data, they are always used together when a solution is presented. For details on solution vectors, see the reference page for assempde.

Post Processing and Presentation

Given a solution/mesh pair, a variety of tools is provided for the visualization and processing of the data. pdeintrp and pdeprtni can be used to interpolate between functions defined at triangle nodes and functions defined at triangle midpoints. tri2grid interpolates a functions from a triangular mesh to a rectangular grid. pdegrad and pdecgrad compute gradients of the solution. pdeplot has a large number of options for plotting the solution. pdecont and pdesurf are convenient shorthands for pdeplot.

Tips for Solving PDEs Programmatically

Use the export facilities of the PDE app as much as you can. They provide data structures with the correct syntax, and these are good starting points that you can modify to suit your needs.

Working with the system matrices and vectors produced by assema and assemb can sometimes be valuable. When solving the same equation for different loads or boundary conditions, it pays to assemble the stiffness matrix only once. Point loads on a particular node can be implemented by adding the load to the corresponding row in the right side vector. A nonlocal constraint can be incorporated into the H and R matrices.

An example of a handwritten Coefficient file is circlef.m, which produces a point load. You can find the full example in pdedemo7 and on the assempde reference page.

The routines for adaptive mesh generation and solution are powerful but can lead to dense meshes and thus long computation times. Setting the Ngen parameter to one limits you to a single refinement step. This step can then be repeated to show the progress of the refinement. The Maxt parameter helps you stop before the adaptive solver generates too many triangles. An example of a handwritten triangle selection function is circlepick, used in pdedemo7. Remember that you always need a decomposed geometry with adaptmesh.

Deformed meshes are easily plotted by adding offsets to the Point matrix p. Assuming two variables stored in the solution vector u:

np = size(p,2); 
pdemesh(p+scale*[u(1:np) u(np+1:np+np)]',e,t)

The time evolution of eigenmodes is obtained by, e.g.,

u1 = u(:,mode)*cos(sqrt(l(mode))*tlist) 											% hyperbolic 

for positive eigenvalues in hyperbolic problems, or

u1 = u(:,mode)*exp(-l(mode)*tlist); 											% parabolic

in parabolic problems. This makes nice animations, perhaps together with deformed mesh plots.

Was this topic helpful?