%% Solve System of PDEs
% This example shows how to formulate, compute, and plot the solution to a system of two partial differential equations.
% Consider the system of PDEs
% \frac{\partial u_1 }{\partial t}=0\ldotp 024\frac{\partial^2 u_1 }{\partial x^2 }-F\left(u_1 -u_2 \right),
% \frac{\partial u_2 }{\partial t}=0\ldotp 170\frac{\partial^2 u_2 }{\partial x^2 }+F\left(u_1 -u_2 \right)\ldotp
% (The function F\left(y\right)=e^{5\ldotp 73y} -e^{-11\ldotp 46y} is used as a shorthand.)
% The equation holds on the interval 0\le x\le 1 for times t\ge 0. The initial conditions are
% u_1 \left(x,0\right)=1,
% u_2 \left(x,0\right)=0\ldotp
% The boundary conditions are
% \begin{array}{l}
% \frac{\partial }{\partial x}u_1 \left(0,t\right)=0,\\
% {\;\;\;\;\;\;u}_2 \left(0,t\right)=0,\\
% \frac{\partial }{\partial x}u_2 \left(1,t\right)=0,\\
% {\;\;\;\;\;\;u}_1 \left(1,t\right)=1\ldotp
% \end{array}
% To solve this equation in MATLAB, you need to code the equation, the initial conditions, and the 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.
%
%% Code Equation
% Before you can code the equation, you need to make sure that it is in the form that the pdepe solver expects:
% c\left(x,t,u,\frac{\partial u}{\partial x}\right)\frac{\partial u}{\partial t}=x^{-m} \frac{\partial }{\partial x}\left(x^m f\left(x,t,u,\frac{\partial u}{\partial x}\right)\right)+s\left(x,t,u,\frac{\partial u}{\partial x}\right)\ldotp
% In this form, the PDE coefficients are matrix-valued and the equation becomes
% \left\lbrack \begin{array}{cc}
% 1 & 0\\
% 0 & 1
% \end{array}\right\rbrack \frac{\partial }{\partial t}\left\lbrack \begin{array}{c}
% u_1 \\
% u_2
% \end{array}\right\rbrack =\frac{\partial }{\partial x}\left\lbrack \begin{array}{c}
% 0\ldotp 024\frac{\partial u_1 }{\partial x}\\
% 0\ldotp 170\frac{\partial u_2 }{\partial x}
% \end{array}\right\rbrack +\left\lbrack \begin{array}{c}
% -F\left(u_1 -u_2 \right)\\
% F\left(u_1 -u_2 \right)
% \end{array}\right\rbrack \ldotp
% So the values of the coefficients in the equation are
% m=0
% c\left(x,t,u,\frac{\partial u}{\partial x}\right)=\left\lbrack \begin{array}{c}
% 1\\
% 1
% \end{array}\right\rbrack (diagonal values only)
% f\left(x,t,u,\frac{\partial u}{\partial x}\right)=\left\lbrack \begin{array}{c}
% 0\ldotp 024\frac{\partial u_1 }{\partial x}\\
% 0\ldotp 170\frac{\partial u_2 }{\partial x}
% \end{array}\right\rbrack
% s\left(x,t,u,\frac{\partial u}{\partial x}\right)=\left\lbrack \begin{array}{c}
% -F\left(u_1 -u_2 \right)\\
% F\left(u_1 -u_2 \right)
% \end{array}\right\rbrack
% Now you can create a function to code the equation. The function should have the signature [c,f,s] = pdefun(x,t,u,dudx):
% 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. It is a two-element vector where u(1) is u_1 \left(x,t\right) and u(2) is u_2 \left(x,t\right).
% dudx is the partial spatial derivative \partial u/\partial x. It is a two-element vector where dudx(1) is \partial u_1 /\partial x and dudx(2) is \partial u_2 /\partial x.
% The outputs c, f, and s correspond to coefficients in the standard PDE equation form expected by pdepe.
% As a result, the equations in this example can be represented by the function:
% function [c,f,s] = pdefun(x,t,u,dudx)
% c = [1; 1];
% f = [0.024; 0.17] .* dudx;
% y = u(1) - u(2);
% F = exp(5.73*y)-exp(-11.47*y);
% s = [-F; F];
% end
% (Note: All functions are included as local functions at the end of the example.)
%
%% Code Initial Conditions
% Next, write a function that returns the initial condition. The initial condition is applied at the first time value and provides the value of u\left(x,t_0 \right) for any value of x. The number of initial conditions must equal the number of equations, so for this problem there are two initial conditions. Use the function signature u0 = pdeic(x) to write the function.
% The initial conditions are
% u_1 \left(x,0\right)=1,
% u_2 \left(x,0\right)=0\ldotp
% The corresponding function is
% function u0 = pdeic(x)
% u0 = [1; 0];
% end
%
%% Code Boundary Conditions
% Now, write a function that evaluates the boundary conditions
% \begin{array}{l}
% \frac{\partial }{\partial x}u_1 \left(0,t\right)=0,\\
% {\;\;\;\;\;\;u}_2 \left(0,t\right)=0,\\
% \frac{\partial }{\partial x}u_2 \left(1,t\right)=0,\\
% {\;\;\;\;\;\;u}_1 \left(1,t\right)=1\ldotp
% \end{array}
% For problems posed on the interval a\le x\le b, the boundary conditions apply for all t and either x=a or x=b. The standard form for the boundary conditions expected by the solver is
% p\left(x,t,u\right)+q\left(x,t\right)f\left(x,t,u,\frac{\partial u}{\partial x}\right)=0\ldotp
% Written in this form, the boundary conditions for the partial derivatives of u need to be expressed in terms of the flux f\left(x,t,u,\frac{\partial u}{\partial x}\right). So the boundary conditions for this problem are
% For x=0, the equation is
% \left\lbrack \begin{array}{c}
% 0\\
% u_2
% \end{array}\right\rbrack +\left\lbrack \begin{array}{c}
% 1\\
% 0
% \end{array}\right\rbrack \cdot \left\lbrack \begin{array}{c}
% 0\ldotp 024\frac{\partial u_1 }{\partial x}\\
% 0\ldotp 170\frac{\partial u_2 }{\partial x}
% \end{array}\right\rbrack =0\ldotp
% The coefficients are:
% p_L \left(x,t,u\right)=\left\lbrack \begin{array}{c}
% 0\\
% u_2
% \end{array}\right\rbrack ,    q_L \left(x,t\right)=\left\lbrack \begin{array}{c}
% 1\\
% 0
% \end{array}\right\rbrack \ldotp
% Likewise, for x=1 the equation is
% \left\lbrack \begin{array}{c}
% u_1 -1\\
% 0
% \end{array}\right\rbrack +\left\lbrack \begin{array}{c}
% 0\\
% 1
% \end{array}\right\rbrack \cdot \left\lbrack \begin{array}{c}
% 0\ldotp 024\frac{\partial u_1 }{\partial x}\\
% 0\ldotp 170\frac{\partial u_2 }{\partial x}
% \end{array}\right\rbrack =0\ldotp
% The coefficients are:
% p_R \left(x,t,u\right)=\left\lbrack \begin{array}{c}
% u_1 -1\\
% 0
% \end{array}\right\rbrack ,    q_R \left(x,t\right)=\left\lbrack \begin{array}{c}
% 0\\
% 1
% \end{array}\right\rbrack \ldotp
% The boundary function should use the function signature [pl,ql,pr,qr] = pdebc(xl,ul,xr,ur,t):
% The inputs xl and ul correspond to u and x for the left boundary.
% The inputs xr and ur correspond to u and x for the right boundary.
% t is the independent time variable.
% The outputs pl and ql correspond to p_L \left(x,t,u\right) and q_L \left(x,t\right) for the left boundary (x=0 for this problem).
% The outputs pr and qr correspond to p_R \left(x,t,u\right) and q_R \left(x,t\right) for the right boundary (x=1 for this problem).
% The boundary conditions in this example are represented by the function:
% function [pl,ql,pr,qr] = pdebc(xl,ul,xr,ur,t)
% pl = [0; ul(2)];
% ql = [1; 0];
% pr = [ur(1)-1; 0];
% qr = [0; 1];
% end
%
%% Select Solution Mesh
% The solution to this problem changes rapidly when t is small. Although pdepe selects a time step that is appropriate to resolve the sharp changes, to see the behavior in the output plots you need to select appropriate output times. For the spatial mesh, there are boundary layers in the solution at both ends of 0\le x\le 1, so you need to specify mesh points there to resolve the sharp changes.

