Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_pde_2d_ellip_fd (d03eb)

## Purpose

nag_pde_2d_ellip_fd (d03eb) uses the Strongly Implicit Procedure to calculate the solution to a system of simultaneous algebraic equations of five-point molecule form on a two-dimensional topologically-rectangular mesh. (‘Topological’ means that a polar grid, for example (r,θ)$\left(r,\theta \right)$, can be used, being equivalent to a rectangular box.)

## Syntax

[t, itcoun, itused, resids, chngs, ifail] = d03eb(n1, a, b, c, d, e, q, t, aparam, itmax, itcoun, ndir, ixn, iyn, conres, conchn, 'n2', n2)
[t, itcoun, itused, resids, chngs, ifail] = nag_pde_2d_ellip_fd(n1, a, b, c, d, e, q, t, aparam, itmax, itcoun, ndir, ixn, iyn, conres, conchn, 'n2', n2)

## Description

Given a set of simultaneous equations
 Mt = q $Mt=q$ (1)
(which could be nonlinear) derived, for example, from a finite difference representation of a two-dimensional elliptic partial differential equation and its boundary conditions, the function determines the values of the dependent variable t$t$. q$q$ is a known vector of length n1 × n2${n}_{1}×{n}_{2}$ and M$M$ is a square (n1 × n2)$\left({n}_{1}×{n}_{2}\right)$ by (n1 × n2)$\left({n}_{1}×{n}_{2}\right)$ matrix.
The equations must be of five-diagonal form:
 aijti,j − 1 + bijti − 1,j + cijtij + dijti + 1,j + eijti,j + 1 = qij $aijti,j-1+bijti-1,j+cijtij+dijti+1,j+eijti,j+1=qij$
for i = 1,2,,n1$i=1,2,\dots ,{n}_{1}$ and j = 1,2,,n2$j=1,2,\dots ,{n}_{2}$, provided cij0.0${c}_{ij}\ne 0.0$. Indeed, if cij = 0.0${c}_{ij}=0.0$, then the equation is assumed to be
 tij = qij. $tij=qij.$
For example, if n1 = 3${n}_{1}=3$ and n2 = 2${n}_{2}=2$, the equations take the form:
 [ c11 d11 e11 b21 c21 d21 e21 b31 c31 e31 a12 c12 d12 a22 b22 c22 d22 a32 b32 c32 ]
 [ t11 t21 t31 t12 t22 t32 ]
=
 [ q11 q21 q31 q12 q22 q32 ]
.
$[ c11 d11 e11 b21 c21 d21 e21 b31 c31 e31 a12 c12 d12 a22 b22 c22 d22 a32 b32 c32 ] [ t11 t21 t31 t12 t22 t32 ]=[ q11 q21 q31 q12 q22 q32 ] .$
The system is solved iteratively, from a starting approximation t(1)${t}^{\left(1\right)}$, by the formulae
 r(n) = q − Mt(n) Ms(n) = r(n) t(n + 1) = t(n) + s(n).
