Plot characteristic and solution without solving in closed-form

Math 613 o Fall 2019 o Victor Matveev o Homework 8 o Problem 1
clear;
M        = 33;                  % Number of characteristics
L        = 2;                   % Boundary of the x-domain: -L < x < L
tShock   = exp(1/2)/6;          % Exact breaking time
x0Array  = linspace(-L, L, M);  % Array of x0 values
xArray   = [];                  % Initialize array of x(x0, tShock) values

subplot(212); hold on;

for x0 = x0Array
    u0       = exp(-x0^2);             % Initial condition
    Velocity = 3*u0.^2;                % Velocity of characteristic
    xAtShock = x0 + tShock * Velocity; % Compute x(x0, tShock)
    xArray   = [xArray, xAtShock];     % Store it recursively in an array

    if abs(x0 - 0.5) < 0.1  % Special shock characteristic is a thick line:
        plot([x0 xAtShock], [0 tShock], 'r-', 'linewidth', 2);
    else                    % All other characteristics are thin red lines:
        plot([x0 xAtShock], [0 tShock], 'r-');
    end
end

axis([-L L 0 tShock]);
xlabel('x'); ylabel('time');
title('Characteristics');

subplot(211); hold on;
plot(xArray, exp(-x0Array.^2), 'b-', 'linewidth', 2);  % Plot solution at tShock
plot(0.5 + tShock * 3*exp(-1/2), exp(-0.5^2), 'ro', 'MarkerFaceColor', 'r');
axis([-L L 0 1]);
xlabel('x'); ylabel('u(x,t_{break})');
title('Solution at breaking time');