x = [0 0.005 0.01 0.05 0.1 0.2 0.5 0.7 0.9 0.95 0.99 0.995 1];
t = [0 0.005 0.01 0.05 0.1 0.5 1 1.5 2];

%
%% Solve Equation
% Finally, solve the equation using the symmetry m, the PDE equation, the initial conditions, the boundary conditions, and the meshes for x and t.

m = 0;
sol = pdepe(m,@pdefun,@pdeic,@pdebc,x,t);

% pdepe returns the solution in a 3-D array sol, where sol(i,j,k) approximates the kth component of the solution u_k evaluated at t(i) and x(j). Extract each solution component into a separate variable.

u1 = sol(:,:,1);
u2 = sol(:,:,2);

%
%% Plot Solution
% Create surface plots of the solutions for u_1 and u_2 plotted at the selected mesh points for x and t.

surf(x,t,u1)
title('u_1(x,t)')
xlabel('Distance x')
ylabel('Time t')
surf(x,t,u2)
title('u_2(x,t)')
xlabel('Distance x')
ylabel('Time t')

%
%% 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] = pdefun(x,t,u,dudx) % Equation to solve
c = [1; 1];
f = [0.024; 0.17] .* dudx;
y = u(1) - u(2);
F = exp(5.73*y)-exp(-11.47*y);
s = [-F; F];
end
% ---------------------------------------------
function u0 = pdeic(x) % Initial Conditions
u0 = [1; 0];
end
% ---------------------------------------------
function [pl,ql,pr,qr] = pdebc(xl,ul,xr,ur,t) % Boundary Conditions
pl = [0; ul(2)];
ql = [1; 0];
pr = [ur(1)-1; 0];
qr = [0; 1];
end
% ---------------------------------------------

% Copyright 2019 The MathWorks, Inc.
%
%