## Set Initial Conditions

### What Are Initial Conditions?

The term *initial condition* has two meanings:

For time-dependent problems, the initial condition is the solution

*u*at the initial time, and also the initial time-derivative if the`m`

coefficient is nonzero. Set the initial condition in the model using`setInitialConditions`

.For nonlinear stationary problems, the initial condition is a guess or approximation of the solution

*u*at the initial iteration of the nonlinear solver. Set the initial condition in the model using`setInitialConditions`

.If you do not specify the initial condition for a stationary problem,

`solvepde`

uses the zero function for the initial iteration.

### Constant Initial Conditions

For a system of *N* equations, you can give
constant initial conditions as either a scalar or as a vector with *N* components.
For example, if the initial condition is *u* = 15
for all components, use the following command.

setInitialConditions(model,15);

If *N* = 3, and the initial condition is 15
for the first equation, 0 for the second equation, and –3 for
the third equation, use the following commands.

u0 = [15,0,-3]; setInitialConditions(model,u0);

If the `m`

coefficient is nonzero, give an
initial condition for the time derivative as well. Set this initial
derivative in the same form as the first initial condition. For example,
if the initial derivative of the solution is `[4,3,0]`

,
use the following commands.

u0 = [15,0,-3]; ut0 = [4,3,0]; setInitialConditions(model,u0,ut0);

### Nonconstant Initial Conditions

If your initial conditions are not constant, set them by writing a function of the form.

`function u0 = initfun(location)`

`solvepde`

computes and populates the data in the
`location`

structure array and passes this data to your function.
You can define your function so that its output depends on this data. You can use any
name instead of `location`

. To use additional arguments in your
function, wrap your function (that takes additional arguments) with an anonymous
function that takes only the `location`

argument. For example:

u0 = @(location) initfunWithAdditionalArgs(location,arg1,arg2...) setInitialConditions(model,u0)

`location`

is a structure array with fields `location.x`

,
`location.y`

, and, for 3-D problems, `location.z`

.
Your function must return a matrix `u0`

of size
*N*-by-`M`

, where *N* is the
number of equations in your PDE and `M = length(location.x)`

. The
fields in `location`

are row vectors.

For example, suppose you have a 2-D problem with *N* =
2 equations:

$$\begin{array}{l}\frac{{\partial}^{2}u}{\partial {t}^{2}}-\nabla \xb7\left(\nabla u\right)=\left[\begin{array}{c}3+x\\ 4-x-y\end{array}\right]\\ u(0)=\left[\begin{array}{c}4+{x}^{2}+{y}^{2}\\ 0\end{array}\right]\\ \frac{\partial u}{\partial t}(0)=\left[\begin{array}{c}0\\ \mathrm{sin}(xy)\end{array}\right]\end{array}$$

This problem has `m`

= 1, `c`

=
1, and `f`

= $$\left[\begin{array}{c}3+x\\ 4-x-y\end{array}\right]$$.
Because `m`

is nonzero, give both an initial value
of *u* and an initial value of the derivative of *u*.

Write the following function files. Save them to a location
on your MATLAB^{®} path.

```
function uinit = u0fun(location)
M = length(location.x);
uinit = zeros(2,M);
uinit(1,:) = 4 + location.x.^2 + location.y.^2;
```

```
function utinit = ut0fun(location)
M = length(location.x);
utinit = zeros(2,M);
utinit(2,:) = sin(location.x.*location.y);
```

Pass the initial conditions to your PDE model:

u0 = @u0fun; ut0 = @ut0fun; setInitialConditions(model,u0,ut0);

### Nodal Initial Conditions

You can use results of previous analysis as nodal initial conditions
for your current model. The geometry and mesh of the model you used
to obtain the results and the current model must be the same. For
example, solve a time-dependent PDE problem for times from `t0`

to `t1`

with
a time step `tstep`

.

results = solvepde(model,t0:tstep:t1);

If later you need to solve this PDE problem for times from `t1`

to `t2`

,
you can use `results`

to set initial conditions.
If you do not explicitly specify the time step, `setInitialConditions`

uses `results`

corresponding
to the last solution time, `t1`

.

setInitialConditions(model,results)

To use `results`

for a particular solution
time instead of the last one, specify the solution time index as a
third parameter of `setInitialConditions`

. For
example, to use the solution at time `t0 + 10*tstep`

,
specify `11`

as the third parameter.

setInitialConditions(model,results,11)