# Solve PDE and Compute Partial Derivatives

This example shows how to solve a transistor partial differential equation (PDE) and use the results to obtain partial derivatives that are part of solving a larger problem.

Consider the PDE

`$\frac{\partial \mathit{u}}{\partial \mathit{t}}=\mathit{D}\frac{{\partial }^{2}\mathit{u}}{\partial {\mathit{x}}^{2}}-\frac{\mathit{D}\eta }{\mathit{L}}\frac{\partial \mathit{u}}{\partial \mathit{x}}.$`

This equation arises in transistor theory , and $\mathit{u}\left(\mathit{x},\mathit{t}\right)$ is a function describing the concentration of excess charge carriers (or holes) in the base of a PNP transistor. $\mathit{D}$ and $\eta$ are physical constants. The equation holds on the interval $0\le \mathit{x}\le \mathit{L}$ for times $\mathit{t}\ge 0$.

The initial condition includes a constant $\mathit{K}$ and is given by

`$\mathit{u}\left(\mathit{x},0\right)=\frac{\mathit{K}\text{\hspace{0.17em}}\mathit{L}}{\mathit{D}}\left(\frac{1-{\mathit{e}}^{-\eta \left(1-\mathit{x}/\mathit{L}\right)}}{\eta }\right).$`

The problem has boundary conditions given by

`$\mathit{u}\left(0,\mathit{t}\right)=\mathit{u}\left(\mathit{L},\mathit{t}\right)=0.$`

For fixed $\mathit{x}$, the solution to the equation $\mathit{u}\left(\mathit{x},\mathit{t}\right)$ describes the collapse of excess charge as $\mathit{t}\to \infty$. This collapse produces a current, called the emitter discharge current, which has another constant ${\mathit{I}}_{\mathit{p}}$:

`$\mathit{I}\left(\mathit{t}\right)={\left[\frac{{\mathit{I}}_{\mathit{p}}\mathit{D}}{\mathit{K}}\frac{\partial }{\partial \mathit{x}}\mathit{u}\left(\mathit{x},\mathit{t}\right)\right]}_{\mathit{x}=0}.$`

The equation is valid for $\mathit{t}>0$ due to the inconsistency in the boundary values at $\mathit{x}=0$ for $\mathit{t}=0$ and $\mathit{t}>0$. Since the PDE has a closed-form series solution for $\mathit{u}\left(\mathit{x},\mathit{t}\right)$, you can calculate the emitter discharge current analytically as well as numerically, and compare the results.

To solve this problem in MATLAB, you need to code the PDE equation, initial conditions, and boundary conditions, then select a suitable solution mesh before calling the solver `pdepe`. You either can include the required functions as local functions at the end of a file (as done here), or save them as separate, named files in a directory on the MATLAB path.

### Define Physical Constants

To keep track of the physical constants, create a structure array with fields for each one. When you later define functions for the equations, the initial condition, and the boundary conditions, you can pass in this structure as an extra argument so that the functions have access to the constants.

```C.L = 1; C.D = 0.1; C.eta = 10; C.K = 1; C.Ip = 1;```

### Code Equation

Before you can code the equation, you need to make sure that it is in the form that the `pdepe` solver expects:

`$\mathit{c}\left(\mathit{x},\mathit{t},\mathit{u},\frac{\partial \mathit{u}}{\partial \mathit{x}}\right)\frac{\partial \mathit{u}}{\partial \mathit{t}}={\mathit{x}}^{-\mathit{m}}\frac{\partial }{\partial \mathit{x}}\left({\mathit{x}}^{\mathit{m}}\mathit{f}\left(\mathit{x},\mathit{t},\mathit{u},\frac{\partial \mathit{u}}{\partial \mathit{x}}\right)\right)+\mathit{s}\left(\mathit{x},\mathit{t},\mathit{u},\frac{\partial \mathit{u}}{\partial \mathit{x}}\right).$`

In this form, the PDE is

