function bistable
%PDEX1  Example 1 for PDEPE
%   This is a simple example with an analytical solution that illustrates 
%   the straightforward formulation, computation, and plotting of the solution
%   of a single PDE.
%
%   In the form expected by PDEPE, the single PDE is
%
%   [pi^2] .*  D_ [u] =  D_ [   Du/Dx   ] +      [0]
%              Dt        Dx
%    ----         ---       -------------    -------------
%     c            u        f(x,t,u,Du/Dx)   s(x,t,u,Du/Dx)
%
%   The equation is to hold on an interval 0 <= x <= 1 for times t >= 0.
%   There is an analytical solution u(x,t) = exp(-t)*sin(pi*x). Boundary
%   conditions are specified so that it will be the solution of the initial-
%   boundary value problem.  Obviously the initial values are sin(pi*x). 
%   Two kinds of boundary conditions are chosen so as to show how they
%   appear in the form expected by the solver.  The left bc is u(0,t) = 0:
%
%     [u]    +     [0] .* [       Du/Dx          ] = [0]
% 
%     ---          ---    ------------------------   ---
%   p(0,t,u)      q(0,t)        f(0,t,u,Du/Dx)        0
%
%   The right bc is taken to be pi*exp(-t) + Du/Dx(1,t) = 0:
%
%   [ pi*exp(-t) ] + [1] .* [   Du/Dx    ] = [0]
%
%   --------------   ---    --------------   ---
%      p(1,t,u)     q(1,t)   f(1,t,u,Du/Dx)   0
%
%   The problem is coded in subfunctions PDEX1PDE, PDEX1IC, and PDEX1BC.
%
%   See also PDEPE, FUNCTION_HANDLE.

%   Lawrence F. Shampine and Jacek Kierzenka
%   Copyright 1984-2004 The MathWorks, Inc.
%   $Revision: 1.6.4.2 $  $Date: 2005/06/21 19:25:04 $

global r;
r=input('r=');
m = 0;
x = linspace(-20,20,401);
t = linspace(0,50,51);
t1=0:1:50;
x1=-20:0.1:20;

sol = pdepe(m,@pdex1pde,@pdex1ic,@pdex1bc,x,t);
% Extract the first solution component as u.  This is not necessary
% for a single equation, but makes a point about the form of the output.

u = sol(:,:,1);

% A solution profile can also be illuminating.

plot(x,u(1,:),'-',x,u(10,:),'-',x,u(20,:),'-',x,u(30,:),'-',x,u(40,:),'Linewidth',2);
axis([-20 20 -0.1 1.1]);
xlabel('x');
ylabel('u(x,t)');
pause;
close;


% --------------------------------------------------------------------------

function [c,f,s] = pdex1pde(x,t,u,DuDx)
global r
c=1;
f = DuDx;
s = -u*(u-r)*(u-1);

% --------------------------------------------------------------------------

function u0 = pdex1ic(x)
u0=0.5*(1-tanh(x/0.5));

% --------------------------------------------------------------------------

function [pl,ql,pr,qr] = pdex1bc(xl,ul,xr,ur,t)
pl = ul-1;
ql = 0;
pr = ur;
qr = 0;
  

