specifyCoefficients
This topic describes how to write the coefficient c
in
equations such as
The topic applies to the recommended workflow for including
coefficients in your model using specifyCoefficients
.
For 2-D systems, c
is a tensor with 4N2 elements.
For 3-D systems, c
is a tensor with 9N2 elements.
For a definition of the tensor elements, see Definition of the c Tensor Elements. N is
the number of equations, see Equations You Can Solve Using PDE Toolbox.
To write the coefficient c
for inclusion
in the PDE model via specifyCoefficients
, give c
as
either of the following:
If c
is constant, give a column
vector representing the elements in the tensor.
If c
is not constant, give a function
handle. The function must be of the form
ccoeffunction(location,state)
solvepde
or solvepdeeig
pass the
location
and state
structures to
ccoeffunction
. The function must return a matrix of size
N1-by-Nr, where:
N1 is the length of the vector
representing the c
coefficient. There are several
possible values of N1, detailed in Some c Vectors Can Be Short. For 2-D
geometry, 1 ≤ N1 ≤ 4N2,
and for 3-D geometry, 1 ≤ N1 ≤ 9N2.
Nr is the number of points in the location that the
solver passes. Nr is equal to the length of the
location.x
or any other
location
field. The function should evaluate
c
at these points.
For 2-D systems, the notation represents an N-by-1 matrix with an (i,1)-component
For 3-D systems, the notation represents an N-by-1 matrix with an (i,1)-component
All representations of the c
coefficient
begin with a “flattening” of the tensor to a matrix.
For 2-D systems, the N-by-N-by-2-by-2
tensor flattens to a 2N-by-2N matrix,
where the matrix is logically an N-by-N matrix
of 2-by-2 blocks.
For 3-D systems, the N-by-N-by-3-by-3 tensor flattens to a 3N-by-3N matrix, where the matrix is logically an N-by-N matrix of 3-by-3 blocks.
These matrices further get flattened into a column vector. First the N-by-N matrices of 2-by-2 and 3-by-3 blocks are transformed into "vectors" of 2-by-2 and 3-by-3 blocks. Then the blocks are turned into vectors in the usual column-wise way.
The coefficient vector c
relates to the tensor c as follows. For 2-D systems,
Coefficient c(i,j,k,l)
is in row (4N(j–1) + 4i + 2l + k – 6) of the
vector c
.
For 3-D systems,
Coefficient c(i,j,k,l)
is in row (9N(j–1) + 9i + 3l + k – 12) of
the vector c
.
Often, your tensor c has structure, such as symmetric or block diagonal. In many cases, you can represent c using a smaller vector than one with 4N2 components for 2-D or 9N2 components for 3-D. The following sections give the possibilities.
Scalar c, 2-D Systems. The software interprets a scalar c as a diagonal matrix, with c(i,i,1,1) and c(i,i,2,2) equal to the scalar, and all other entries 0.
Two-Element Column Vector c, 2-D Systems. The software interprets a two-element column vector c as a diagonal matrix, with c(i,i,1,1) and c(i,i,2,2) as the two entries, and all other entries 0.
Three-Element Column Vector c, 2-D Systems. The software interprets a three-element column vector c as a symmetric block diagonal matrix, with c(i,i,1,1) = c(1), c(i,i,2,2) = c(3), and c(i,i,1,2) = c(i,i,2,1) = c(2).
Four-Element Column Vector c, 2-D Systems. The software interprets a four-element column vector c as a block diagonal matrix.
N-Element Column Vector c, 2-D Systems. The software interprets an N-element column vector c as a diagonal matrix.
Caution
If N = 2, 3, or 4, the 2-, 3-, or 4-element column vector form takes precedence over the N-element form. For example, if N = 3, and you have a c matrix of the form
you cannot use the N-element
form of c. Instead, you must use the 2N-element
form. If you give c as the vector [c1;c2;c3]
,
the software interprets c
as a 3-element form:
Instead, use the 2N-element
form [c1;c1;c2;c2;c3;c3]
.
2N-Element Column Vector c, 2-D Systems. The software interprets a 2N-element column vector c as a diagonal matrix.
Caution
If N = 2, the 4-element form takes precedence over the 2N-element form. For example, if your c matrix is
you cannot give c as [c1;c2;c3;c4]
,
because the software interprets this vector as the 4-element form
Instead, use the 3N-element
form [c1;0;c2;c3;0;c4]
or the 4N-element
form [c1;0;0;c2;c3;0;0;c4]
.
3N-Element Column Vector c, 2-D Systems. The software interprets a 3N-element column vector c as a symmetric block diagonal matrix.
Coefficient c(i,j,k,l)
is in row (3i + k + l – 4) of the
vector c
.
4N-Element Column Vector c, 2-D Systems. The software interprets a 4N-element column vector c as a block diagonal matrix.
Coefficient c(i,j,k,l)
is in row (4i + 2l + k – 6) of the
vector c
.
2N(2N+1)/2-Element Column Vector c, 2-D Systems. The software interprets a 2N(2N+1)/2-element column vector c as a symmetric matrix. In the following diagram, • means the entry is symmetric.
Coefficient c(i,j,k,l),
for i < j,
is in row (2j2 – 3j + 4i + 2l + k – 5) of the vector c
.
For i = j,
coefficient c(i,j,k,l)
is in row (2i2 + i + l + k – 4) of the
vector c
.
4N2-Element Column Vector c, 2-D Systems. The software interprets a 4N2-element column vector c as a matrix.
Coefficient c(i,j,k,l)
is in row (4N(j–1) + 4i + 2l + k – 6) of the
vector c
.
Scalar c, 3-D Systems. The software interprets a scalar c as a diagonal matrix, with c(i,i,1,1), c(i,i,2,2), and c(i,i,3,3) equal to the scalar, and all other entries 0.
Three-Element Column Vector c, 3-D Systems. The software interprets a three-element column vector c as a diagonal matrix, with c(i,i,1,1), c(i,i,2,2), and c(i,i,3,3) as the three entries, and all other entries 0.
Six-Element Column Vector c, 3-D Systems. The software interprets a six-element column vector c as a symmetric block diagonal matrix, with
c(i,i,1,1) = c(1)
c(i,i,2,2) = c(3)
c(i,i,1,2) = c(i,i,2,1) = c(2)
c(i,i,1,3) = c(i,i,3,1) = c(4)
c(i,i,2,3) = c(i,i,3,2) = c(5)
c(i,i,3,3) = c(6).
In the following diagram, • means the entry is symmetric.
Nine-Element Column Vector c, 3-D Systems. The software interprets a nine-element column vector c as a block diagonal matrix.
N-Element Column Vector c, 3-D Systems. The software interprets an N-element column vector c as a diagonal matrix.
Caution
If N = 3, 6, or 9, the 3-, 6-, or 9-element column vector form takes precedence over the N-element form. For example, if N = 3, and you have a c matrix of the form
you cannot use the N-element
form of c. If you give c as
the vector [c1;c2;c3]
, the software interprets c
as
a 3-element form:
Instead, use one of these forms:
6N-element form — [c1;0;c1;0;0;c1;c2;0;c2;0;0;c2;c3;0;c3;0;0;c3]
9N-element form — [c1;0;0;0;c1;0;0;0;c1;c2;0;0;0;c2;0;0;0;c2;c3;0;0;0;c3;0;0;0;c3]
3N-Element Column Vector c, 3-D Systems. The software interprets a 3N-element column vector c as a diagonal matrix.
Caution
If N = 3, the 9-element form takes precedence over the 3N-element form. For example, if your c matrix is
you cannot give c as [c1;c2;c3;c4;c5;c6;c7;c8;c9]
,
because the software interprets this vector as the 9-element form
Instead, use one of these forms:
6N-element form — [c1;0;c2;0;0;c3;c4;0;c5;0;0;c6;c7;0;c8;0;0;c9]
9N-element form — [c1;0;0;0;c2;0;0;0;c3;c4;0;0;0;c5;0;0;0;c6;c7;0;0;0;c8;0;0;0;c9]
6N-Element Column Vector c, 3-D Systems. The software interprets a 6N-element column vector c as a symmetric block diagonal matrix. In the following diagram, • means the entry is symmetric.
Coefficient c(i,j,k,l)
is in row (6i + k + 1/2l(l–1) – 6) of the
vector c
.
9N-Element Column Vector c, 3-D Systems. The software interprets a 9N-element column vector c as a block diagonal matrix.
Coefficient c(i,j,k,l)
is in row (9i + 3l + k – 12) of
the vector c
.
3N(3N+1)/2-Element Column Vector c, 3-D Systems. The software interprets a 3N(3N+1)/2-element column vector c as a symmetric matrix. In the following diagram, • means the entry is symmetric.
Coefficient c(i,j,k,l),
for i < j,
is in row (9(j–1)(j–2)/2 + 6(j–1) + 9i + 3l + k – 12) of
the vector c
. For i = j, coefficient c(i,j,k,l)
is in row (9(i–1)(i–2)/2 + 15(i–1) + 1/2l(l–1) + k) of
the vector c
.
9N2-Element Column Vector c, 3-D Systems. The software interprets a 9N2-element column vector c as a matrix.
Coefficient c(i,j,k,l)
is in row (9N(j–1) + 9i + 3l + k – 12) of
the vector c
.
If your c
coefficient is not constant, represent
it as a function of the form
ccoeffunction(location,state)
solvepde
or solvepdeeig
pass the
location
and state
structures to
ccoeffunction
. The function must return a matrix of size
N1-by-Nr, where:
N1 is the number of coefficients you pass to the solver. There are several possible values of N1, detailed in Some c Vectors Can Be Short. For 2-D geometry, 1 ≤ N1 ≤ 4N2, and for 3-D geometry, 1 ≤ N1 ≤ 9N2.
Nr is the number of points in the location that the solver
passes. Nr is equal to the length of the
location.x
or any other location
field. The function should evaluate c
at these points.
Pass the coefficient to specifyCoefficients
as
a function handle, such as
specifyCoefficients(model,'c',@ccoeffunction,...)
location
is a structure with these fields:
location.x
location.y
location.z
location.subdomain
The fields x
, y
, and
z
represent the x-,
y-, and z- coordinates of points for
which your function calculates coefficient values. The
subdomain
field represents the subdomain numbers, which
currently apply only to 2-D models. The location fields are row vectors.
state
is a structure with these
fields:
state.u
state.ux
state.uy
state.uz
state.time
The state.u
field represents the current value of the solution
u. The state.ux
,
state.uy
, and state.uz
fields are
estimates of the solution’s partial derivatives
(∂u/∂x,
∂u/∂y, and
∂u/∂z) at the corresponding points of
the location structure. The solution and gradient estimates are
N-by-Nr matrices. The
state.time
field is a scalar representing time for
time-dependent models.
For example, suppose N =
3, and you have 2-D geometry. Suppose your c
matrix
is of the form
where unlisted elements are zero. Here s1(x,y) is 5 in subdomain 1, and is 10 in subdomain 2.
This c
is a symmetric, block-diagonal matrix
with different coefficients in each block. So it is natural to represent c
as
a 3N-Element Column Vector c, 2-D Systems:
For that form, the following function is appropriate.
function cmatrix = ccoeffunction(location,state)
n1 = 9;
nr = numel(location.x);
cmatrix = zeros(n1,nr);
cmatrix(1,:) = ones(1,nr);
cmatrix(2,:) = 2*ones(1,nr);
cmatrix(3,:) = 8*ones(1,nr);
cmatrix(4,:) = 1+location.x.^2 + location.y.^2;
cmatrix(5,:) = state.u(2,:)./(1 + state.u(1,:).^2 + state.u(3,:).^2);
cmatrix(6,:) = cmatrix(4,:);
cmatrix(7,:) = 5*location.subdomain;
cmatrix(8,:) = -ones(1,nr);
cmatrix(9,:) = cmatrix(7,:);
To include this function as your c
coefficient,
pass the function handle @ccoeffunction
:
specifyCoefficients(model,'c',@ccoeffunction,...