`$\frac{\partial \mathit{u}}{\partial \mathit{t}}={\mathit{x}}^{0}\text{\hspace{0.17em}}\frac{\partial }{\partial \mathit{x}}\left({\mathit{x}}^{0}\text{\hspace{0.17em}}\mathit{D}\frac{\partial \mathit{u}}{\partial \mathit{x}}\right)-\frac{\mathit{D}\eta }{\mathit{L}}\frac{\partial \mathit{u}}{\partial \mathit{x}}.$`

So the values of the coefficients in the equation are

• $\mathit{m}=0$ (Cartesian coordinates with no angular symmetry)

• $\mathit{c}\left(\mathit{x},\mathit{t},\mathit{u},\frac{\partial \mathit{u}}{\partial \mathit{x}}\right)=1$

• $\mathit{f}\left(\mathit{x},\mathit{t},\mathit{u},\frac{\partial \mathit{u}}{\partial \mathit{x}}\right)=\mathit{D}\frac{\partial \mathit{u}}{\partial \mathit{x}}$

• $\mathit{s}\left(\mathit{x},\mathit{t},\mathit{u},\frac{\partial \mathit{u}}{\partial \mathit{x}}\right)=-\frac{\mathit{D}\eta }{\mathit{L}}\frac{\partial \mathit{u}}{\partial \mathit{x}}$

Now you can create a function to code the equation. The function should have the signature `[c,f,s] = transistorPDE(x,t,u,dudx,C)`:

• `x` is the independent spatial variable.

• `t` is the independent time variable.

• `u` is the dependent variable being differentiated with respect to `x` and `t`.

• `dudx` is the partial spatial derivative $\partial \mathit{u}/\partial \mathit{x}$.

• `C` is an extra input containing the physical constants.

• The outputs `c`, `f`, and `s` correspond to coefficients in the standard PDE equation form expected by `pdepe`.

As a result, the equation in this example can be represented by the function:

```function [c,f,s] = transistorPDE(x,t,u,dudx,C) D = C.D; eta = C.eta; L = C.L; c = 1; f = D*dudx; s = -(D*eta/L)*dudx; end ```

(Note: All functions are included as local functions at the end of the example.)

### Code Initial Condition

Next, write a function that returns the initial condition. The initial condition is applied at the first time value, and provides the value of $\mathit{u}\left(\mathit{x},{\mathit{t}}_{0}\right)$ for any value of $\mathit{x}$. Use the function signature `u0 = transistorIC(x,C)` to write the function.

The initial condition is

`$\mathit{u}\left(\mathit{x},0\right)=\frac{\mathit{K}\text{\hspace{0.17em}}\mathit{L}}{\mathit{D}}\left(\frac{1-{\mathit{e}}^{-\eta \left(1-\mathit{x}/\mathit{L}\right)}}{\eta }\right).$`

The corresponding function is

```function u0 = transistorIC(x,C) K = C.K; L = C.L; D = C.D; eta = C.eta; u0 = (K*L/D)*(1 - exp(-eta*(1 - x/L)))/eta; end ```

### Code Boundary Conditions

Now, write a function that evaluates the boundary conditions $\mathit{u}\left(0,\mathit{t}\right)=\mathit{u}\left(1,\mathit{t}\right)=0$. For problems posed on the interval $\mathit{a}\le \mathit{x}\le \mathit{b}$, the boundary conditions apply for all $\mathit{t}$ and either $\mathit{x}=\mathit{a}$ or $\mathit{x}=\mathit{b}$. The standard form for the boundary conditions expected by the solver is

`$\mathit{p}\left(\mathit{x},\mathit{t},\mathit{u}\right)+\mathit{q}\left(\mathit{x},\mathit{t}\right)\mathit{f}\left(\mathit{x},\mathit{t},\mathit{u},\frac{\partial \mathit{u}}{\partial \mathit{x}}\right)=0.$`

Written in this form, the boundary conditions for this problem are

- For $\mathit{x}=0$, the equation is $\mathit{u}+0\cdot \mathit{d}\frac{\partial \mathit{u}}{\partial \mathit{x}}=0.$ The coefficients are:

• ${\mathit{p}}_{\mathit{L}}\left(\mathit{x},\mathit{t},\mathit{u}\right)=\mathit{u},$

