You are on page 1of 3

ME 614 Homework 2

Prof. S. H. Frankel
Due Date: February 10, 2011
Abstract
This assignment deals with material in Chapter 3 of your textbook and is related to basic concepts
associated with finite-difference approximations of partial derivatives and of PDEs such as convergence,
consistency, and stability. Some coding is required since the best way to learn a numerical method is to
code it up yourself. Enjoy! Prepare handwritten write-ups on engineering paper of all the problems and
your computer code, graphics, and sample output in an electronic version.

Problems
1. Solve problem 3.12 in your textbook.
2. Find the most accurate formula for the first derivative at xi utilizing known values of f at xi1 , xi , xi+1 and
xi+2 . The points are uniformly spaced. Give the leading error term and state the order of the method.
3. Modified wavenumbers for non-central finite-difference schemes are complex. Derive the modified
3fj +4fj+1 fj+2
wavenumber for the down-wind scheme given by fj0 =
+ O(h2 ) and studied in problem
2h
3.12 (above). Plot its real and imaginary parts separately and discuss your results.
4. Solve problem 3.25 in your textbook.

Computer Programming Practice

In order to continue to develop and test your Fortran90 programming skills, and to prepare you for some of
the computer projects to follow, please write Fortran90 programs to do the following:
1. Write a Fortran90 computer program to use the fourth-order Pade scheme and the third-order boundary
schemes given in the notes to differentiate f (x) = sin(5x) on 0 x 3. Use fifteen uniformly spaced
points. Plot the computed derivative and the exact derivative as a function of x on the same graph.
Comment on the accuracy of the results. For coding you will need to solve a tridiagonal system of
linear algebraic equations. Use the subroutine in Appendix A of your book or the one included in this
handout below. Consider testing it for a simple 3 3 system for which you know the answer. Again
create a subroutine derivs and call the tridiagonal solver from there.
SUBROUTINE thomas(neqn,bb,aa,cc,xx,qq)
REAL, INTENT(IN), DIMENSION(neqn) :: qq, aa, bb, cc
INTEGER, INTENT(IN) :: neqn
INTEGER :: i
REAL, INTENT(OUT), DIMENSION(neqn) :: xx
REAL, DIMENSION(neqn) :: l, u, d, yy
!
! aa(1) cc(1) 0 ----------0
! bb(1) aa(2) cc(2) 0 -----0
! 0
bb(2) aa(3) cc(3)---0
!
aa(n-1)cc(n-1)
!
bb(n-1)aa(n)
!

! LU decomposition
d(1) = aa(1)
u(1) = cc(1)
do i = 1, neqn-2
l(i)
= bb(i)/d(i)
d(i+1) = aa(i+1) - l(i)*u(i)
u(i+1) = cc(i+1)
end do
l(neqn-1) = bb(neqn-1)/d(neqn-1)
d(neqn)
= aa(neqn) - l(neqn-1)*u(neqn-1)
! Forward substituion Lyy=qq
yy(1) = qq(1)
do i = 2, neqn
yy(i) = qq(i) - l(i-1)*yy(i-1)
end do
! Backward substitution Ux=yy
xx(neqn) = yy(neqn)/d(neqn)
do i = neqn-1,1,-1
xx(i) = (yy(i) - u(i)*xx(i+1))/d(i)
end do
END SUBROUTINE thomas
2. As we will learn, in the context of time-marching problems governed by parabolic and hyperbolic PDEs,
the semi-discrete method or method of lines, is an important approach with regard to stability analysis
of time-dependent PDEs and for implementing higher-order spatial discretization schemes. In this
approach, we first discretize in space alone, which gives a large system of ODEs with each component
of the sysytem corresponding to the solution at some grid point, as a function of time. The system of
ODEs can then be solved using one of the methods for ODEs that you may have seen before and that
is described below.
Consider the classical fourth-order four-stage Runge-Kutta method for a system of initial value problems or IVPs u0 = f (t, u) on a t b and u(a) = and let w represent the numerical approximation
to the exact solution u:
1
wi+1 = wi + (k1 + 2k2 + 2k3 + k4 )
6
with



h
k1
k1 = hf(ti , wi ); k2 = hf ti + , wi +
2
2


h
k2
k3 = hf ti + , wi +
; k4 = hf(ti + h, wi + k3 )
2
2

(1)

(2)
(3)

Use the following subroutine which employs whole array operations to solve an IVP system. You will
need to write a subroutine called rhs that evaluates an array dimensioned to the number of dependent
variables you have to solve and evaluated according to the rhs of the system you are actually solving.
The rk4 subroutine would be called as part of a do-loop that marches the solution forward in time. Test
x
your subroutine using a scalar ODE such as dx
dt = 1 + t on 1 t 6 subject to x(1) = 1. The solution
2

at t = 6.0 should be 16.728424 as the exact solution is x(t) = t(1+ln(t)). Then test your routine for the
following famous non-linear second-order van der Pol differential equation x00 +(x2 1)x0 +x = 0 where
 is a positive constant. Convert this equation into a system of two first-order ODEs by introducing
u1 = x and u2 = x0 and take  = 4 and apply the initial condition x(0) = 0.75, x0 (0) = 0 and use a
time step of 1 105 to solve until t = 20. Plot the solutions versus time and also make a phase plot
of u2 vs. u1 .
SUBROUTINE rk4(u)
USE types_vars
IMPLICIT NONE
REAL(DP), DIMENSION(nvar) :: u, rhs, uold
REAL(DP), DIMENSION(nvar) :: rkc1,rkc2,rkc3,rkc4
rhs = 0.0 ! Initialize rhs
uold = u

! Store current solution as uold

! RK4
! Step 1
call rhseqn(u,t,rhs)
rkc1 = dt * rhs
u = uold + onehalf * rkc1
! Step2
call rhseqn(u,t+onehalf*dt,rhs)
rkc2 = dt * rhs
u = uold + onehalf* rkc2
! Step3
call rhseqn(u,t+onehalf*dt,rhs)
rkc3 = dt * rhs
u = uold + rkc3
! Step4
call rhseqn(u,t+dt,rhs)
rkc4 = dt * rhs
u = uold + onesixth*(rkc1 + 2.*rkc2 + 2.*rkc3 + rkc4)
END SUBROUTINE rk4

You might also like