$r (n) =q-Mt(n) Ms(n) =r (n) t (n+1) =t (n) +s (n) .$
Thus r(n)${r}^{\left(n\right)}$ is the residual of the n$n$th approximate solution t(n)${t}^{\left(n\right)}$, and s(n)${s}^{\left(n\right)}$ is the update change vector. The calling program supplies an initial approximation for the values of the dependent variable in the array t, the coefficients of the five-point molecule system of equations in the arrays a, b, c, d and e, and the source terms in the array q. The function derives the residual of the latest approximate solution and then uses the approximate LU$LU$ factorization of the Strongly Implicit Procedure with the necessary acceleration parameter adjustment by calling nag_pde_2d_ellip_fd_iter (d03ua) at each iteration. nag_pde_2d_ellip_fd (d03eb) combines the newly derived change with the old approximation to obtain the new approximate solution for t$t$. The new solution is checked for convergence against the user-supplied convergence criteria and if these have not been achieved the iterative cycle is repeated. Convergence is based on both the maximum absolute normalized residuals (calculated with reference to the previous approximate solution as these are calculated at the commencement of each iteration) and on the maximum absolute change made to the values of t$t$.
Problems in topologically non-rectangular regions can be solved using the function by surrounding the region by a circumscribing topological rectangle. The equations for the nodal values external to the region of interest are set to zero (i.e., cij = tij = 0${c}_{ij}={t}_{ij}=0$) and the boundary conditions are incorporated into the equations for the appropriate nodes.
If there is no better initial approximation when starting the iterative cycle, an array of all zeros can be used as the initial approximation.
The function can be used to solve linear elliptic equations in which case the arrays a, b, c, d, e and q are constants and for which a single call provides the required solution. It can also be used to solve nonlinear elliptic equations in which case some or all of these arrays may require updating during the progress of the iterations as more accurate solutions are derived. The function will then have to be called repeatedly in an outer iterative cycle. Dependent on the nonlinearity, some under relaxation of the coefficients and/or source terms may be needed during their recalculation using the new estimates of the solution.
The function can also be used to solve each step of a time-dependent parabolic equation in two space dimensions. The solution at each time step can be expressed in terms of an elliptic equation if the Crank–Nicolson or other form of implicit time integration is used.
Neither diagonal dominance, nor positive-definiteness, of the matrix M$M$ formed from the arrays a, b, c, d, e is necessary to ensure convergence.
For problems in which the solution is not unique in the sense that an arbitrary constant can be added to the solution, for example Laplace's equation with all Neumann boundary conditions, a parameter is incorporated so that the solution can be rescaled by subtracting a specified nodal value from the whole solution t$t$ after the completion of every iteration to keep rounding errors to a minimum for those cases when the convergence is slow.

## References

Jacobs D A H (1972) The strongly implicit procedure for the numerical solution of parabolic and elliptic partial differential equations Note RD/L/N66/72 Central Electricity Research Laboratory
Stone H L (1968) Iterative solution of implicit approximations of multi-dimensional partial differential equations SIAM J. Numer. Anal. 5 530–558

## Parameters

### Compulsory Input Parameters