• ${\mathit{q}}_{\mathit{L}}\left(\mathit{x},\mathit{t}\right)=0.$

- Likewise for $\mathit{x}=1$, the equation is $\mathit{u}+0\cdot \mathit{d}\frac{\partial \mathit{u}}{\partial \mathit{x}}=0.$ The coefficients are:

• ${\mathit{p}}_{\mathit{R}}\left(\mathit{x},\mathit{t},\mathit{u}\right)=\mathit{u},$

• ${\mathit{q}}_{\mathit{R}}\left(\mathit{x},\mathit{t}\right)=0.$

The boundary function should use the function signature `[pl,ql,pr,qr] = transistorBC(xl,ul,xr,ur,t)`:

• The inputs x`l` and u`l` correspond to $\mathit{x}$ and $\mathit{u}$ for the left boundary.

• The inputs `xr` and `ur` correspond to $\mathit{x}$ and $\mathit{u}$ for the right boundary.

• `t` is the independent time variable.

• The outputs `pl` and `ql` correspond to ${\mathit{p}}_{\mathit{L}}\left(\mathit{x},\mathit{t},\mathit{u}\right)$ and ${\mathit{q}}_{\mathit{L}}\left(\mathit{x},\mathit{t}\right)$ for the left boundary ($\mathit{x}=0$ for this problem).

• The outputs `pr` and `qr` correspond to ${\mathit{p}}_{\mathit{R}}\left(\mathit{x},\mathit{t},\mathit{u}\right)$ and ${\mathit{q}}_{\mathit{R}}\left(\mathit{x},\mathit{t}\right)$ for the right boundary ($\mathit{x}=1$ for this problem).

The boundary conditions in this example are represented by the function:

```function [pl,ql,pr,qr] = transistorBC(xl,ul,xr,ur,t) pl = ul; ql = 0; pr = ur; qr = 0; end ```

### Select Solution Mesh

The solution mesh defines the values of $\mathit{x}$ and $\mathit{t}$ where the solution is evaluated by the solver. Since the solution to this problem changes rapidly, use a relatively fine mesh of 50 spatial points in the interval $0\le \mathit{x}\le \mathit{L}$ and 50 time points in the interval $0\le \mathit{t}\le 1$.

```x = linspace(0,C.L,50); t = linspace(0,1,50);```

### Solve Equation

Finally, solve the equation using the symmetry $\mathit{m}$, the PDE equation, the initial condition, the boundary conditions, and the meshes for $\mathit{x}$ and $\mathit{t}$. Since `pdepe` expects the PDE function to use four inputs and the initial condition function to use one input, create function handles that pass in the structure of physical constants as an extra input.

```m = 0; eqn = @(x,t,u,dudx) transistorPDE(x,t,u,dudx,C); ic = @(x) transistorIC(x,C); sol = pdepe(m,eqn,ic,@transistorBC,x,t);```

`pdepe` returns the solution in a 3-D array `sol`, where `sol(i,j,k)` approximates the `k`th component of the solution ${\mathit{u}}_{\mathit{k}}$evaluated at `t(i)` and `x(j)`. For this problem `u` has only one component, but in general you can extract the `k`th solution component with the command `u = sol(:,:,k)`.

`u = sol(:,:,1);`

### Plot Solution

Create a surface plot of the solution $\mathit{u}$ plotted at the selected mesh points for $\mathit{x}$ and $\mathit{t}$.

```surf(x,t,u) title('Numerical Solution (50 mesh points)') xlabel('Distance x') ylabel('Time t') zlabel('Solution u(x,t)')``` Now, plot just $\mathit{x}$ and $\mathit{u}$ to get a side view of the contours in the surface plot.

```plot(x,u) xlabel('Distance x') ylabel('Solution u(x,t)') title('Solution profiles at several times')``` ### Compute Emitter Discharge Current

Using a series solution for $\mathit{u}\left(\mathit{x},\mathit{t}\right)$, the emitter discharge current can be expressed as the infinite series :

