%

clear; close all


disp('Solves the parabolic equation u_t = u_xx')
disp('in the interval (0,1) with Dirichlet b.c.')
disp(' ')

disp('[Example 1: u(0,t)=u(1,t)=0, u(x,0) = 1-2*abs(x-0.5)    ]')
disp('[Example 2: u(0,t)=u(1,t)=0, u(x,0) = x*(1-x)           ]')
disp('[Example 3: u(0,t)=u(1,t)=0, u(x,0) = 1                 ]')
disp('[Example 4: u(0,t)=0, u(1,t)=1, u(x,0)= x*(3-2*x)       ]')
disp('[Example 5: u(0,t)=t, u(1,t)=0, u(x,0)= 1-2*abs(x-0.5)  ]')
disp(' ')
example=input('Example (1, 2, 3 4 or 5) = ');
disp(' ')

a=0;
b=1;
m = 10;
Ax=(b-a)/m;
disp(strcat('---> delta_x = ',num2str(Ax)));
disp(' ')
x=[a:Ax:b];

tfin = 0.1;
npast = input('Number of time steps = ');
At=tfin/npast;
t=[0:At:npast*At];
disp(strcat('---> delta_t = ',num2str(At)));
disp(' ')

switch(example)
    case 1
        f=1-2*abs(x-0.5);
        g=0;
        h=0;
    case 2
        f=x.*(1-x);
        g=0;
        h=0;
    case 3
        f=1 ;
        g=0 ;
        h=0 ;
    case 4
        f = x.*(3-2*x); 
        g = 0; 
        h = 1; 
    case 5
        f=1-2*abs(x-0.5);
        g=t;
        h=0;        
    otherwise
        error('Example not available')
end

disp('[ (1) Explicit(Forward Euler)]')
disp('[ (2) Implicit (Backward Euler) ]')
disp('[ (3) Crank-Nicolson                 ]')
disp(' ')
method = input('Resolution method = ');

switch(method)
    case 1
        u=parabolic_ex(a,b,m,At,npast,f,g,h);
        titulo = 'Solution of u_t=u_{xx} with the explicit method';
    case 2
        u=parabolic_im(a,b,m,At,npast,f,g,h);
        titulo = 'Solution of u_t=u_{xx} with the implicit method';
    case 3
        u=parabolic_cn(a,b,m,At,npast,f,g,h);
        titulo = 'Solution of u_t=u_{xx} with Crank-Nicolson method';
    otherwise
        error('Unknown method')
end



% Postprocess
figure(1),clf
subplot(1,2,1)
plot(x,u)
xlabel('x'), ylabel('y')
axis square, axis([0 1 0 1])
if example == 4
    axis([0 1 0 1.2])
end
title(titulo)

subplot(1,2,2)
surf(t,x,u), axis([0 At*npast a b 0 1])
if example == 4
    axis([0 At*npast a b 0 1.2])
end
xlabel('t'), ylabel('x'), zlabel('u')
axis square

