Problem Set 7
Problem Set 7
Caitlin McIlwain
June 2016
dt
u(t + dt, x) = −c (u(t, x + dt) − u(t, x − dx)) + u(t − dt, x)
dx
dt
u(t + dt, 0) = −c (u(t, 0 + dt) − u(t, 0 − dx)) + u(t − dt, 0)
dx
where u(t, −dx) = u(t, L), this gives us one boundary condition
equalling:
dt
u(t + dt, 0) = −c (u(t, dt) − u(t, L)) + u(t − dt, 0)
dx
1
Plugging in x=L, we get:
dt
u(t + dt, L) = −c (u(t, L + dt) − u(t, L − dx)) + u(t − dt, L)
dx
where u(t, L + dx) = u(t, 0), this gives us the second boundary con-
dition equalling:
dt
u(t + dt, L) = −c (u(t, 0) − u(t, L − dx)) + u(t − dt, L)
dx
(c) Code for x and t vectors in Matlab:
% set up spatial discritization
L= 5;% size of region
nx = 1000; % number of spatial discritizations
x= [0: L/(nx-1): L]’;% define spatial grid
dx = L/(nx-1); % grid size
% set up time discritization
tfinal = 3; % final time
nt = 2*nx; % number of time steps
time = [0: tfinal/(nt-1) : tfinal]’; % time grid
dt = tfinal/(nt-1); % size of time step
Where dx = 0.0050 and dt = 0.0015.
(d) Code for initial conditions defined:
% set up initial conditions
u= exp((-(x-1).^2)./0.01); %u(t=0,x)
up = u; % u(t= -dt, x)
(e) Defining this equation:
un = up + λAu
dt
u(t + dt, x) = u(t − dt, x) − c (u(t, x + dt) − u(t, x − dx))
dx
0 1 0 ... −1
−1 0 1 ... 0
A= 0
−1 0 ... 0
.. .. .. .. ..
. . . . .
1 0 0 ... 0
" #
u(t + dt, x)
un = ..
.
" #
u(t, x)
u= ..
.
2
" #
u(t − dt, x)
up = ..
.
λ = −0.2998
It is necessary for matrix A to look like an off-diagonal matrix in
order for the coefficients to match up on the ”u” vector. This is
why A is an off diagonal matrix, while the two corners represent the
boundary conditions.
(f) Code for solution to wave-equation:
% set up spatial discritization
L= 5;% size of region
nx = 1000; % number of spatial discritizations x= % define spatial grid
x= [0: L/(nx-1): L]’;% define spatial grid
dx = L/(nx-1); % grid size
% set up time discritization
tfinal = 3; % final time
nt = 2*nx; % number of time steps
time = [0: tfinal/(nt-1) : tfinal]’; % time grid
dt = tfinal/(nt-1); % size of time step
3
for i= 1:length(time) % all your time points
% time stepping equation
un = up+lambda*A*u; % u(t+dt, x)
% update variables
up = u;
u = un;
% plot for a subset of times
if find(any(abs(time(i)-tplot)<dt), 1)
figure(1)
plot(x,u)
xlabel(’space, x’)
ylabel(’traveling wave, u’)
xlim([0,5])
ylim([0,1])
drawnow
end
% save for some subset of times
if find(any(abs(time(i)-tsave)<dt), 1)
usave(:,ii) = u;
ii =ii+1;
end
end
% plot at final time (in case not included in plot vector)
figure(1)
plot(x,u)
xlabel(’space, x’)
ylabel(’traveling wave, u’)
4
2. Error Analysis
(a) Code for defined vector ureal
% calculate actual solution
ureal = exp((-(x-4).^2)./0.01);
(b) Calculated integral of absolute error between the true solution and
the numerical solutions:
error = abs(u-ureal);
totalerror = trapz(x,error)
errors = [errors totalerror]
I plotted my final u(t = 3, x) calculated from 1 below:
5
As you can see, they are very similar.
Here is the code I used to plot ureal:
%plot ureal
figure(3)
plot(x,ureal)
title(’ureal wave figure at t=3’)
xlabel(’space, x’)
ylabel(’traveling wave, u’)
xlim([0,L])
ylim([-0.2,1.2])
(c) I created a for loop to run through each different nx (100, 250, 500,
1000, 2500, 5000, and 10000) value. The code outputted the ’error’
each time, and I then compiled these errors into a vector to use for
part d. Here is the output of errors:
errors =
Columns 1 through 5
Columns 6 through 8
(d) To see how the error grows, I used a log log plot of dx by error. The
plot is included below:
6
As dx increases, so does the error. The plot looks parabolic, which
suggests that n = 2. So, Error ∝ O(h2 ).
3. Unstable Numerical Differentiation Scheme
(a) The central difference formula in space can be written as:
f (x + h) − f (x − h)
2h
∂u(t, x) u(t, x + dx) − u(t, x − dx)
=
∂x 2dx
The one-sided difference formula in general can be written as:
un = u + λAu
7
0 1 0 ... −1
−1 0 1 ... 0
A= 0
−1 0 ... 0
.. .. .. .. ..
. . . . .
1 0 0 ... 0
" #
u(t + dt, x)
un = ..
.
" #
u(t, x)
u= ..
.
Where λ = −dt 2dt . In this case λ = −0.0999
This is different from the central differencing scheme in time because
we only need one initial condition, not two. So in Euler’s time step-
ping, we have the point in time we are starting at, and we are finding
the next point. In Central Differencing, we have the point in time
we start at, the point before that, and we are finding the point after
the one in which we are starting at.
(c) My results: The wave travels fairly smoothly for the first part of its
journey. When the travelling wave reaches x=3, the wave ’explodes’.
There is significant instability. Here is a plot of the saved figures at
time points within dt of t = [0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 1.85
1.9 1.95 2]:
(d) At time t=2, the wave becomes unstable, and the whole graph essen-
tially collapses. This shows us that the central differencing formula
for time and space discretizations is stable, while the Euler method
for discretizing time is unstable.