% Code written by E. Renzi (Loughborough University) for the SIG meeting
% of "Mathematical Challenges of Nonlinear Waves and Interfacial Dynamics",
% on 25th and 26th September 2017.

% MATLAB Tutorial 1

clear
close all

% Part 1
%(a)
%t = 0;

% Input dialogue (Part 2 a)
prompt = {'Instant t','Instant t1','Instant t2'};
name = 'Insert instants';
defaultanswer = {'0','-1','1'};

answer = inputdlg(prompt,name,1,defaultanswer);

t = str2double(answer{1});
t1 = str2double(answer{2});
t2 = str2double(answer{3});

%(b)
X = -20:0.01:20;

u0 = 12.* (3+4.*cosh(2.*X-8*t)+cosh(4.*X-64*t))./...
    (3.*cosh(X-28*t)+cosh(3.*X-36*t)).^2;

%(c)
plot(X,u0)
title('Plot of u(x,0)')
xlabel('x')
ylabel('u0')
grid on
%(d)
%t1 = -1;
%t2 = 1;

u1 = 12.* (3+4.*cosh(2.*X-8*t1)+cosh(4.*X-64*t1))./...
    (3.*cosh(X-28*t1)+cosh(3.*X-36*t1)).^2;
u2 = 12.* (3+4.*cosh(2.*X-8*t2)+cosh(4.*X-64*t2))./...
    (3.*cosh(X-28*t2)+cosh(3.*X-36*t2)).^2;
%(e)
figure
plot(X,u1,X,u0,X,u2)
title('Plot of u(x,t) at several times')
xlabel('x')
ylabel('u(x,t)')
grid on
legend('u(x,-1)','u(x,0)','u(x,1)')
%(f)
figure
subplot(1,3,1)
plot(X,u1)
xlabel('x')
ylabel('u(x,-1)')
grid on
subplot(1,3,2)
plot(X,u0)
xlabel('x')
ylabel('u(x,0)')
grid on
subplot(1,3,3)
plot(X,u2)
xlabel('x')
ylabel('u(x,1)')
grid on

% Part 2

%(b)
prompt2 = {'Instant ti','Increment dt','Instant tf'};
name2 = 'Insert instants';
defaultanswer2 = {'-5','0.1','5'};

answer2 = inputdlg(prompt2,name2,1,defaultanswer2);

ti = str2double(answer2{1});
dt = str2double(answer2{2});
tf = str2double(answer2{3});

T = ti:dt:tf;

%(c)
for i = 1:1:length(X)
    for j = 1:1:length(T)
        u(i,j) = 12* (3+4*cosh(2*X(i)-8*T(j))+cosh(4*X(i)-64*T(j)))/...
            (3*cosh(X(i)-28*T(j))+cosh(3*X(i)-36*T(j)))^2;
    end
end
figure 
contourf(T,X,u,50)
title('Contour plot of soliton collision')
xlabel('t')
ylabel('x')
