Personal Website:
Scott Congreve

Teaching

Download<< Back

function run_fdode

% Space discretisation (ignoring boundary)
N=10;
x = pi*(1:N)/(N+1);

% Initial conditions:
u0 = sin(x);

% Time space:
t0 = 0;
T = 4;

% Compute solution:
[t,u] = ode15s(@(t,x) fdode(t,x,N), [t0,T], u0);

% Add boundary
bc = zeros(size(t));
u = [bc u bc];
x = [0, x, pi];

% Plot 1D slices in time
figure;
[~,mid] = min(abs(t-(t(1)+t(end))/2));
plot(x, u(1,:), 'k', x, u(mid,:), 'b', x, u(end,:), 'r');
legend(['t = ' num2str(t(1))], ['t = ' num2str(t(mid))], ['t = ' num2str(t(end))]);
xlabel('x');
ylabel('u');

% Plot 1D slices in space
figure;
mid = round((N+2)/2);
plot(t, u(:,2), 'k', t, u(:,mid), 'b', t, u(:,N+1), 'r');
legend(['x = ' num2str(x(1))], ['x = ' num2str(x(mid))], ['x = ' num2str(x(N+1))]);
xlabel('t');
ylabel('u');

% Plot surface
figure;
surf(x, t, u);
xlabel('x');
ylabel('t');
zlabel('u');
end