• Aucun résultat trouvé

Homework Problems

Dans le document in C++ and MPI (Page 99-103)

Example of Usage

2.4 Homework Problems

1. Prove that the condition number in the L2-norm of an othogonal matrix is 1, and that the condition number (in any norm) of a matrix is greater or equal one.

2. Use the classical Gram-Schmidt and the modified Gram-Schmidt algorithms to or-thonormalize the vectors

Compare the two results. What do you observe?

3. Find the eigenvalues of an n×n matrix with all entries equal to 1.

4. Modify Collatz-B so that instead of using the expression xn+1 = 3xn + 1 you use xn+1 = 5xn + 1. Use the number 100 as an initial guess and with the maximum number of iterations set to 10,000. What do you observe? Postulate as to why this happens.

(Hint: Have you ever played an arcade game in which you eventually were scoring a negative number?)

5. The Fibonacci sequence is a sequence of integers created by the following inductive process: givenf0 = 0 andf1 = 1, the next number in the sequence is equal to the sum of the previous two numbers, i.e.,

fn=fn1+fn2.

This process can be continued indefinitely and produces the Fibonacci sequence: 0,1,1,2,3,5,8,13 (a) Write a program which allows the user to input a desired number of terms N of

the Fibonacci sequence, and outputs the sequence f0, f1, . . . , fN to the screen.

(b) Modify your program so that you keep track of the ratio ffn

n−1. How many terms do you need so that the difference between ffn

n−1 and ffn−1

n−2 is less than 1.0×105? (c) What is the value of this ratio? The number you have converged to is called the

Golden Mean.

6. The harmonic seriesk=11/kdiverges, i.e., it grows without bound as we include more terms. Consider the truncation that includes the first n terms, which we call partial sum Sn and can be computed recursively fromSn=Sn1+ 1/n with S1 = 1. What is the largest Sn that can be obtained in your computer in single precision?

7. The Pythagorean theorem states that the sum of the squares of the sides of a right triangle is equal to the square of the hypotenuse. Thus, ifxandyare the lengths of the two sides of a right triangle, and z is the length of the hypotenuse, then x2 +y2=z2. Fermat, a lawyer and amateur mathematician during the 1600s, postulated that there

exists no other integer m (other than m= 2) such that xm +ym=zm for the sides of a right triangle as described above.

(a) Write a function power which takes as input a double precision number x and an integer m and returns as output a double precision number which is equal to the value of x raised to the power m (i.e., xm).

(b) Write a function pythagoreus which takes as input two double precision numbers x andy and an integerm and returns as output a double precision number which is equal to the value of xm+ym. Use the function power written above.

(c) Write a program which queries the user for three values: two double precision number which equal the length of two of the sides of a right triangle, and an integer N. Your program should first use the function pythagoreus to determine the value of the square of the hypotenuse. Then you should write a loop which checks to see if there exists any integer 2 < m≤ N such that zm =xm+ym. If you find such a value of m, print the result and break out of the loop. If you do not find a value m such that the above expression is true, print a message which states that no value can be found for the value N provided by the user.

8. Change the stride of the summing example given in section 2.3 (make the stride of the additions equal to the number of processors). This will require devising new formulae for the variables startval and endval, and changing the i = i+ 1 used within the summing loop to some other increment.

9. Modify the summing example as follows:

(a) At the beginning of the main function, add an integer variablemaster, and initial-ize this value to some number between zero and the number of processors minus one.

(b) Modify theM P I Send/M P I Recvsequence such that all processes exceptmaster send, and process masterreceives.

(Hint: From the example, in theM P I Send, the ‘0’, denotes that you are sending to process zero; in theM P I Recv, thej denotes the process from which a message is being received. These will need to be modified.)

(c) Output the sum from master.

(d) Add cout statements so that each sending process prints a message stating to whom it is sending, and add cout statements so that the receiving process ac-knowledges from whom it has received.

10. Modify the summing example as follows:

(a) Instead of summing integers, change the appropriate variables so that you will now sum doubles. You will need to use M P I DOU BLE instead of M P I IN T within the MPI calls. Verify that you obtain the same answer as the integer case.

(b) Change the sum so that you are summing 1i instead of i.

(c) At the top of the program, immediately following the #include<iostream.h>

statement, add #include<iomanip.h>. Then prior to calling the cout statement, add the following line:

cout << setprecision(20);

After making these changes and recompiling, run your program on 2,4, and 8 processes. What differences in the sum of 1i do you see? Postulate as to why this is so.

11. Modify the parallel MPI code to do the following:

(a) Have process zero query the user for the number of elements over which to sum.

(b) From process zero, distribute to all processes the number of elements to sum (using sends and receives) and appropriately calculate the interval over which each process is to sum.

(c) Accomplish the summing as is already done in the program.

(d) After creating the final answer on process zero, print the result.

Chapter 3

Approximation

Two of the most common tasks in scientific computing are interpolation of discrete data and approximation by known functions of the numerical solution, the source terms, and the boundary or initial conditions. Therefore, we need to perform these tasks both accurately and efficiently. The data are not always nicely distributed on a uniform lattice or grid, and thus we must learn how to manage these situations as well. We often use polynomials to represent discrete data as they are easy to “manipulate,” i.e., differentiate and integrate. However, sines and cosines as well as special functions, called wavelets, are very effective means to perform interpolation and approximation, and they have very interesting properties.

In this section, we will study different such representations and their corresponding C++

implementations. We consider cases where the data are just sufficient to determine exactly the representation (deterministic case) as well as cases where the data are more than the information needed (overdetermined case).

Finally, we will present a more detailed discussion of M P I Send and M P I Recv, the two fundamental building blocks of MPI.

94

Dans le document in C++ and MPI (Page 99-103)