%% Solve PDE with Discontinuity
% This example shows how to solve a PDE that interfaces with a material. The material interface creates a discontinuity in the problem at x=0\ldotp 5, and the initial condition has a discontinuity at the right boundary x=1.
% Consider the piecewise PDE
% \;\left\lbrace \begin{array}{cc}
% \frac{\partial u}{\partial t}=x^{-2} \frac{\partial }{\partial x}\left(x^2 \;5\frac{\partial u}{\partial x}\right)-1000e^u  & \left(0\le x\le 0\ldotp 5\right)\\
% \frac{\partial u}{\partial t}=x^{-2} \frac{\partial }{\partial x}\left(x^2 \;\frac{\partial u}{\partial x}\right)-e^u  & \left(0\ldotp 5\le x\le 1\right)
% \end{array}\right.
% The initial conditions are
% \begin{array}{l}
% u\left(x,0\right)=0\;\;\left(0\le x<1\right),\\
% u\left(1,0\right)=1\;\;\left(x=1\right)\ldotp
% \end{array}
% The boundary conditions are
% \begin{array}{l}
% \;\;\;\;\;\;\;\frac{\partial u}{\partial x}=0\;\;\left(x=0\right),\\
% u\left(1,t\right)=1\;\;\left(x=1\right)\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 a form that the pdepe solver expects. The standard form that pdepe expects is
% 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).
% In this case, the PDE is in the proper form, so you can read off the values of the coefficients.
% \;\left\lbrace \begin{array}{cc}
% \frac{\partial u}{\partial t}=x^{-2} \frac{\partial }{\partial x}\left(x^2 \;5\frac{\partial u}{\partial x}\right)-1000e^u  & \left(0\le x\le 0\ldotp 5\right)\\
% \frac{\partial u}{\partial t}=x^{-2} \frac{\partial }{\partial x}\left(x^2 \;\frac{\partial u}{\partial x}\right)-e^u  & \left(0\ldotp 5\le x\le 1\right)
% \end{array}\right.
% The values for the flux term f\left(x,t,u,\frac{\partial u}{\partial x}\right) and source term s\left(x\ldotp t,u,\frac{\partial u}{\partial x}\right) change depending on the value of x. The coefficients are:
% m=2
% c\left(x,t,u,\frac{\partial u}{\partial x}\right)=1
% \left\lbrace \begin{array}{cc}
% f\left(x,t,u,\frac{\partial u}{\partial x}\right)=5\frac{\partial u}{\partial x} & \left(0\le x\le 0\ldotp 5\right)\\
% f\left(x,t,u,\frac{\partial u}{\partial x}\right)=\frac{\partial u}{\partial x} & \left(0\ldotp 5\le x\le 1\right)
% \end{array}\right.
% \left\lbrace \begin{array}{cc}
% s\left(x,t,u,\frac{\partial u}{\partial x}\right)=-1000e^u  & \left(0\le x\le 0\ldotp 5\right)\\
% s\left(x,t,u,\frac{\partial u}{\partial x}\right)=-e^u  & \left(0\ldotp 5\le x\le 1\right)
% \end{array}\right.
% Now you can create a function to code the equation. The function should have the signature [c,f,s] = pdex2pde(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.
% dudx is the partial spatial derivative \partial u/\partial x.
% The outputs c, f, and s correspond to coefficients in the standard PDE equation form expected by pdepe. These coefficients are coded in terms of the input variables x, t, u, and dudx.
% As a result, the equation in this example can be represented by the function:
% function [c,f,s] = pdex2pde(x,t,u,dudx)
% c = 1;
% if x <= 0.5
%     f = 5*dudx;
%     s = -1000*exp(u);
% else
%     f = dudx;
%     s = -exp(u);
% end
% 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 conditions. 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. Use the function signature u0 = pdex2ic(x) to write the function.
% The initial conditions are
% \begin{array}{l}
% u\left(x,0\right)=0\;\;\;\;\;\;\;\;\left(0\le x<1\right),\\
% u\left(1,0\right)=1\;\;\;\;\;\;\;\;\left(x=1\right)\ldotp
% \end{array}
% The corresponding function is
% function u0 = pdex2ic(x)
% if x < 1
%     u0 = 0;
% else
%     u0 = 1;
% end
% end
%
%% Code Boundary Conditions
% Now, write a function that evaluates the boundary conditions. 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
% Since this example has spherical symmetry (m=2), the pdepe solver automatically enforces the left boundary condition to bound the solution at the origin, and ignores any conditions specified for the left boundary in the boundary function. So for the left boundary condition, you can specify p_L =q_L =0. For the right boundary condition, you can rewrite the boundary condition in the standard form and read off the coefficient values for p_R and q_R.
% For x=1, the equation is  u\left(1,t\right)=1\to \left(u-1\right)+0\cdot \frac{\partial u}{\partial x}=0. The coefficients are:
% p_R \left(1,t,u\right)=u-1
% q_R \left(1,t\right)=0
% The boundary function should use the function signature [pl,ql,pr,qr] = pdex2bc(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] = pdex2bc(xl,ul,xr,ur,t)
% pl = 0;
% ql = 0;
% pr = ur - 1;
% qr = 0;
% end
%
%% Select Solution Mesh
% The spatial mesh should include several values near x=0\ldotp 5 to account for the discontinuous interface, as well as points near x=1 because of the inconsistent initial value (u\left(1,0\right)=1) and boundary value (u\left(1,t\right)=0) at that point. The solution changes rapidly for small t, so use a time step that can resolve this sharp change.

x = [0 0.1 0.2 0.3 0.4 0.45 0.475 0.5 0.525 0.55 0.6 0.7 0.8 0.9 0.95 0.975 0.99 1];
t = [0 0.001 0.005 0.01 0.05 0.1 0.5 1];

%
%% 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 = 2;
sol = pdepe(m,@pdex2pde,@pdex2ic,@pdex2bc,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). The size of sol is length(t)-by-length(x)-by-length(u0), since u0 specifies an initial condition for each solution component. For this problem, u has only one component, so sol is a 8-by-18 matrix, but in general you can extract the kth solution component with the command u = sol(:,:,k).
% Extract the first solution component from sol.

u = sol(:,:,1);

%
%% Plot Solution
% Create a surface plot of the solution u plotted at the selected mesh points for x and t. Since m=2 the problem is posed in a spherical geometry with spherical symmetry, so the solution only changes in the radial x direction.

surf(x,t,u)
title('Numerical solution with nonuniform mesh')
xlabel('Distance x')
ylabel('Time t')
zlabel('Solution u')

% Now, plot just x and u to get a side view of the contours in the surface plot. Add a line at x=0\ldotp 5 to highlight the effect of the material interface.

plot(x,u,x,u,'*')
line([0.5 0.5], [-3 1], 'Color', 'k')
xlabel('Distance x')
ylabel('Solution u')
title('Solution profiles at several times')

%
%% 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] = pdex2pde(x,t,u,dudx) % Equation to solve
c = 1;
if x <= 0.5
    f = 5*dudx;
    s = -1000*exp(u);
else
    f = dudx;
    s = -exp(u);
end
end
%----------------------------------------------
function u0 = pdex2ic(x) %Initial conditions
if x < 1
    u0 = 0;
else
    u0 = 1;
end
end
%----------------------------------------------
function [pl,ql,pr,qr] = pdex2bc(xl,ul,xr,ur,t) % Boundary conditions
pl = 0;
ql = 0;
pr = ur - 1;
qr = 0;
end
%----------------------------------------------

% Copyright 2019 The MathWorks, Inc.