Programming assignment #2 Results
Numerical Methods for PDEs Spring 2007
Jim E. Jones
Assignment #2
• Forward Difference method (explicit)
• Backward difference method (implicit)
• Crank-Nicolson method (implicit)
lxxfxu
ttlutu
tlxtxx
utx
t
u
0),()0,(
0,0),(),0(
0,0),,(),(2
22
Assignment #2 will is due Wednesday Feb 21. You will code up these three methods for a particular problem and look at accuracy and stability issues.
PDE solution is:
The Burden and Faires text contains results for this example
Assignment #2
lxxfxu
ttlutu
tlxtxx
utx
t
u
0),()0,(
0,0),(),0(
10,0),,(),(2
22
)sin()(,1,1 xxfl
)sin(),(2
xetxu t
Your job is to experiment with different values of h and k. Do your best to investigate numerically some of the issues we’ve talked about in the lecture.
• Stability: Run at least two problems with forward differences. One that satisfies the stability condition and one that does not. Comment on your observations. We’ve not seen it yet, but the other two methods are unconditionally stable.
•Convergence: Backward and Forward differencing has truncation error O(k+h2). Crank-Nicolson is O(k2+h2). Calculate the errors you see and comment on how they agree, or not, with these truncation error results.
•Comparison: Comment on the relative strengths and weaknesses of the three methods.
Assignment #2
Forward.mfunction [ w ] = Forward(N,T) h=1/Nk=1/Tr=k/(h*h); wnew=zeros(N+1,1);wold=zeros(N+1,1);for i=2:N+1 wold(i)=PDEsolution((i-1)*h,0);end for tstep=1:T for i=2:N wnew(i)=r*(wold(i-1)+wold(i+1))+(1-2*r)*wold(i); end wold=wnew;end emax=0;for i=2:N err=wnew(i)-PDEsolution((i-1)*h,1); if abs(err) > emax emax=abs(err); endendemax=emax
function u=PDEsolution(x,t) u=exp(-pi*pi*t)*sin(pi*x);
i=1 i=8
tstep=0
tstep=3
Example: N=7, T=3
Backward.mfunction [ w ] = Backward(N,T)h=1/Nk=1/Tr=k/(h*h); wnew=zeros(N-1,1);wold=zeros(N-1,1);for i=1:N-1 wold(i)=PDEsolution(i*h,0);end a=-r*ones(N-1,1);b=(1+2*r)*ones(N-1,1);c=-r*ones(N-1,1); for tstep=1:T wnew=tridisolve(a,b,c,wold); wold=wnew;end emax=0;for i=1:N-1 err=wnew(i)-PDEsolution(i*h,1); if abs(err) > emax emax=abs(err); endendemax=emax
i=1 i=6
tstep=0
tstep=3
Example: N=7, T=3
CrankN.mfunction [ w ] = CrankN(N,T) h=1/Nk=1/Tr=k/(h*h); wnew=zeros(N-1,1);wold=zeros(N-1,1);for i=1:N-1 wold(i)=PDEsolution(i*h,0);end a=-(r/2)*ones(N-1,1);b=(1+r)*ones(N-1,1);c=-(r/2)*ones(N-1,1);d=zeros(N-1,1); for tstep=1:T d(1)=(r/2)*wold(2)+(1-r)*wold(1); for i=2:N-2 d(i)=(r/2)*(wold(i-1)+wold(i+1))+(1-r)*wold(i); end d(N-1)=(r/2)*wold(N-2)+(1-r)*wold(N-1); wnew=tridisolve(a,b,c,d); wold=wnew;end
i=1 i=6
tstep=0
tstep=3
Example: N=7, T=3
emax=0;for i=1:N-1 err=wnew(i)-PDEsolution(i*h,1); if abs(err) > emax emax=abs(err); endendemax=emax
Forward Differences: Results
Forward h 0.1 0.05 0.0025 0.00125
k 0.01 * * * *
k 0.0025 2.07E-06 * * *
k 0.000625 2.70E-06 5.23E-07 * *
k 0.00015625 3.94E-06 6.61E-07 1.31E-07 *
k 3.90625E-05 4.25E-06 9.60E-07 1.64E-07 3.28E-08
Infinity norm of error at T=1
* Denotes method diverged
Forward Differences: Results
Forward h 0.1 0.05 0.0025 0.00125
k 0.01 * * * *
k 0.0025 2.07E-06 * * *
k 0.000625 2.70E-06 5.23E-07 * *
k 0.00015625 3.94E-06 6.61E-07 1.31E-07 *
k 3.90625E-05 4.25E-06 9.60E-07 1.64E-07 3.28E-08
Infinity norm of error at T=1
* Denotes method diverged
•Stability?
Forward Differences: Results
Forward h 0.1 0.05 0.0025 0.00125
k 0.01 * * * *
k 0.0025 2.07E-06 * * *
k 0.000625 2.70E-06 5.23E-07 * *
k 0.00015625 3.94E-06 6.61E-07 1.31E-07 *
k 3.90625E-05 4.25E-06 9.60E-07 1.64E-07 3.28E-08
Infinity norm of error at T=1
* Denotes method diverged
•Stability? r=k/h2
r=.25
r=1.0
Forward Differences: Results
Forward h 0.1 0.05 0.0025 0.00125
k 0.01 * * * *
k 0.0025 2.07E-06 * * *
k 0.000625 2.70E-06 5.23E-07 * *
k 0.00015625 3.94E-06 6.61E-07 1.31E-07 *
k 3.90625E-05 4.25E-06 9.60E-07 1.64E-07 3.28E-08
Infinity norm of error at T=1
* Denotes method diverged
•Accuracy?
Forward Differences: Results
Forward h 0.1 0.05 0.0025 0.00125
k 0.01 * * * *
k 0.0025 2.07E-06 * * *
k 0.000625 2.70E-06 5.23E-07 * *
k 0.00015625 3.94E-06 6.61E-07 1.31E-07 *
k 3.90625E-05 4.25E-06 9.60E-07 1.64E-07 3.28E-08
Infinity norm of error at T=1
* Denotes method diverged
•Accuracy? O(k+h2)
Backward Differences: Results
Infinity norm of error at T=1
* Denotes method diverged
Backward h 0.1 0.05 0.0025 0.00125
k 0.01 3.62E-05 3.15E-05 3.04E-05 3.01E-05
k 0.0025 1.14E-05 7.75E-06 6.87E-06 6.65E-06
k 0.000625 6.06E-06 2.68E-06 1.86E-06 1.66E-06
k 0.00015625 4.78E-06 1.46E-06 6.59E-07 4.61E-07
k 3.90625E-05 4.46E-06 1.16E-06 3.62E-07 1.64E-07
Backward Differences: Results
Infinity norm of error at T=1
* Denotes method diverged
Backward h 0.1 0.05 0.0025 0.00125
k 0.01 3.62E-05 3.15E-05 3.04E-05 3.01E-05
k 0.0025 1.14E-05 7.75E-06 6.87E-06 6.65E-06
k 0.000625 6.06E-06 2.68E-06 1.86E-06 1.66E-06
k 0.00015625 4.78E-06 1.46E-06 6.59E-07 4.61E-07
k 3.90625E-05 4.46E-06 1.16E-06 3.62E-07 1.64E-07
•Stability?
Backward Differences: Results
Infinity norm of error at T=1
* Denotes method diverged
Backward h 0.1 0.05 0.0025 0.00125
k 0.01 3.62E-05 3.15E-05 3.04E-05 3.01E-05
k 0.0025 1.14E-05 7.75E-06 6.87E-06 6.65E-06
k 0.000625 6.06E-06 2.68E-06 1.86E-06 1.66E-06
k 0.00015625 4.78E-06 1.46E-06 6.59E-07 4.61E-07
k 3.90625E-05 4.46E-06 1.16E-06 3.62E-07 1.64E-07
•Accuracy?
Backward Differences: Results
Infinity norm of error at T=1
* Denotes method diverged
Backward h 0.1 0.05 0.0025 0.00125
k 0.01 3.62E-05 3.15E-05 3.04E-05 3.01E-05
k 0.0025 1.14E-05 7.75E-06 6.87E-06 6.65E-06
k 0.000625 6.06E-06 2.68E-06 1.86E-06 1.66E-06
k 0.00015625 4.78E-06 1.46E-06 6.59E-07 4.61E-07
k 3.90625E-05 4.46E-06 1.16E-06 3.62E-07 1.64E-07
•Accuracy? O(k+h2)
Crank-Nicolson: Results
Infinity norm of error at T=1
* Denotes method diverged
Crank N h 0.1 0.05 0.0025 0.00125
k 0.1 2.93E-05 3.10E-05 3.15E-05 3.16E-05
k 0.05 5.93E-06 8.79E-06 9.48E-06 9.65E-06
k 0.0025 1.66E-06 1.53E-06 2.29E-06 2.49E-06
k 0.00125 3.68E-06 4.05E-07 3.84E-07 5.80E-07
Crank-Nicolson: Results
Infinity norm of error at T=1
* Denotes method diverged
Crank N h 0.1 0.05 0.0025 0.00125
k 0.1 2.93E-05 3.10E-05 3.15E-05 3.16E-05
k 0.05 5.93E-06 8.79E-06 9.48E-06 9.65E-06
k 0.0025 1.66E-06 1.53E-06 2.29E-06 2.49E-06
k 0.00125 3.68E-06 4.05E-07 3.84E-07 5.80E-07
Note K values are much larger than in previous tables. This means fewer time steps = less work. Also they are reduced by a factor of 2 (not 4) in each successive row.
Crank-Nicolson: Results
Infinity norm of error at T=1
* Denotes method diverged
Crank N h 0.1 0.05 0.0025 0.00125
k 0.1 2.93E-05 3.10E-05 3.15E-05 3.16E-05
k 0.05 5.93E-06 8.79E-06 9.48E-06 9.65E-06
k 0.0025 1.66E-06 1.53E-06 2.29E-06 2.49E-06
k 0.00125 3.68E-06 4.05E-07 3.84E-07 5.80E-07
•Stability?
Crank-Nicolson: Results
Infinity norm of error at T=1
* Denotes method diverged
Crank N h 0.1 0.05 0.0025 0.00125
k 0.1 2.93E-05 3.10E-05 3.15E-05 3.16E-05
k 0.05 5.93E-06 8.79E-06 9.48E-06 9.65E-06
k 0.0025 1.66E-06 1.53E-06 2.29E-06 2.49E-06
k 0.00125 3.68E-06 4.05E-07 3.84E-07 5.80E-07
•Accuracy?
Crank-Nicolson: Results
Infinity norm of error at T=1
* Denotes method diverged
Crank N h 0.1 0.05 0.0025 0.00125
k 0.1 2.93E-05 3.10E-05 3.15E-05 3.16E-05
k 0.05 5.93E-06 8.79E-06 9.48E-06 9.65E-06
k 0.0025 1.66E-06 1.53E-06 2.29E-06 2.49E-06
k 0.00125 3.68E-06 4.05E-07 3.84E-07 5.80E-07
•Accuracy? O(k2+h2)