1:     n1 – int64int32nag_int scalar
The number of nodes in the first coordinate direction, n1${n}_{1}$.
Constraint: n1 > 1${\mathbf{n1}}>1$.
2:     a(lda,n2) – double array
lda, the first dimension of the array, must satisfy the constraint ldan1$\mathit{lda}\ge {\mathbf{n1}}$.
a(i,j)${\mathbf{a}}\left(\mathit{i},\mathit{j}\right)$ must contain the coefficient of the ‘southerly’ term involving ti,j1${t}_{\mathit{i},\mathit{j}-1}$ in the (i,j)$\left(\mathit{i},\mathit{j}\right)$th equation of the system (1), for i = 1,2,,n1$\mathit{i}=1,2,\dots ,{\mathbf{n1}}$ and j = 1,2,,n2$\mathit{j}=1,2,\dots ,{\mathbf{n2}}$. The elements of a, for j = 1$j=1$, must be zero after incorporating the boundary conditions, since they involve nodal values from outside the rectangle.
3:     b(lda,n2) – double array
lda, the first dimension of the array, must satisfy the constraint ldan1$\mathit{lda}\ge {\mathbf{n1}}$.
b(i,j)${\mathbf{b}}\left(\mathit{i},\mathit{j}\right)$ must contain the coefficient of the ‘westerly’ term involving ti1,j${t}_{i-1,j}$ in the (i,j)$\left(i,j\right)$th equation of the system (1), for i = 1,2,,n1$\mathit{i}=1,2,\dots ,{\mathbf{n1}}$ and j = 1,2,,n2$\mathit{j}=1,2,\dots ,{\mathbf{n2}}$. The elements of b, for i = 1$i=1$, must be zero after incorporating the boundary conditions, since they involve nodal values from outside the rectangle.
4:     c(lda,n2) – double array
lda, the first dimension of the array, must satisfy the constraint ldan1$\mathit{lda}\ge {\mathbf{n1}}$.
c(i,j)${\mathbf{c}}\left(\mathit{i},\mathit{j}\right)$ must contain the coefficient of the ‘central’ term involving tij${t}_{\mathit{i}\mathit{j}}$ in the (i,j)$\left(\mathit{i},\mathit{j}\right)$th equation of the system (1), for i = 1,2,,n1$\mathit{i}=1,2,\dots ,{\mathbf{n1}}$ and j = 1,2,,n2$\mathit{j}=1,2,\dots ,{\mathbf{n2}}$. The elements of c are checked to ensure that they are nonzero. If any element is found to be zero, the corresponding algebraic equation is assumed to be tij = qij${t}_{\mathit{i}\mathit{j}}={q}_{\mathit{i}\mathit{j}}$. This feature can be used to define the equations for nodes at which, for example, Dirichlet boundary conditions are applied, or for nodes external to the problem of interest, by setting c(i,j) = 0.0${\mathbf{c}}\left(\mathit{i},\mathit{j}\right)=0.0$ at appropriate points, and the corresponding value of q(i,j)${\mathbf{q}}\left(\mathit{i},\mathit{j}\right)$ to the appropriate value, namely the prescribed value of t(i,j)${\mathbf{t}}\left(\mathit{i},\mathit{j}\right)$ in the Dirichlet case or zero at an external point.
5:     d(lda,n2) – double array
lda, the first dimension of the array, must satisfy the constraint ldan1$\mathit{lda}\ge {\mathbf{n1}}$.
d(i,j)${\mathbf{d}}\left(\mathit{i},\mathit{j}\right)$ must contain the coefficient of the ‘easterly’ term involving ti + 1,j${t}_{\mathit{i}+1,\mathit{j}}$ in the (i,j)$\left(\mathit{i},\mathit{j}\right)$th equation of the system (1), for i = 1,2,,n1$\mathit{i}=1,2,\dots ,{\mathbf{n1}}$ and j = 1,2,,n2$\mathit{j}=1,2,\dots ,{\mathbf{n2}}$. The elements of d, for i = n1$i={\mathbf{n1}}$, must be zero after incorporating the boundary conditions, since they involve nodal values from outside the rectangle.
6:     e(lda,n2) – double array
lda, the first dimension of the array, must satisfy the constraint ldan1$\mathit{lda}\ge {\mathbf{n1}}$.
e(i,j)${\mathbf{e}}\left(\mathit{i},\mathit{j}\right)$ must contain the coefficient of the ‘northerly’ term involving ti,j + 1${t}_{\mathit{i},\mathit{j}+1}$ in the (i,j)$\left(\mathit{i},\mathit{j}\right)$th equation of the system (1), for i = 1,2,,n1$\mathit{i}=1,2,\dots ,{\mathbf{n1}}$ and j = 1,2,,n2$\mathit{j}=1,2,\dots ,{\mathbf{n2}}$. The elements of e, for j = n2$j={\mathbf{n2}}$ must be zero after incorporating the boundary conditions, since they involve nodal values from outside the rectangle.
7:     q(lda,n2) – double array
lda, the first dimension of the array, must satisfy the constraint ldan1$\mathit{lda}\ge {\mathbf{n1}}$.
q(i,j)${\mathbf{q}}\left(\mathit{i},\mathit{j}\right)$ must contain qij${q}_{\mathit{i}\mathit{j}}$, for i = 1,2,,n1$\mathit{i}=1,2,\dots ,{\mathbf{n1}}$ and j = 1,2,,n2$\mathit{j}=1,2,\dots ,{\mathbf{n2}}$, i.e., the source term values at the nodal points for the system (1).
8:     t(lda,n2) – double array
lda, the first dimension of the array, must satisfy the constraint ldan1$\mathit{lda}\ge {\mathbf{n1}}$.
t(i,j)${\mathbf{t}}\left(\mathit{i},\mathit{j}\right)$ must contain the element tij${t}_{\mathit{i}\mathit{j}}$ of the approximate solution to the equations supplied by the calling program as an initial starting value, for i = 1,2,,n1$\mathit{i}=1,2,\dots ,{\mathbf{n1}}$ and j = 1,2,,n2$\mathit{j}=1,2,\dots ,{\mathbf{n2}}$.
If no better approximation is known, an array of zeros can be used.
9:     aparam – double scalar
The iteration acceleration factor. A value of 1.0$1.0$ is adequate for most typical problems. However, if convergence is slow, the value can be reduced, typically to 0.2$0.2$ or 0.1$0.1$. If divergence is obtained, the value can be increased, typically to 2.0$2.0$, 5.0$5.0$ or 10.0$10.0$.
Constraint: 0.0 < aparam((n11)2 + (n21)2) / 2.0$0.0<{\mathbf{aparam}}\le \left({\left({\mathbf{n1}}-1\right)}^{2}+{\left({\mathbf{n2}}-1\right)}^{2}\right)/2.0$.
10:   itmax – int64int32nag_int scalar
The maximum number of iterations to be used by the function in seeking the solution. A reasonable value might be 30$30$ if n1 = n2 = 10${\mathbf{n1}}={\mathbf{n2}}=10$ or 100$100$ if n1 = n2 = 50${\mathbf{n1}}={\mathbf{n2}}=50$.
11:   itcoun – int64int32nag_int scalar
On the first call of nag_pde_2d_ellip_fd (d03eb), itcoun must be set to 0$0$. On subsequent entries, its value must be unchanged from the previous call.
12:   ndir – int64int32nag_int scalar
Indicates whether or not the system of equations has a unique solution. For systems which have a unique solution, ndir must be set to any nonzero value. For systems derived from, for example, Laplace's equation with all Neumann boundary conditions, i.e., problems in which an arbitrary constant can be added to the solution, ndir should be set to 0$0$ and the values of the next two parameters must be specified. For such problems the function subtracts the value of the function derived at the node (ixn, iyn) from the whole solution after each iteration to reduce the possibility of large rounding errors. You must also ensure that for such problems the appropriate consistency condition on the source terms q is satisfied.
13:   ixn – int64int32nag_int scalar
Is ignored unless ndir is equal to zero, in which case it must specify the first index of the nodal point at which the solution is to be set to zero. The node should not correspond to a corner node, or to a node external to the region of interest.
14:   iyn – int64int32nag_int scalar
Is ignored unless ndir is equal to zero, in which case it must specify the second index of the nodal point at which the solution is to be set to zero. The node should not correspond to a corner node, or to a node external to the region of interest.
15:   conres – double scalar
The convergence criterion to be used on the maximum absolute value of the normalized residual vector components. The latter is defined as the residual of the algebraic equation divided by the central coefficient when the latter is not equal to 0.0$0.0$, and defined as the residual when the central coefficient is zero.
Clearly conres should not be less than a reasonable multiple of the machine precision.
16:   conchn – double scalar
The convergence criterion to be used on the maximum absolute value of the change made at each iteration to the elements of the array t, namely the dependent variable. Clearly conchn should not be less than a reasonable multiple of the machine precision multiplied by the maximum value of t attained.
Convergence is achieved when both the convergence criteria are satisfied. You can therefore set convergence on either the residual or on the change, or (as is recommended) on a requirement that both are below prescribed limits.

