%  This is Problem 1 from P.M. Dew and J.E. Walsh, A set of library 
%  routines for solving parabolic equations in one space variable, ACM
%  Trans. Math. Software, 7(1981) 295-314.  They use it to illustrate
%  the capability of their method of lines code for solving problems
%  of both elliptic and parabolic type.  In this approach to solving
%  PDEs, the parabolic equations give rise to ODEs and the elliptic 
%  equations to algebraic equations.  The two equations are
%
%           0 = D^2(u1)/Dx^2 - exp(-2*u2)
%    D(u2)/Dt = D^2(u2)/Dx^2 + exp(-2*u2) - exp(-u1) 
%
%  The problem is set on 0 <= x <= 1 and t >= 0.  The initial values are  
%  u2(x,0) = log(x+p)  for a parameter p > 1.  Here we take p = 10.
%
%  The boundary conditions are x = 0 are D(u1)/Dx = exp(-u1) and 
%  u2 = log(p+t).  The boundary conditions at x = 1 are u1 = log(1+p+t)
%  and D(u2)/Dx = exp(-u1).
%
%  The analytical solution is u1 = u2 = log(x+p+t).
%  
%  This example is interesting because it is representative of a class
%  of DAEs, it involves a "large" number of algebraic equations, and the
%  obvious initial conditions are inconsistent. The problem is solved
%  quickly, but it is sufficiently large and the Jacobian is sufficiently
%  sparse that it is a good idea to inform the code about the structure
%  of the Jacobian.  This is done to illustrate how to proceed.

% Copyright 1999, The MathWorks, Inc.

clear all
global P N
P = 10;

%  Use a mesh of N equally spaced points in 0 <= x <= 1.
N = 20;
x = linspace(0,1,N)';

%  Initial conditions are taken from the analytical solution.  
%  NOTE that they are not consistent for the DAE resulting from 
%  discretization, except for the two Dirichlet boundary conditions.
u10 = log(x + P);
u20 = u10;
y0 = [u10; u20];

options = odeset('Mass','M','Jpattern','on');

[t,y] = ode15s('ex12ode',[0 0.5 1],y0,options);

u1 = y(:,1:N);
u2 = y(:,1+N:N+N);
true = [log(x'+P+0); log(x'+P+0.5); log(x'+P+1)];
plot(x,u1,'*',x,u2,'o',x,true)
xlabel('u1 = *, u2 = o, solid line is the analytical solution')
title('MOL Solution of Elliptic-Parabolic System of PDEs')
