Math 6840: Solutions for Problem Set...
Transcript of Math 6840: Solutions for Problem Set...
W.D. Henshaw Math 6840: Solutions for Problem Set 4
1. (10 points) von Neumann analysis in two-dimensions.
Consider the two-dimensional parabolic problem for u = u(x, y, t),
∂tu = κ1∂2xu+ κ2∂
2yu− αu, x ∈ [0, 2π]2,
u(x, y, 0) = u0(x, y),
with periodic boundary conditions. Here κ1 > 0, κ2 > 0 and α > 0.
(a) Write down the method-of-lines (MOL) scheme for this problem using second-order accuratecentral differences (CD2).
(b) Determine the time-stepping eigenvalue λk for the MOL scheme. Expand λk for small kx∆xand ky∆y to determine the order of accuracy of the scheme in space.
(c) Determine the amplification factor, A, when using the theta-scheme in time to solve the test-equation found in part (b).
(d) Determine the time-step restriction based on absolute stability when using the theta-scheme intime. Assume 0 ≤ θ ≤ 1.
Solution:
(a) Here is the MOL approximation (for periodic BC’s we add a ghost point at −1).
dVµνdt
=(κ1D+xD−x + κ2D+yD−y
)Vµ,ν − αVµ,ν , µ = 0, 1, . . . , Nx − 1, ν = 0, 1, . . . , Ny − 1,
VNx,ν(t) = V0,ν(t), V−1,ν(t) = VNx−1,ν(t), ν = −1, 0, 1, 2, . . . , Ny,
Vµ,Ny(t) = Vµ,0(t), Vµ,−1(t) = Vµ,Ny−1(t), µ = −1, 0, 1, 2, . . . , Nx,
Vµ,ν(0) = u0(xµ,ν , yµ,ν), µ = −1, 0, 1, 2, . . . , Nx − 1, ν = −1, 1, 2, . . . , Ny − 1,
(b) Let
Vµν = V̂ (t)eik·xµν
Using
D+xD−xeik·xµν =
−4 sin2(ξx/2)
∆x2,
D+yD−yeik·xµν =
−4 sin2(ξy/2)
∆y2,
where ξx = kx∆x and ξy = ky∆y, gives
dV̂
dt= λkV̂ ,
λk = −(κ1
4 sin2(ξx/2)
∆x2+ κ2
4 sin2(ξy/2)
∆y2
)− α.
1
For small kx∆x and small ky∆y we have
4 sin2(kx∆x/2)
∆x2= k2
x +O(∆x2),
4 sin2(ky∆y/2)
∆y2= k2
y +O(∆y2),
and thus
λk = −(κ1k
2x + κ2k
22
)+O(∆x2 + ∆y2)
and the scheme is second order accurate since the true symbol is λek = −(κ1k2x + κ2k
22)− α.
(c) Using the θ scheme gives (from the previous problem set)
A =1 + (1− θ)λk∆t
1− θλk∆t
(d) Following the previous problem set, the time-step restriction is as follows.For θ ≥ 1
2 there is no time-step restriction.For θ < 1
2 , we require
∆t(κ1
4
∆x2+ κ2
4
∆y2+ α
)≤ 2
1− 2θ
2
2. AFS
(a) The AFS scheme solves[I − ∆t
2D+xD−x
] [I − ∆t
2D+yD−y
]Un+1ij
=
[I +
∆t
2D+xD−x
] [I +
∆t
2D+yD−y
]Unij ,+
∆t
2
[f(xij , t
n+1) + f(xij , tn)],
def= Gij , i = 1, 2, . . . , Nx − 1, j = 1, 2, . . . , Ny − 1
with boundary and initial conditions
Un+10,j = gax(yj , t
n+1), Un+1Nx,j
= gbx(yj , tn+1), j = 0, 1, 2, . . . , Ny,
Un+1i,0 = gay(xi, t
n+1), Un+1i,Ny
= gby(xi, tn+1), i = 0, 1, 2, . . . , Nx,
U0i,j = u0(xij), i = 0, 1, 2, . . . , Nx, j = 0, 1, 2, . . . , Ny.
In matrix form this looks like[I − ∆t
2A
] [I − ∆t
2B
]Wn+1 =
[I +
∆t
2A
] [I +
∆t
2B
]Wn +
∆t
2
[Fn+1 + Fn
]These equations can be efficiently solved using a set of 1D tridiagonal solves. Define
U∗ij =
[I − ∆t
2D+yD−y
]Un+1ij .
Stage I: Solve the 1D tridiagonal systems in i (for each fixed j = 1, 2, . . . , Ny − 1)[I − ∆t
2D+xD−x
]U∗ij = Gij , i = 1, 2, . . . , Nx − 1,
with boundary conditions on the left and right,
U∗0,j =
[I − ∆t
2D+yD−y
]gax(yj , t),
U∗Nx,j =
[I − ∆t
2D+yD−y
]gbx(yj , t).
Stage II: Solve the 1D triagonal systems in j (for each fixed i = 1, 2, . . . , Nx − 1)[I − ∆t
2D+yD−y
]Un+1ij = U∗
ij , j = 1, 2, . . . , Ny − 1 (1)
with boundary conditions on the bottom and top,
Un+1i,0 = gay(xi, t
n+1), Un+1i,Ny
= gby(xi, tn+1).
Also set boundary conditions on the left and right
Un+10,j = gax(yj , t
n+1), Un+1Nx,j
= gbx(yj , tn+1), j = 0, 1, 2, . . . , Ny
(b)
Polynomial manufactured solution results:
3
>> heat2dAFSMMS
poly: t=0.50 CPU=2.00e-02 Nx= 20 Ny= 20 Nt= 10 dt=5.00e-02 max-Err=3.55e-15
poly: t=0.50 CPU=3.00e-02 Nx= 40 Ny= 40 Nt= 20 dt=2.50e-02 max-Err=1.42e-14 ratio=0.25
poly: t=0.50 CPU=1.00e-01 Nx= 80 Ny= 80 Nt= 40 dt=1.25e-02 max-Err=4.75e-14 ratio=0.30
poly: t=0.50 CPU=4.80e-01 Nx=160 Ny=160 Nt= 80 dt=6.25e-03 max-Err=1.07e-13 ratio=0.45
poly: t=0.50 CPU=2.95e+00 Nx=320 Ny=320 Nt=160 dt=3.13e-03 max-Err=1.35e-13 ratio=0.79
True solution results:
>> heat2dAFSMMS
true: t=0.50 CPU=2.00e-02 Nx= 20 Ny= 20 Nt= 10 dt=5.00e-02 max-Err=2.69e-03
true: t=0.50 CPU=3.00e-02 Nx= 40 Ny= 40 Nt= 20 dt=2.50e-02 max-Err=6.56e-04 ratio=4.10
true: t=0.50 CPU=1.00e-01 Nx= 80 Ny= 80 Nt= 40 dt=1.25e-02 max-Err=1.63e-04 ratio=4.02
true: t=0.50 CPU=4.70e-01 Nx=160 Ny=160 Nt= 80 dt=6.25e-03 max-Err=4.07e-05 ratio=4.01
true: t=0.50 CPU=2.52e+00 Nx=320 Ny=320 Nt=160 dt=3.13e-03 max-Err=1.02e-05 ratio=4.00
-0.05
2
1.5 1
0u
Heat Eqn true (AFS) t=5.000e-01 Nx=40
y
1
x
0.05
0.50.5
0 0
-0.04
-0.03
-0.02
-0.01
0
0.01
0.02
0.03
0.04
-1
2
-0.5
1.5 1
0
10-3
err
or
Error - Heat Eqn true (AFS) t=5.000e-01 Nx=40
0.5
y
1
x
1
0.50.5
0 0
-6
-4
-2
0
2
4
6
10-4
Figure 1: Solution and error for Nx = 40.
Listing 1: heat2dAFSMMS.m
1 %
2 % Solve the 2D heat equation using an Approximate Factored Scheme (AFS)
3 % and manufactured solutions
4 %
5 % u_t = kappa * (u_xx + u_yy ), ax < x < bx, ay < y < by, 0 < t <= tFinal
6 % u(ax,y,t) = gax(y,t), u(bx,y,t)=gbx(y,t), ay < y < by
7 % u(x,ay,t) = gay(x,t), u(x,by,t)=gby(x,t), ax < x < ay
8 % u(x,y,0) = u_0(x,y)
9 %
10 clear; clf; fontSize=14;
11 rainbow; % load rainbow colour table
1213 debug=0; % set to 1 for debugging
14 plotOption=1; % set to 1 for plotting
15 kappa=.05; % coefficient of diffusion
16 kx=3.*pi; % x-wave number in the IC and exact solution
17 ky=2.*pi; % y-wave number in the IC and exact solution
18 ax=0.; bx=1.; ay=0.; by=2.; % space interval interval
19 tFinal=.5; % final time
20
4
21 % exact solution function:
22 % ue = @(x,y,t) cos(kx*x).*cos(ky*y)*exp(-kappa*(kx^2+ky^2)*t);
2324 % Choose the manufactured solution:
25 ms = ’poly’;
26 % ms = ’trig’; % un-comment for trig MS
27 % ms = ’true’;
2829 if( strcmp(ms,’poly’) )
30 % Polynomial manufactured solution:
31 % b0=.5; b1=.7; b2=.0; % time coefficients
32 b0=.5; b1=.7; b2=.9; % time coefficients
33 c00=1.; c10=.8; c20=.6; c01=.3; c11=.25; c02=.2; % space coefficients
34 % b1=1.; b2=1.; c10=1; c20=1; c01=1; c11=.0; c02=.0;
3536 ue = @(x,y,t) (b0+b1*t+b2*t^2)*(c00 +c10*x +c20*x.^2 + c01*y + c11*x.*y + c02*y.^2);
37 uet = @(x,y,t) (b1+2.*b2*t )*(c00 +c10*x +c20*x.^2 + c01*y + c11*x.*y + c02*y.^2);
38 uexx = @(x,y,t) (b0+b1*t+b2*t^2)*(2.*c20);
39 ueyy = @(x,y,t) (b0+b1*t+b2*t^2)*(2.*c02);
40 f = @(x,y,t) uet(x,y,t) - kappa*(uexx(x,y,t)+ueyy(x,y,t)); % forcing
4142 elseif( strcmp(ms,’trig’) )
43 % Trigonometric manufactured solution:
44 kt = pi; kx=5*pi; ky=4.*pi;
45 ue = @(x,y,t) cos(kt*t)*cos(kx*x).*cos(ky*y);
46 uet = @(x,y,t) -kt*sin(kt*t)*cos(kx*x).*cos(ky*y);
47 uexx = @(x,y,t) (-kx^2)*ue(x,y,t);
48 ueyy = @(x,y,t) (-ky^2)*ue(x,y,t);
49 f = @(x,y,t) uet(x,y,t) - kappa*(uexx(x,y,t)+ueyy(x,y,t)); % forcing
5051 elseif( strcmp(ms,’true’) )
52 % exact solution function:
53 kx=3.*pi; ky=2.*pi;
54 ue = @(x,y,t) sin(kx*x).*cos(ky*y)*exp(-kappa*(kx^2+ky^2)*t);
55 f = @(x,y,t) 0.; % forcing
56 end
575859 % Choose BC’s and IC to match the true solution
60 gax = @(y,t) ue(ax,y,t); % BC RHS at x=ax
61 gbx = @(y,t) ue(bx,y,t); % BC RHS at x=bx
62 gay = @(x,t) ue(x,ay,t); % BC RHS at y=ay
63 gby = @(x,t) ue(x,by,t); % BC RHS at y=by
64 u0 = @(x,y) ue(x,y,0); % IC function
6566 numResolutions=5;
67 for m=1:numResolutions % Loop over grid resolutions
6869 % --- Setup the grid ---
70 Nx=10*2^m; Ny=Nx; % number of space intervals
71 dx=(bx-ax)/Nx; dy=(by-ay)/Ny; % grid spacing
7273 iax=1; iay=1; % index of boundary point at x=xa, y=ya
74 ibx=iax+Nx; iby=1+Ny; % index of boundary point at x=xb, y=yb
75 Ngx=ibx; Ngy=iby; % number of grid points in x and y
7677 i1x=iax+1; i1y=iay+1; % first interior pt in x and y
78 i2x=ibx-1; i2y=iby-1; % last interior pt in x and y
79
5
80 % -- form the 2D grid points ---
81 x = zeros(Ngx,Ngy); y = zeros(Ngx,Ngy); % grid
82 for( iy=1:Ngy )
83 for( ix=1:Ngx )
84 x(ix,iy)=ax + (ix-iax)*dx;
85 y(ix,iy)=ay + (iy-iay)*dy;
86 end;
87 end;
8889 % allocate space for the solution
90 un = zeros(Ngx,Ngy); % holds U_i^n
9192 dt = dx; % time step (adjusted below)
93 Nt = round(tFinal/dt); % number of time-steps
94 dt = tFinal/Nt; % adjust dt to reach tFinal exactly
9596 t=0.;
97 un = u0(x,y); % initial conditions
9899 % ----- Form the implicit matrices ------
100101 % Allocate the sparse matrix, vector-solution and RHS
102 Ax = sparse(Ngx,Ngx); % implicit matrix
103 Wx = zeros(Ngx,1); % solution as a vector
104 rhsx = zeros(Ngx,1); % right-hand-side
105106 Ay = sparse(Ngy,Ngy); % implicit matrix
107 Wy = zeros(Ngy,1); % solution as a vector
108 rhsy = zeros(Ngy,1); % right-hand-side
109110 % Fill in the interior equations: I - (.5*kappa*dt)*( D+x D-x )
111 for( ix=i1x:i2x )
112 ie = ix; % eqn number
113 Ax(ie,ix-1) = - (.5*kappa*dt)*( 1/dx^2 );
114 Ax(ie,ix ) = 1. - (.5*kappa*dt)*( -2*( 1/dx^2 ) );
115 Ax(ie,ix+1) = - (.5*kappa*dt)*( 1/dx^2 );
116 end;
117 % Boundary conditions for the matrix
118 ix=iax; ie=ix; Ax(ie,ie)=1.; % BC at x=xa
119 ix=ibx; ie=ix; Ax(ie,ie)=1.; % BC at x=xb
120121 % Fill in the interior equations: I - (.5*kappa*dt)*( D+y D-y )
122 for( iy=i1y:i2y )
123 ie = iy; % eqn number for pt (ix,iy)
124 Ay(ie,iy-1) = - (.5*kappa*dt)*( 1/dy^2 );
125 Ay(ie,iy ) = 1. - (.5*kappa*dt)*( -2*( 1/dy^2 ) );
126 Ay(ie,iy+1) = - (.5*kappa*dt)*( 1/dy^2 );
127 end;
128 % Boundary conditions for the matrix
129 iy=iay; ie=iy; Ay(ie,ie)=1.; % BC at y=ya
130 iy=iby; ie=iy; Ay(ie,ie)=1.; % BC at y=yb
131132 DpxDmx = @(u,I,J) (u(I+1,J)-2*u(I,J)+u(I-1,J))/(dx^2);
133 DpyDmy = @(u,I,J) (u(I,J+1)-2*u(I,J)+u(I,J-1))/(dy^2);
134135 % --- Start time-stepping loop ---
136 cpu0=cputime;
137138 dAx =decomposition(Ax,’lu’); % factor the matrix
6
139 dAy =decomposition(Ay,’lu’); % factor the matrix
140141 I1=i1x:i2x; I2=i1y:i2y; % interior points
142 J1=iax:ibx; J2=iay:iby; % includes boundary points
143144 for( n=1:Nt )
145146 t = n*dt; % new time
147148 % ======== STAGE I =====
149 % -- evaluate the right-hand-side at interior pts: (over-write un)
150 un(J1,I2) = un(J1,I2) + (.5*kappa*dt)*DpyDmy(un,J1,I2); % NOTE J1, include boundary pts
151 un(I1,I2) = un(I1,I2) + (.5*kappa*dt)*DpxDmx(un,I1,I2) ...
152 + (.5*dt)*( f(x(I1,I2),y(I1,I2),t) + f(x(I1,I2),y(I1,I2),t-dt) );
153154 % Boundary conditions for U^* = [I - .5*kappa*dt*D+yD-y] U^{n+1}
155 ix=iax; un(ix,I2)=gax(y(ix,I2),t) ...
156 - (.5*kappa*dt/dy^2)*( gax(y(ix,I2+1),t) -2.*gax(y(ix,I2),t) + gax(y(ix,I2-1),t) );
157 ix=ibx; un(ix,I2)=gbx(y(ix,I2),t) ...
158 - (.5*kappa*dt/dy^2)*( gbx(y(ix,I2+1),t) -2.*gbx(y(ix,I2),t) + gbx(y(ix,I2-1),t) );
159160 % --- solves in x-direction (for U^*) ---
161 un(J1,I2) = dAx\un(J1,I2);
162163 % ========== STAGE II =============
164 iy=iay; un(J1,iy)=gay(x(J1,iy),t); % BC at y=ya
165 iy=iby; un(J1,iy)=gby(x(J1,iy),t); % BC at y=yb
166167 % --- solves in y-direction ---
168 un(I1,J2) = (dAy\(un(I1,J2)’))’;
169170 ix=iax; un(ix,J2)=gax(y(ix,J2),t); % BC at x=xa
171 ix=ibx; un(ix,J2)=gbx(y(ix,J2),t); % BC at x=xb
172173 if debug==1
174 surf( x,y,un );
175 title(sprintf(’Solution t=%9.3e’,t));
176 uexact = ue(x,y,t); % eval exact solution:
177 errMax = max(max(abs(un-uexact))); % max-norm error
178 fprintf(’%s: t=%10.4e: Nx=%d Ny=%d Nt=%d dt=%9.3e max-Error=%8.2e\n’,ms,t,Nx,Ny,Nt,dt,errMax);
179 pause
180 % plot errors
181 surf( x,y,un-uexact );
182 title(sprintf(’Error - Heat Equation (AFS) t=%9.3e (Nx=%d) cfl=%4.2f’,t,Nx,cfl));
183 xlabel(’x’); ylabel(’y’); zlabel(’error’);
184 pause
185 end
186187 end;
188 % --- End time-stepping loop ---
189 cpu = cputime-cpu0;
190191 uexact = ue(x,y,t); % eval exact solution:
192 errMax = max(max(abs(un-uexact))); % max-norm error
193 err(m)=errMax;
194 fprintf(’%s: t=%4.2f CPU=%8.2e Nx=%3d Ny=%3d Nt=%3d dt=%8.2e max-Err=%8.2e’,ms,t,cpu,Nx,Ny,Nt,dt,
errMax);
195 if( m>1 ) fprintf(’ ratio=%4.2f\n’,err(m-1)/err(m)); else fprintf(’\n’); end;
196
7
197 % plot results
198 if( plotOption==1 && m==2 )
199 figure(1)
200 surf( x,y,un ); colormap(rainbowMap); colorbar;
201 title(sprintf(’Heat Eqn %s (AFS) t=%9.3e Nx=%d’,ms,t,Nx));
202 xlabel(’x’); ylabel(’y’); zlabel(’u’); set(gca,’FontSize’,fontSize);
203 % grid on;
204 plotName=sprintf(’heat2d_AFS_MS%s_N%i.eps’,ms,Nx);
205 print(’-depsc2’,plotName); % save as an eps file
206 % fprintf(’Wrote plot file %s\n’,plotName);
207208 figure(2)
209 % plot errors
210 surf( x,y,un-uexact ); colormap(rainbowMap); colorbar;
211 title(sprintf(’Error - Heat Eqn %s (AFS) t=%9.3e Nx=%d’,ms,t,Nx));
212 xlabel(’x’); ylabel(’y’); zlabel(’error’); set(gca,’FontSize’,fontSize);
213 plotName=sprintf(’heat2d_AFS_MS%s_N%i_err.eps’,ms,Nx);
214 print(’-depsc2’,plotName); % save as an eps file
215 % fprintf(’Wrote plot file %s\n’,plotName);
216217 pause(1);
218 end;
219220 end; % end loop over grid resolutions
8
3. (20 points) Advection equation. Consider the initial value problem for the advection equation
ut + cux = 0, 0 ≤ x < 2π, 0 ≤ t ≤ tfinal,
u(x, 0) = u0(x),
u(x+ 2π, t) = u(x, t),
with periodic boundary conditions. Discretize on a uniform grid xj = j∆x, j = −1, 0, . . . , Nx with∆x = 2π/Nx. Use one ghost point on the left to implement the periodic boundary conditions.
Solution:
(a) The exact solution is
u(x, t) = u0(x− ct)
(b) The Lax-Wendroff scheme is
Un+1i = Uni − c∆tD0U
ni + c2 ∆t2
2D+xD−xU
ni
Substituting Unj = Aneikxj leads to
A = 1− c∆t
∆xi sin(ξ)− 2
c2∆t2
∆x2sin2(ξ/2),
= 1− λi sin(ξ)− 2λ2 sin2(ξ/2),
= 1− 2λi sin(ξ/2) cos(ξ/2)− 2λ2 sin2(ξ/2),
where ξ = k∆x. For absolute stability we require |A| ≤ 1 or |A|2 ≤ 1. Now |A|2 is
|A|2 = (1− 2λ2 sin2(ξ/2))2 + 4λ2 sin2(ξ/2) cos2(ξ/2),
= 1− 4λ2 sin2(ξ/2) + 4λ4 sin4(ξ/2) + 4λ2 sin2(ξ/2)− 4λ2 sin4(ξ/2),
= 1 + 4λ2 sin4(ξ/2)(λ2 − 1)
Thus |A|2 ≤ 1 implies
1 + 4λ2 sin4(ξ/2)(λ2 − 1) ≤ 1,
=⇒ sin4(ξ/2)(λ2 − 1) ≤ 0,
=⇒ λ2 ≤ 1
The time-step restriction from absolute stability is thus
|c|∆t∆x
≤ 1
(c) The code is given below. Here are results for the sine initial condition
9
>> advSchemes
Solve the advection equation with scheme=LF
LF:sine: t=20.000: Nx= 20 Nt= 141 dt=1.42e-01 errMax=1.46e+00 errL1=5.94e+00
LF:sine: t=20.000: Nx= 40 Nt= 283 dt=7.07e-02 errMax=1.80e+00 errL1=7.33e+00 ratio=[0.81,0.81] (max,L1)
LF:sine: t=20.000: Nx= 80 Nt= 566 dt=3.53e-02 errMax=5.06e-01 errL1=2.05e+00 ratio=[3.56,3.57] (max,L1)
LF:sine: t=20.000: Nx=160 Nt=1132 dt=1.77e-02 errMax=1.26e-01 errL1=5.04e-01 ratio=[4.01,4.07] (max,L1)
LF:sine: t=20.000: Nx=320 Nt=2264 dt=8.83e-03 errMax=3.14e-02 errL1=1.25e-01 ratio=[4.02,4.02] (max,L1)
>> advSchemes
Solve the advection equation with scheme=LW
LW:sine: t=20.000: Nx= 20 Nt= 141 dt=1.42e-01 errMax=9.96e-01 errL1=4.05e+00
LW:sine: t=20.000: Nx= 40 Nt= 283 dt=7.07e-02 errMax=1.17e+00 errL1=4.75e+00 ratio=[0.85,0.85] (max,L1)
LW:sine: t=20.000: Nx= 80 Nt= 566 dt=3.53e-02 errMax=4.68e-01 errL1=1.87e+00 ratio=[2.50,2.54] (max,L1)
LW:sine: t=20.000: Nx=160 Nt=1132 dt=1.77e-02 errMax=1.24e-01 errL1=4.97e-01 ratio=[3.78,3.76] (max,L1)
LW:sine: t=20.000: Nx=320 Nt=2264 dt=8.83e-03 errMax=3.13e-02 errL1=1.25e-01 ratio=[3.96,3.97] (max,L1)
0 1 2 3 4 5 6
x
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
u
Advection Eqn. LF t=2.000e+01 (Nx=20) dt=1.42e-01
computed
true
0 1 2 3 4 5 6
x
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
u
Advection Eqn. LF t=2.000e+01 (Nx=40) dt=7.07e-02
computed
true
0 1 2 3 4 5 6
x
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
u
Advection Eqn. LF t=2.000e+01 (Nx=80) dt=3.53e-02
computed
true
0 1 2 3 4 5 6
x
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
u
Advection Eqn. LF t=2.000e+01 (Nx=160) dt=1.77e-02
computed
true
Figure 2: Leap-frog.
10
0 1 2 3 4 5 6
x
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
u
Advection Eqn. LW t=2.000e+01 (Nx=20) dt=1.42e-01
computed
true
0 1 2 3 4 5 6
x
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
u
Advection Eqn. LW t=2.000e+01 (Nx=40) dt=7.07e-02
computed
true
0 1 2 3 4 5 6
x
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
u
Advection Eqn. LW t=2.000e+01 (Nx=80) dt=3.53e-02
computed
true
0 1 2 3 4 5 6
x
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
u
Advection Eqn. LW t=2.000e+01 (Nx=160) dt=1.77e-02
computed
true
Figure 3: Lax-Wendroff.
11
(c) Periodic top-hat
>> advSchemes
Solve the advection equation with scheme=LF
LF:topHat: t=20.000: Nx= 20 Nt= 141 dt=1.42e-01 errMax=7.38e-01 errL1=1.55e+00
LF:topHat: t=20.000: Nx= 40 Nt= 283 dt=7.07e-02 errMax=5.55e-01 errL1=1.35e+00 ratio=[1.33,1.15] (max,L1)
LF:topHat: t=20.000: Nx= 80 Nt= 566 dt=3.53e-02 errMax=8.43e-01 errL1=1.02e+00 ratio=[0.66,1.32] (max,L1)
LF:topHat: t=20.000: Nx=160 Nt=1132 dt=1.77e-02 errMax=6.74e-01 errL1=7.42e-01 ratio=[1.25,1.38] (max,L1)
LF:topHat: t=20.000: Nx=320 Nt=2264 dt=8.83e-03 errMax=7.18e-01 errL1=6.53e-01 ratio=[0.94,1.14] (max,L1)
>> advSchemes
Solve the advection equation with scheme=LW
LW:topHat: t=20.000: Nx= 20 Nt= 141 dt=1.42e-01 errMax=5.48e-01 errL1=1.01e+00
LW:topHat: t=20.000: Nx= 40 Nt= 283 dt=7.07e-02 errMax=5.79e-01 errL1=8.46e-01 ratio=[0.95,1.20] (max,L1)
LW:topHat: t=20.000: Nx= 80 Nt= 566 dt=3.53e-02 errMax=6.57e-01 errL1=6.01e-01 ratio=[0.88,1.41] (max,L1)
LW:topHat: t=20.000: Nx=160 Nt=1132 dt=1.77e-02 errMax=6.41e-01 errL1=3.98e-01 ratio=[1.03,1.51] (max,L1)
LW:topHat: t=20.000: Nx=320 Nt=2264 dt=8.83e-03 errMax=6.73e-01 errL1=2.70e-01 ratio=[0.95,1.48] (max,L1)
0 1 2 3 4 5 6
x
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
1.2
u
Advection Eqn. LF t=2.000e+01 (Nx=20) dt=1.42e-01
computed
true
0 1 2 3 4 5 6
x
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
1.2
1.4
u
Advection Eqn. LF t=2.000e+01 (Nx=40) dt=7.07e-02
computed
true
0 1 2 3 4 5 6
x
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
1.2
1.4
u
Advection Eqn. LF t=2.000e+01 (Nx=80) dt=3.53e-02
computed
true
0 1 2 3 4 5 6
x
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
1.2
1.4
u
Advection Eqn. LF t=2.000e+01 (Nx=160) dt=1.77e-02
computed
true
Figure 4: Leap-frog.
12
0 1 2 3 4 5 6
x
-0.2
0
0.2
0.4
0.6
0.8
1
u
Advection Eqn. LW t=2.000e+01 (Nx=20) dt=1.42e-01
computed
true
0 1 2 3 4 5 6
x
-0.2
0
0.2
0.4
0.6
0.8
1
1.2
u
Advection Eqn. LW t=2.000e+01 (Nx=40) dt=7.07e-02
computed
true
0 1 2 3 4 5 6
x
-0.2
0
0.2
0.4
0.6
0.8
1
1.2
u
Advection Eqn. LW t=2.000e+01 (Nx=80) dt=3.53e-02
computed
true
0 1 2 3 4 5 6
x
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
1.2
1.4
u
Advection Eqn. LW t=2.000e+01 (Nx=160) dt=1.77e-02
computed
true
Figure 5: Lax-Wendroff.
13
Listing 2: advSchemes.m
1 %
2 % Solve the advection equation on a periodic domain with different schemes
3 %
4 % u_t = -c*u_x , 0 < x < 2*pi , 0 < t <= tFinal
5 % u(x+2*pi,t) = u(x,t),
6 % u(x,0) = u_0(x)
7 %
8 clear; clf;
91011 scheme=’LF’;
12 scheme=’LW’;
1314 solution=’sine’;
15 % solution=’topHat’;
1617 plotOption=0; % 1=plot movie
1819 c=2.; % wave speed
20 kx=4.; % wave number in the IC and exact solution
21 a=0.; b=2*pi; % space interval interval
22 tFinal=20.; % final time
23 cfl=.9; % cfl scale factor
2425 % define the exact solution function
26 if( strcmp(solution,’sine’) )
27 uexact = @(x,t) sin(kx*(x-c*t));
28 else
29 % top hat
30 uexact = @(x,t) periodicTopHat( x-c*t, 1.,3. );
31 end
3233 u0 = @(x) uexact(x,0); % initial condition function
3435 fprintf(’Solve the advection equation with scheme=%s\n’,scheme);
36 % Loop over grid resolutions:
37 for m=1:5
38 Nx=10*2^m; % number of space intervals
39 dx=(b-a)/Nx; % grid spacing
4041 nga=1; % number of ghost on left
42 ia=1+nga; % index of boundary point at x=a
43 ib=ia+Nx; % index of boundary point at x=b
44 i1=ia; % first index to apply equation at.
45 i2=ib-1; % last index to apply equation
46 Ng=ib; % number of grid points
4748 x = linspace(a-nga*dx,b,Ng)’; % spatial grid (include ghost points)
4950 % allocate space for the solution at two levels
51 un = zeros(Ng,1); % holds U_i^n
52 unp1 = zeros(Ng,1); % holds U_i^{n+1}
5354 dt = cfl*dx/c; % time step (adjusted below)
55 Nt = round(tFinal/dt); % number of time-steps
56 % fprintf(’dt=%9.3e, adjusted=%9.3e, diff=%8.2e\n’,dt,tFinal/Nt,abs(dt-tFinal/Nt));
57 dt = tFinal/Nt; % adjust dt to reach tFinal exactly
14
58 I = i1:i2;
5960 Dz = @(u,I) (u(I+1) -u(I-1))/(2.*dx);
61 DpDm = @(u,I) (u(I+1)-2*u(I)+u(I-1))/(dx^2);
6263 t=0.;
64 un = u0(x); % initial conditions
65 if( strcmp(scheme,’LF’) )
66 um = uexact(x,t-dt); % Leap Frog needs u(-dt)
67 end
6869 if( plotOption > 0 ) % plot initial conditions
70 plot(x,un,’b-+’); title(sprintf(’%s: t=%9.3e’,scheme,t)); xlabel(’t’); xlim([0,2*pi]); set(gca
,’FontSize’,14);
71 drawnow;
72 pause;
73 end
7475 % --- Start time-stepping loop ---
76 for( n=1:Nt )
77 t = n*dt; % new time
7879 if( strcmp(scheme,’LF’) )
8081 % Leap-frog
82 unp1(I) = um(I) + (-2*c*dt )*Dz(un,I);
83 um = un;
8485 else
8687 % Lax-Wendroff:
88 unp1(I) = un(I) + (-c*dt)*Dz(un,I) + (.5*c^2*dt^2)*DpDm(un,I);
8990 end
9192 unp1(ia-1)=unp1(ia-1+Nx); % periodic BC on left
93 unp1(ib )=unp1(ib -Nx); % periodic BC at x=b
9495 un=unp1; % Set un <- unp1 for next step
9697 if( plotOption>0 ) % make a movie
98 plot(x,un,’b-+’); title(sprintf(’%s: t=%9.3e’,scheme,t)); xlabel(’t’); xlim([0,2*pi]); set(gca
,’FontSize’,14);
99 drawnow; pause(.1);
100 end
101 end;
102 % --- End time-stepping loop ---
103104 ue = uexact(x,t); % eval exact solution:
105 err = un(ia:ib-1)-ue(ia:ib-1); % error (do not include periodic ghost);
106 errMax(m) = max(abs(err)); % max-norm error
107 errL1(m) = norm(err,1)*dx; % discrete L1-norm
108 fprintf(’%s:%s: t=%6.3f: Nx=%3d Nt=%4d dt=%8.2e errMax=%8.2e errL1=%8.2e’,scheme,solution,t,Nx,Nt
,dt,errMax(m),errL1(m));
109 if( m==1 ) fprintf(’\n’); else fprintf(’ ratio=[%3.2f,%3.2f] (max,L1)\n’,errMax(m-1)/errMax(m),
errL1(m-1)/errL1(m)); end;
110111 % plot results
112 plot( x(ia:ib),un(ia:ib),’b-o’, x(ia:ib),ue(ia:ib),’k-’,’Linewidth’,2);
15
113 legend(’computed’,’true’); set(gca,’FontSize’,14);
114 title(sprintf(’Advection Eqn. %s t=%9.3e (Nx=%d) dt=%8.2e’,scheme,t,Nx,dt));
115 xlabel(’x’); ylabel(’u’); xlim([0,2*pi]);
116 grid on;
117 plotFile=sprintf(’adv_%s_%s_Nx_%d.eps’,scheme,solution,Nx);
118 print(’-depsc2’,plotFile); % fprintf(’Saved file %s\n’,plotFile);
119120 if( plotOption>0 ) pause; end;
121122 end; % end for m
16