### Optional Input Parameters

1:     n2 – int64int32nag_int scalar
Default: The second dimension of the arrays a, b, c, d, e, q, t. (An error is raised if these dimensions are not equal.)
The number of nodes in the second coordinate direction, n2${n}_{2}$.
Constraint: n2 > 1${\mathbf{n2}}>1$.

### Input Parameters Omitted from the MATLAB Interface

lda wrksp1 wrksp2 wrksp3

### Output Parameters

1:     t(lda,n2) – double array
ldan1$\mathit{lda}\ge {\mathbf{n1}}$.
The solution derived by the function.
2:     itcoun – int64int32nag_int scalar
Its value is increased by the number of iterations used on this call (namely itused). It therefore stores the accumulated number of iterations actually used. For subsequent calls for the same problem, i.e., with the same n1 and n2 but possibly different coefficients and/or source terms, as occur with nonlinear systems or with time-dependent systems, itcoun is the accumulated number of iterations. This applies to the second and subsequent calls to nag_pde_2d_ellip_fd (d03eb). In this way a suitable cycling of the sequence of iteration parameters is obtained in the calls to nag_pde_2d_ellip_fd_iter (d03ua).
3:     itused – int64int32nag_int scalar
The number of iterations actually used on that call.
4:     resids(itmax) – double array
The maximum absolute value of the residuals calculated at the i$\mathit{i}$th iteration, for i = 1,2,,itused$\mathit{i}=1,2,\dots ,{\mathbf{itused}}$. If you want to know the maximum absolute residual of the solution which is returned you must calculate this in the calling program. The sequence of values resids indicates the rate of convergence.
5:     chngs(itmax) – double array
The maximum absolute value of the changes made to the components of the dependent variable t at the i$\mathit{i}$th iteration, for i = 1,2,,itused$\mathit{i}=1,2,\dots ,{\mathbf{itused}}$. The sequence of values chngs indicates the rate of convergence.
6:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Errors or warnings detected by the function:
ifail = 1${\mathbf{ifail}}=1$
 On entry, n1 < 2${\mathbf{n1}}<2$, or n2 < 2${\mathbf{n2}}<2$.
