clc
close all

for k = [1 2 3 4 5]
    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= k; disp(['Example (1, 2, 3 4 or 5) = ' num2str(k)]);
    disp(' ')
    
    n = [24 20 18 5];
    for i = [1 2 3 4]
        a=0;
        b=1;
        m = 10; disp('Number of intervals M = 10');
        Ax=(b-a)/m;
        disp(strcat('---> delta_x = ',num2str(Ax)));
        disp(' ')
        x=a:Ax:b;
        
        tfin = 0.1; disp('Final time = 0.1');
        npast = n(i); disp(['Number of time steps = ' num2str(npast)]);
        At=tfin/npast;
        t=0:At:npast*At;
        r = At/Ax^2;
        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
        
        for j = [1 2 3]
            disp('[ (1) Explicit(Forward Euler)]')
            disp('[ (2) Implicit (Backward Euler) ]')
            disp('[ (3) Crank-Nicolson                 ]')
            disp(' ')
            method = j; disp(['Resolution method = ' num2str(j)]);
            
            switch(method)
                case 1
                    u=parabolic_ex(a,b,m,At,npast,f,g,h);
                    titulo = {['Example No.' num2str(k)], ['Solution of u_t=u_{xx} with the explicit method, ' num2str(n(i)) ' time steps'],['r = ' num2str(r)]};
                case 2
                    u=parabolic_im(a,b,m,At,npast,f,g,h);
                    titulo = {['Example No.' num2str(k)], ['Solution of u_t=u_{xx} with the implicit method, ' num2str(n(i)) ' time steps'],['r = ' num2str(r)]};
                case 3
                    u=parabolic_cn(a,b,m,At,npast,f,g,h);
                    titulo = {['Example No.' num2str(k)], ['Solution of u_t=u_{xx} with Crank-Nicolson method, ' num2str(n(i)) ' time steps'],['r = ' num2str(r)]};
                otherwise
                    error('Unknown method')
            end
            
            
            % Postprocess
            figure,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
            
            
            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
            
            set(gcf,'NextPlot','add');
            axes;
            tit = title(titulo);
            set(gca,'Visible','off');
            set(tit,'Visible','on');
            
        end
    end
end
