0% found this document useful (0 votes)
53 views

Problem Set 7

This document discusses numerically solving the 1D wave equation using finite differences. It presents: 1) The central difference formulas to discretize derivatives in space and time. 2) Code to implement the scheme, including initializing conditions and plotting solutions. 3) An analysis of error as the grid is refined, showing the method is second order. 4) Discussion of how a one-sided difference in time leads to an unstable scheme.

Uploaded by

api-408897152
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Problem Set 7

This document discusses numerically solving the 1D wave equation using finite differences. It presents: 1) The central difference formulas to discretize derivatives in space and time. 2) Code to implement the scheme, including initializing conditions and plotting solutions. 3) An analysis of error as the grid is refined, showing the method is second order. 4) Discussion of how a one-sided difference in time leads to an unstable scheme.

Uploaded by

api-408897152
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Problem Set 7

Caitlin McIlwain
June 2016

1. Stable Numerical solution of the wave equation:


(a) u(t + dt, x) as a function of u(t, x + dx) u(t, x − dx) u(t − dt, x)
Central Difference Formula:
f (x + h) − f (x − h)
2h
∂u(t, x) u(t, x + dx) − u(t, x − dx)
=
∂x 2dx
∂u(t, x) u(t + dt, x) − u(t − dt, x)
=
∂t 2dt
The Wave Equation is denoted as:
∂u ∂u
= −c (1)
∂t ∂x
So, by plugging the time and spatial discretizations into the wave
equation, we get:

u(t + dt, x) − u(t − dt, x) u(t, x + dx) − u(t, x − dx)


= −c
2dt 2dx
Where if we solve for u(t + dt, x), we get

dt
u(t + dt, x) = −c (u(t, x + dt) − u(t, x − dx)) + u(t − dt, x)
dx

(b) Boundary Conditions: Plugging in x=0, we get:

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

% points where you want to save


tsave = [0 0.5 1 1.5 2 2.5 3]’;

% points where you want to display solution on the plot


tplot = [0: 10*dt: tfinal]’;

% set up initial conditions


u= exp((-(x-1).^2)./0.01); %u(t=0,x)
up = u; % u(t= -dt, x)

% plot initial condtion


figure(1)
plot(x,u);
xlabel(’space, x’)
ylabel(’traveling wave, u’)
% define finite difference formula
lambda = -dt/dx
n=1000;
e=ones(n,1);
A=spdiags([e -e],[1 -1],n,n);
A(end,1)=-1;
A(1,end)=1;

% may need multiple lines to define matrix A


ii = 1;

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’)

% calculate actual solution


ureal = exp((-(x-4).^2)./0.01);
error = abs(u-ureal);
totalerror = trapz(x,error)
for i = 1 : 2: 2 * length(tsave)
figure(2)
plot(x, usave(:, i))
drawnow
hold on
end
Here is the plot produced:

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:

I also plotted the exact solution at t=3 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

0.4351 0.1234 0.0329 0.0082

Columns 6 through 8

0.0018 0.0010 0.0006

(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:

∂u(t, x) u(t, x + h) − u(t, x)


=
∂x h
In time, it can be written as:

∂u(t, x) u(t + dt, x) − u(t, x)


=
∂t dt
We can plug them into the wave equation which is
∂u ∂u
= −c (2)
∂t ∂x

u(t + dt, x) − u(t, x) u(t, x + dx) − u(t, x − dx)


=−
dt 2dx
−dt
u(t + dt, x) = (u(t, x + dx) − u(t, x − dx)) + u(t, x)
2dx
(b) Defining this equation:

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.

You might also like