ifail = 2${\mathbf{ifail}}=2$
 On entry, lda < n1$\mathit{lda}<{\mathbf{n1}}$.
ifail = 3${\mathbf{ifail}}=3$
 On entry, aparam ≤ 0.0${\mathbf{aparam}}\le 0.0$.
ifail = 4${\mathbf{ifail}}=4$
 On entry, aparam > ((n1 − 1)2 + (n2 − 1)2) / 2.0${\mathbf{aparam}}>\left({\left({\mathbf{n1}}-1\right)}^{2}+{\left({\mathbf{n2}}-1\right)}^{2}\right)/2.0$.
ifail = 5${\mathbf{ifail}}=5$
Convergence was not achieved after itmax iterations.

## Accuracy

The improvement in accuracy for each iteration depends on the size of the system and on the condition of the update matrix characterised by the five-diagonal coefficient arrays. The ultimate accuracy obtainable depends on the above factors and on the machine precision. The rate of convergence obtained with the Strongly Implicit Procedure is not always smooth because of the cyclic use of nine acceleration parameters. The convergence may become slow with very large problems, for example when n1 = n2 = 60${\mathbf{n1}}={\mathbf{n2}}=60$. The final accuracy may be judged approximately from the rate of convergence determined from the sequence of values returned in chngs and the magnitude of the maximum absolute value of the change vector on the last iteration stored in ${\mathbf{chngs}}\left({\mathbf{itused}}\right)$.

The time taken per iteration is approximately proportional to n1 × n2${\mathbf{n1}}×{\mathbf{n2}}$.
Convergence may not always be obtained when the problem is very large and/or the coefficients of the equations have widely disparate values. The latter case is often associated with a near ill-conditioned matrix.

## Example

This example solves Laplace's equation in a rectangle with a non-uniform grid spacing in the x$x$ and y$y$ coordinate directions and with Dirichlet boundary conditions specifying the function on the perimeter of the rectangle equal to
 e(1.0 + x) / y(n2) × cos(y / y(n2)). $e(1.0+x)/y(n2)×cos(y/y(n2)).$