`$\mathit{I}\left(\mathit{t}\right)=2{\pi }^{2}{\mathit{I}}_{\mathit{p}}\left(\frac{1-{\mathit{e}}^{-\eta }}{\eta }\right)\sum _{\mathit{n}=1}^{\infty }\frac{{\mathit{n}}^{2}}{{\mathit{n}}^{2}{\pi }^{2}+{\eta }^{2}/4}{\mathit{e}}^{-\frac{\mathit{dt}}{{\mathit{L}}^{2}}\left({\mathit{n}}^{2}{\pi }^{2}+{\eta }^{2}/4\right)}.$`

Write a function to calculate the analytic solution for $\mathit{I}\left(\mathit{t}\right)$ using 40 terms in the series. The only variable is time, but specify a second input to the function for the structure of constants.

```function It = serex3(t,C) % Approximate I(t) by series expansion. Ip = C.Ip; eta = C.eta; D = C.D; L = C.L; It = 0; for n = 1:40 % Use 40 terms m = (n*pi)^2 + 0.25*eta^2; It = It + ((n*pi)^2 / m)* exp(-(D/L^2)*m*t); end It = 2*Ip*((1 - exp(-eta))/eta)*It; end ```

Using the numeric solution for $\mathit{u}\left(\mathit{x},\mathit{t}\right)$ as computed by `pdepe`, you can also calculate the numeric approximation for $\mathit{I}\left(\mathit{t}\right)$ at $\mathit{x}=0$ with

`$\mathit{I}\left(\mathit{t}\right)={\left[\frac{{\mathit{I}}_{\mathit{p}}\mathit{D}}{\mathit{K}}\frac{\partial }{\partial \mathit{x}}\mathit{u}\left(\mathit{x},\mathit{t}\right)\right]}_{\mathit{x}=0}.$`

Calculate the analytic and numeric solutions for $\mathit{I}\left(\mathit{t}\right)$ and plot the results. Use `pdeval` to compute the value of $\partial \mathit{u}/\partial \mathit{x}$ at $\mathit{x}=0$.

```nt = length(t); I = zeros(1,nt); seriesI = zeros(1,nt); iok = 2:nt; for j = iok % At time t(j), compute du/dx at x = 0. [~,I(j)] = pdeval(m,x,u(j,:),0); seriesI(j) = serex3(t(j),C); end % Numeric solution has form I(t) = (I_p*D/K)*du(0,t)/dx I = (C.Ip*C.D/C.K)*I; plot(t(iok),I(iok),'o',t(iok),seriesI(iok)) legend('From PDEPE + PDEVAL','From series') title('Emitter discharge current I(t)') xlabel('Time t')``` The results match reasonably well. You can further improve the numeric result from `pdepe` by using a finer solution mesh.

### Local Functions

Listed here are the local helper functions that the PDE solver `pdepe` calls to calculate the solution. Alternatively, you can save these functions as their own files in a directory on the MATLAB path.

```function [c,f,s] = transistorPDE(x,t,u,dudx,C) % Equation to solve D = C.D; eta = C.eta; L = C.L; c = 1; f = D*dudx; s = -(D*eta/L)*dudx; end % ---------------------------------------------------- function u0 = transistorIC(x,C) % Initial condition K = C.K; L = C.L; D = C.D; eta = C.eta; u0 = (K*L/D)*(1 - exp(-eta*(1 - x/L)))/eta; end % ---------------------------------------------------- function [pl,ql,pr,qr] = transistorBC(xl,ul,xr,ur,t) % Boundary conditions pl = ul; ql = 0; pr = ur; qr = 0; end % ---------------------------------------------------- function It = serex3(t,C) % Approximate I(t) by series expansion. Ip = C.Ip; eta = C.eta; D = C.D; L = C.L; It = 0; for n = 1:40 % Use 40 terms m = (n*pi)^2 + 0.25*eta^2; It = It + ((n*pi)^2 / m)* exp(-(D/L^2)*m*t); end It = 2*Ip*((1 - exp(-eta))/eta)*It; end % ----------------------------------------------------```

### References

 Zachmanoglou, E.C. and D.L. Thoe. Introduction to Partial Differential Equations with Applications. Dover, New York, 1986.