Homework/Programming 10. (Due April 16)

advertisement
Homework/Programming 10. (Due April 16)
Math. 417
Problem 1. Develop a MATLAB driver code for implementing the forward
Euler time stepping method applied to the ODE/system
dy
(1.1)
= f (t, y).
dt
with initial data y(0) = y0 over the interval [0, t1] using N steps of uniform
size k = (t1 − t0)/N . This is basically the routine given in class and has the
interface
function y1=feuler(t0,t1,N,y0,feval)
Here y0 is the initial value (at t = t0) and the result y1 is the approximation
to the solution at t1. f eval is the name of the matlab function which returns
f (t, y). As discussed in class, the vectorization syntax of MATLAB allows
this routine to simultaneously work for the single variable case as well as the
system case.
Apply your forward Euler routine to the single variable problem y : [0, 1] → R
satisfying
dy
= −y.
dt
with y(0) = 1. For this example, f (t, y) = −y and you can either create
an m-file function for evaluating f or simply use an inline function in the
calling program.
Test your code by running with small N , e.g, N = 1 and N = 2. When you
are convinced that your computations are correct, approximate the solution
at t1 = 1 varying N = 2, 4, 8, 16, 32, 64. The solution of the above ODE is
y(t) = e−t and hence y(1) = e−1 . Report your approximation at t1 = 1 and
the error |Y N − e−1 | as a function of N . Is the error behavior consistent
with first order convergence?
We next consider numerically approximating the system
dy
= fR − h−2 A3 y.
(1.2)
dt
This is a system of n equations where n is determined by which A3 file is
used and fR is a fixed vector in Rn . The matrices A3 come from the CRS
1
2
files used in the previous two computing assignments and h = 1/(n + 1). In
all cases, we take fR to be the vector with all entries equal to 1. We apply
forward Euler with ti = ik, i.e., a uniform step size equal to k.
To use your forward Euler routine to approximate this system, you need to
set y0 to be an n × 1 matrix (vector). The following m-file routine provides
the function f (t, v) = fR − h−2 A3 v needed to approximate (1.2):
function f=feval(t,w)
[n,m]=size(w);
ohs=(n+1)*(n+1);
f=ones(n,1)-ohs*crseval(w);
Here the routine crseval(w) evaluates A3 w (with A3 a matrix in CRS form)
and w an n × 1 matrix (vector). You should already have this routine as it
was needed in the previous assignment.
The trouble with applying forward Euler to the above problem, indeed with
any explicit method, is that the ODE approximation scheme is unstable
unless k is sufficiently small. For forward Euler to be stable, it is necessary
that k < 2/λn where λn is the largest eigenvalue of the matrix on the right
hand side, i.e., h−2 A3 . This will be discussed in more detail in a later class.
From our earlier homework, we find
(1.3)
k<
2h2
h2
=
.
2 − 2 cos(nπ/(n + 1))
1 − cos(nπ/(n + 1))
When you studied ODE’s, you found that some systems had critical points
(solutions of the time independent system) and you further characterized
them (at least in the case of systems of two independent variables) as stable,
unstable, spiral points, centers, etc. The above equation has a unique critical
point which is the solution of time independent equation
h−2 A3 u = fR .
The reason that this is true will be discussed in a later class. The above
system is the finite difference approximation to the boundary value problem
−w′′ = 1,
on (0, 1),
w(0) = w(1) = 0.
3
Its solution is w(x) = x(1 − x)/2. When forward Euler is stable, you will
be able to see that the solution and the approximate solution are essentially
identical. The following MATLAB code can be used to plot the approximate
and actual solution:
h=1./(n+1);
x=h:h:1-h;
s=.5*x.*(1-x);
plot(x,yn,x,s)
%
%
%
%
Sets a uniform mesh from h to 1-h.
This is the actual solution on the mesh.
Plots the computed solution (yn) and
the actual solution
Problem 2. Write a driver routine for the application of forward Euler to
the system (1.1). Its interface should be
function h10(filen,N,t1)
Here N is the number of steps, t1 is the end time and filen is the CRS file
name, e.g., filen=’A3-txt’. The driver needs to have the global statement
and must read the CRS file so that it is available to crseval when needed. It
also sets up the initial iterate (we shall always uses the y0 = 0).
The first set of runs illustrate the stability issues mentioned above. For
these runs, we will use the smallest CRS file, namely, A3-8.txt. From (1.3),
we see that for stability (with n = 8), we need to have k < 1/157.12 or
N > 157.12 when t1=1. To illustrate the unstabilty behavior, run N =
40, 60, 80, 100, 120 and look at the plots. MATLAB scales the problem so
that both the actual solution and the approximate solution can be plotted
in the same picture. The five runs are unstable. Report the scaling factor
that matlab uses for the plots as a function of N . Next run N = 144.
Somewhat surprisingly, the actual and approximate solutions appear to be
the same. This seems to contradict (1.3). The issue here is that we have
not run enough steps to see the instability. Run with the same k, but use
t1 = 2, 3, 4. Report the MATLAB scaling factor for the resulting plots.
Problem 3. This problem illustrates what happens when k is small enough
so that the approximation is stable. Compute the approximate solution at
t = 1 using N = 200n2 /64 for each of the six A3 problems of Homework 7.
Turn in the plots corresponding to n = 8, 32, 128.
Download