```function nag_pde_2d_ellip_fd_example
% Initialize variables and arrays.
aparam = 1;
itmax = 18;
itcoun = 0;
ndir = 1;
ixn = 0;
iyn = 0;
conres = 1e-06;
conchn = 1e-06;
x = [0; 1; 3; 6; 10; 15];
y = [0; 1; 3; 6; 10; 15; 21; 28; 36; 45];
n1 = 6;
n2 = 10;
k = 1:n1;
p = 1:n2;

% Set up difference equation coefficients, source terms and
% initial conditions.
a = zeros(n1, n2);
e = zeros(n1, n2);
b = zeros(n1, n2);
d = zeros(n1, n2);
c = zeros(n1, n2);
q = zeros(n1, n2);
t = zeros(n1, n2);
for j = 1:n2
for i = 1:n1
if ((i ~= 1) && (i ~= n1) && (j ~= 1) && (j ~= n2))
% Specification for internal nodes
a(i,j) = double(2.0/((y(j)-y(j-1))*(y(j+1)-y(j-1))));
e(i,j) = double(2.0/((y(j+1)-y(j))*(y(j+1)-y(j-1))));
b(i,j) = double(2.0/((x(i)-x(i-1))*(x(i+1)-x(i-1))));
d(i,j) = double(2.0/((x(i+1)-x(i))*(x(i+1)-x(i-1))));
c(i,j) = double(-a(i,j) - b(i,j) - d(i,j) - e(i,j));
q(i,j) = double(0.0);
t(i,j) = double(0.0);
else
% Specification for boundary nodes
a(i,j) = double(0.0);
e(i,j) = double(0.0);
b(i,j) = double(0.0);
d(i,j) = double(0.0);
c(i,j) = double(0.0);
q(i,j) = double(exp((x(i)+1.0)/y(n2))*cos(y(j)/y(n2)));
t(i,j) = double(0.0);
end
end
end

fprintf('nag_pde_2d_ellip_fd example program results\n\n');

[tOut, itcounOut, itused, resids, chngs, ifail] = ...
nag_pde_2d_ellip_fd(int64(n1), a, b, c, d, e, q, t, aparam, int64(itmax), ...
int64(itcoun), int64(ndir), int64(ixn), int64(iyn), conres, conchn);
if ifail ~= 0
% Parameters out of range, or convergence problems.
% Print message and exit.
error('Warning: nag_pde_2d_ellip_fd returned with ifail = %1.0f ',ifail);
end

% Output results.
disp('Iteration      Maximum             Maximum');
disp(' number        residual             change');
for i = 1:int32(itused) % can't use int64 in loop range.
fprintf('  %4.1f        %4.4d        %4.4d\n',i, resids(i), chngs(i));
end
fprintf('\nTable of calculated function values\n\n');
disp('        I     1         2          3         4         5         6');
disp('   J');
for j = 1:int32(n2); k = 1:int32(n1); % can't use int64 in loop range.
disp(sprintf('%8.4f  ', j, tOut(k,j)));
end

% Plot results.
fig = figure('Number', 'off');
display_plot(x, y, tOut)

function display_plot(x, y, tOut)
% Formatting for title and axis labels.
titleFmt = {'FontName', 'Helvetica', 'FontWeight', 'Bold', 'FontSize', 14};
labFmt = {'FontName', 'Helvetica', 'FontWeight', 'Bold', 'FontSize', 13};
set(gca, 'FontSize', 13); % for legend, axis tick labels, etc.
% Plot array as a mesh and contours.
meshc(y, x, tOut)
% Set the title.
title({'Solution to Laplace''s Equation', ['using the Strongly ', ...
'Implicit Procedure on a Five-point Molecule Discretisation']}, ...
titleFmt{:});
% Label the axes.
xlabel('Y', labFmt{:});
ylabel('X', labFmt{:});
zlabel('U', labFmt{:});
% Set the axes limits tight to the x, y and z range.
axis([y(1) y(end) x(1) x(end) 0.5 1.5]);
% Set the view to something nice (determined empirically).
view(32.5, 30);
```
```
nag_pde_2d_ellip_fd example program results

Iteration      Maximum             Maximum
number        residual             change
1.0        1.4270e+00        1.4270e+00
2.0        6.6707e-03        2.1755e-02
3.0        8.4223e-04        1.6213e-03
4.0        7.6352e-05        1.8100e-04
5.0        5.4344e-06        1.1987e-05
6.0        6.4713e-07        1.2453e-06
7.0        5.4672e-08        1.0814e-07

Table of calculated function values

I     1         2          3         4         5         6
J
1.0000    1.0225    1.0454    1.0930    1.1683    1.2769    1.4270
2.0000    1.0222    1.0452    1.0927    1.1680    1.2766    1.4266
3.0000    1.0202    1.0431    1.0906    1.1658    1.2741    1.4238
4.0000    1.0134    1.0362    1.0833    1.1580    1.2657    1.4143
5.0000    0.9973    1.0198    1.0662    1.1397    1.2457    1.3919
6.0000    0.9662    0.9879    1.0329    1.1042    1.2068    1.3484
7.0000    0.9131    0.9337    0.9762    1.0436    1.1406    1.2744
8.0000    0.8308    0.8496    0.8882    0.9495    1.0378    1.1595
9.0000    0.7124    0.7284    0.7616    0.8141    0.8898    0.9942
10.0000    0.5524    0.5649    0.5905    0.6312    0.6899    0.7710

```
```function d03eb_example
% Initialize variables and arrays.
aparam = 1;
itmax = 18;
itcoun = 0;
ndir = 1;
ixn = 0;
iyn = 0;
conres = 1e-06;
conchn = 1e-06;
x = [0; 1; 3; 6; 10; 15];
y = [0; 1; 3; 6; 10; 15; 21; 28; 36; 45];
n1 = 6;
n2 = 10;
k = 1:n1;
p = 1:n2;

% Set up difference equation coefficients, source terms and
% initial conditions.
a = zeros(n1, n2);
e = zeros(n1, n2);
b = zeros(n1, n2);
d = zeros(n1, n2);
c = zeros(n1, n2);
q = zeros(n1, n2);
t = zeros(n1, n2);
for j = 1:n2
for i = 1:n1
if ((i ~= 1) && (i ~= n1) && (j ~= 1) && (j ~= n2))
% Specification for internal nodes
a(i,j) = double(2.0/((y(j)-y(j-1))*(y(j+1)-y(j-1))));
e(i,j) = double(2.0/((y(j+1)-y(j))*(y(j+1)-y(j-1))));
b(i,j) = double(2.0/((x(i)-x(i-1))*(x(i+1)-x(i-1))));
d(i,j) = double(2.0/((x(i+1)-x(i))*(x(i+1)-x(i-1))));
c(i,j) = double(-a(i,j) - b(i,j) - d(i,j) - e(i,j));
q(i,j) = double(0.0);
t(i,j) = double(0.0);
else
% Specification for boundary nodes
a(i,j) = double(0.0);
e(i,j) = double(0.0);
b(i,j) = double(0.0);
d(i,j) = double(0.0);
c(i,j) = double(0.0);
q(i,j) = double(exp((x(i)+1.0)/y(n2))*cos(y(j)/y(n2)));
t(i,j) = double(0.0);
end
end
end

fprintf('d03eb example program results\n\n');

[tOut, itcounOut, itused, resids, chngs, ifail] = ...
d03eb(int64(n1), a, b, c, d, e, q, t, aparam, int64(itmax), ...
int64(itcoun), int64(ndir), int64(ixn), int64(iyn), conres, conchn);
if ifail ~= 0
% Parameters out of range, or convergence problems.
% Print message and exit.
error('Warning: d03eb returned with ifail = %1.0f ',ifail);
end

% Output results.
disp('Iteration      Maximum             Maximum');
disp(' number        residual             change');
for i = 1:int32(itused) % can't use int64 in loop range.
fprintf('  %4.1f        %4.4d        %4.4d\n',i, resids(i), chngs(i));
end
fprintf('\nTable of calculated function values\n\n');
disp('        I     1         2          3         4         5         6');
disp('   J');
for j = 1:int32(n2); k = 1:int32(n1); % can't use int64 in loop range.
disp(sprintf('%8.4f  ', j, tOut(k,j)));
end

% Plot results.
fig = figure('Number', 'off');
display_plot(x, y, tOut)

function display_plot(x, y, tOut)
% Formatting for title and axis labels.
titleFmt = {'FontName', 'Helvetica', 'FontWeight', 'Bold', 'FontSize', 14};
labFmt = {'FontName', 'Helvetica', 'FontWeight', 'Bold', 'FontSize', 13};
set(gca, 'FontSize', 13); % for legend, axis tick labels, etc.
% Plot array as a mesh and contours.
meshc(y, x, tOut)
% Set the title.
title({'Solution to Laplace''s Equation', ['using the Strongly ', ...
'Implicit Procedure on a Five-point Molecule Discretisation']}, ...
titleFmt{:});
% Label the axes.
xlabel('Y', labFmt{:});
ylabel('X', labFmt{:});
zlabel('U', labFmt{:});
% Set the axes limits tight to the x, y and z range.
axis([y(1) y(end) x(1) x(end) 0.5 1.5]);
% Set the view to something nice (determined empirically).
view(32.5, 30);
```
```
d03eb example program results

Iteration      Maximum             Maximum
number        residual             change
1.0        1.4270e+00        1.4270e+00
2.0        6.6707e-03        2.1755e-02
3.0        8.4223e-04        1.6213e-03
4.0        7.6352e-05        1.8100e-04
5.0        5.4344e-06        1.1987e-05
6.0        6.4713e-07        1.2453e-06
7.0        5.4672e-08        1.0814e-07

Table of calculated function values

I     1         2          3         4         5         6
J
1.0000    1.0225    1.0454    1.0930    1.1683    1.2769    1.4270
2.0000    1.0222    1.0452    1.0927    1.1680    1.2766    1.4266
3.0000    1.0202    1.0431    1.0906    1.1658    1.2741    1.4238
4.0000    1.0134    1.0362    1.0833    1.1580    1.2657    1.4143
5.0000    0.9973    1.0198    1.0662    1.1397    1.2457    1.3919
6.0000    0.9662    0.9879    1.0329    1.1042    1.2068    1.3484
7.0000    0.9131    0.9337    0.9762    1.0436    1.1406    1.2744
8.0000    0.8308    0.8496    0.8882    0.9495    1.0378    1.1595
9.0000    0.7124    0.7284    0.7616    0.8141    0.8898    0.9942
10.0000    0.5524    0.5649    0.5905    0.6312    0.6899    0.7710

```