Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

MAT 420 Test 2

Spring 2022

DIRECTIONS

• This exam includes four problems. The first two are mandatory and only one of the last two is required for full credit (100 points). If the first two problems are fully completed, then up to ten points of extra credit may be earned by completing all four problems.

• Follow the directions very carefully, especially when made explicit.

• For each problem include in your exam a printout of any code you wrote yourself, example runs, and/or appropriate figures when needed. Be sure to describe the problem and to explain the logic of your solutions. Your code should be sufficiently documented. Screenshots will not be accepted.

• Clearly label each problem/question in your write-up. Make sure your name appears clearly on the first page.  You may not work in groups.  Web resources are allowed but must be referenced and acknowledged within the report.

• Writeups must be typeset with LATEX.

• Turn in your work on Canvas by 23:59 Thursday, May 5. Your submission should include:

1. a PDF file  t e s t 2 _ L A S T N A M E  .  p d f      containing all material (codes, output, explanations)

2. a tarball  t e s t 2 _ L A S T N A M E  .  t g z      with all your files identified by problem numbers. All code and relevant data necessary to complete the problem should be included in this file. The directory structure should match (file structure may vary):

L A S T N A M E /

p 1 /

Example script

p 2 /

M a  k e f i l e         .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .       Example Makefile

p 2  .  f 9 0       .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .     Example code

p 2 a  .  o u t        .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .      Example required output

p 2 b  .  o u t        .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .      Example required output

p 2 c  .  o u t        .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .      Example required output

script

p 3 /

Example script

p 4 /

Example script


1.  [20 pts REQUIRED] This problem is a collection of short questions.

(a)  [10 pts] A Fortran 90 program m a i n  .  f 9 0    uses functions f 1    and f 2    defined in a module

m o d u l e 1  .  f 9 0 .  What (bash) line command do you need to execute in order to produce an executable named p r o g r a m ?

(b)  [10 pts] Consider the following Fortran 90 program:

1

2

3

4

5

6

7

8

9

1 0

1 1

1 2

p r o g i m r e d o

 

 

 

 

 

 

e  p e n d

n r

 

r a m     p l i c a l  ( 8

a = a / b = a + i f       (

a = 2 *

e x i t d      d o i n t      p r o g

p

i

)

1

t

b

n

:   :

 

 

2     1 d b

a

0

>

 

 

'   ( 4X

r a m

 

o n e

a      =     1 d 0

 

 

 

0 )

 

 

 

E S 2 3 .

p 1

 

b

 

 

 

l e

 

 

 

e 3 )   '

 

 

 

 

 

 

 

 

 

'  a      =       '

 

 

 

 

 

 

 

 

 

 

,

 

 

 

 

 

 

 

 

 

a

i) What does this program compute?

ii) What is the value of  a      at the end of the program?

iii) Would you change anything in this program? Why/how, or why not?

2.  [40pts REQUIRED] Consider the 2-point boundary value problem    u (x) + 2au (x) = 1;   (0 < x < 1)

where a is a positive parameter. Discretize (1) on a uniform grid

0 = x0  < x1  < : : : < xN  < xN+1  = 1

with N + 1 intervals of length  = xi  xi   1  = 1/(N + 1).

(a) Verify (algebraically) that the exact solution is given by

1 + (e2a  1)x  e2ax

2a(e2a   1)        ;

(b)  Use Python to plot the exact solution (2) for a 2 f0:1; 1; 10g.

(c) What is the exact solution to (1) for a = 0?

(d) What happens as a gets larger?

To obtain a numerical solution ui    u(xi ) we enforce the differential equation in (1) at all interior points xi  = i , i = 1; : : : ; N , with  = 1/(N + 1), and use the approximations

ui+1  ui   1                                              ui   1  2ui + ui+1

The resulting system of N linear equations

= 1;    1  i  N;                         (3)

together with the boundary conditions u0  = uN+1  = 0 form a size N  N tridiagonal matrix T, a vector of unknowns u of size N , and a size N constant right-hand side vector of ones,

Tu = T 26 u..1 37 =  261..37 :                                                      (4)

4uN 5      415

(e) Write a double precision Fortran 90 program implementing this system. Use a tridiagonal storage scheme for T, as explained in the LAPACK routine  d g t s v    .

(f)  Use the LAPACK routine  d g t s v      to solve the system. Write the solution to a file showing the numerical approximation ui   (second column) vs the location xi   (first column) for 0  i  N + 1. Consider the following values of a and N :

i. a = 1, N = 10 (save output to  p 2 a  .  o u t    )

ii. a = 10, N = 10 (save output to  p 2 b  .  o u t    )

iii. a = 10, N = 100 (save output to  p 2 c  .  o u t    )

(g)  For each case, plot the numerical solution with the exact solution on the same plot.

Include in  t e s t 2 _ L A S T N A M E  .  t a r  .  g z       your Fortran 90 program  p 2  .  f 9 0    , a  M a k e f i l e    , the out- put files  p 2 a  .  o u t    ,   p 2 b  .  o u t    ,   p 2 c  .  o u t    ,  and your plotting file  p 2  .  p y    .   The  PDF report t e s t 2 _ L A S T N A M E  .  p d f      should of course include all plots, outputs, and comments.

DO ONE OF THE FOLLOWING TWO PROBLEMS FOR FULL CREDIT

(both for up to 10 points extra credit)

3.  [40pts] Consider the initial value problem (predator-prey model)

 v(u)  =  ;       =  1(1)0(0) :                                 (5)

Simulate the solution of (5) in the time interval [0; 100] using N = 1000 constant time steps of length t = h = (100    0)/N . The explicit Euler’s method defines numerical approximations at tn+1  = t0 + (n + 1)h by

      = v(u)n(n)   + h  ; 0  n < N;    with      v(u)0(0)   =  1(1)0(0) :

Create a Python script p 3  .  p y   that

(a) implements the explicit Euler’s method,

(b) implements the improved Euler/Runge–Kutta 2/Heun’s method,

(c) implements the Runge–Kutta 4 method,

(d)  and that compares the results from (a), (b), and (c) graphically in time (t vs population plot) and in phase-space (u vs v plot).

(e) Which approximation should be trusted, if any, or what could be done to improve con- fidence in the approximations?  What happens with the Euler approximation as N is increased to 10; 000?

(f)  If u(t) represents the population of insects and v(t) represents the population of car- nivorous frogs, discuss what the three approximations of the predator and prey model predict for the time interval.

Include in t e s t 2 _ L A S T N A M E  .  t a r  .  g z    the Python program p 3  .  p y   and any output files.

4.  [40 pts] The Legendre polynomial LN  is defined recursively by

L0 (x)    =   1

L1 (x)    =   x

LN (x)   =   xLN   1 (x)  LN   2 (x)   (N > 1)

(a)  Determine the polynomial L6  explicitly by hand.

(b)  Explain how you would determine the zeros of L6  close to x =  (set up the iteration).

(c)  Explain how you would determine all the zeros of L6  at once via an eigenvalue problem (derive and set up the matrix, +5 points extra credit if made symmetric).

(d)  Determine and schematicize the appropriate function call from LAPACK that solves the eigenvalue problem from c).

(e)  Implement (d) with a fortran program p b 4  .  f 9 0    (+5 points extra credit) or with Python. Include in t e s t 2 _ L A S T N A M E  .  t a r  .  g z    your fortran or python program ( p 4  .  f 9 0      or  p 4  .  p y   ) and any revelant output files. If submitting a fortran solution, be sure to include a  M a k e f i l e    .