Note: This page has been translated by MathWorks. Please click here

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

**THIS PAGE DESCRIBES
THE LEGACY WORKFLOW.** New features might not be
compatible with the legacy workflow. For the corresponding step in
the recommended workflow, see f Coefficient for specifyCoefficients.

This section describes how to write the coefficient `f`

in
the equation

$$-\nabla \cdot \left(c\otimes \nabla u\right)+au=f$$

or in similar equations. The number of rows in `f`

indicates *N*,
the number of equations, see Equations You Can Solve Using Legacy Functions. Give `f`

as
any of the following:

A column vector with

*N*components. For example, if*N*= 3,`f`

could be:f = [3;4;10];

A character array with

*N*rows. The rows of the character array are MATLAB^{®}expressions as described in Specify Scalar PDE Coefficients in Character Form, with additional options for nonlinear equations. The additional options are:Represent the

`i`

th component of the solution`u`

using`'u(i)'`

.Similarly, represent the

`i`

th components of the gradients of the solution`u`

using`'ux(i)'`

,`'uy(i)'`

and`'uz(i)'`

.

Pad the rows with spaces so each row has the same number of characters (

`char`

does this automatically). For example, if*N*= 3,`f`

could be:f = char('sin(x)+cos(y)','cosh(x.*y)*(1+u(1).^2)','x.*y./(1+x.^2+y.^2)')

f = sin(x) + cos(y) cosh(x.*y)*(1 + u(1).^2) x.*y./(1 + x.^2 + y.^2)

For 2-D geometry, a function as described in Specify 2-D Scalar Coefficients in Function Form. The function should return a matrix of size

*N*-by-*Nt*, where*Nt*is the number of triangles in the mesh. The function should evaluate`f`

at the triangle centroids, as in Specify 2-D Scalar Coefficients in Function Form. Give solvers the function name as`'`

, or as a function handle @'`filename`

, where`filename`

`filename.m`

is a file on your MATLAB path. For details on writing the function, see Calculate Coefficients in Function Form.For example, if

*N*= 3,`f`

could be:function f = fcoeffunction(p,t,u,time) N = 3; % Number of equations % Triangle point indices it1 = t(1,:); it2 = t(2,:); it3 = t(3,:); % Find centroids of triangles xpts = (p(1,it1) + p(1,it2) + p(1,it3))/3; ypts = (p(2,it1) + p(2,it2) + p(2,it3))/3; [ux,uy] = pdegrad(p,t,u); % Approximate derivatives uintrp = pdeintrp(p,t,u); % Interpolated values at centroids nt = size(t,2); % Number of columns f = zeros(N,nt); % Allocate f % Now the particular functional form of f f(1,:) = xpts - ypts + uintrp(1,:); f(2,:) = 1 + tanh(ux(1,:)) + tanh(uy(3,:)); f(3,:) = (5 + uintrp(3,:)).*sqrt(xpts.^2 + ypts.^2);

Because this function depends on the solution

**u**, if the equation is elliptic, use the`pdenonlin`

solver. The initial value can be all`0`

s in the case of Dirichlet boundary conditions:np = size(p,2); % number of points u0 = zeros(N*np,1); % initial guess

For 3-D geometry, a function as described in Specify 3-D PDE Coefficients in Function Form. The function should return a matrix of size

*N*-by-*Nr*, where*Nr*is the number of points in the region that the solver passes. The function should evaluate`f`

at these points. Give solvers the function as a function handle @, where`filename`

`filename.m`

is a file on your MATLAB path, or is an anonymous function.

It is not reliable to specify `f`

as a scalar
or a single vector of characters. Sometimes the toolbox can expand the
single input to a vector or character array with *N* identical
rows. But you can get an error when the toolbox fails to determine *N*.
Instead of a scalar or a single vector of characters, for reliability specify `f`

as
a column vector or character array with *N* rows.

Was this topic helpful?