Computer Project 2 The purpose of this project is to get some practice setting up and solving large systems of equations using a computer. I will give an outline of how this is to be done using MATLAB, but you are free to adapt the problems to the programming language or math program of your choice. 1. Find a sequence of numbers y1 , y2 , . . . , yN so that yk = 2yk+1 for k = 1, 2, . . . , N − 1 and yN = 1. Generate a plot of yk versus k for N = 50. First of all, we could solve this by hand if we wanted to, but it would take a while. There are N − 1 unknowns, and we know that yN = 1. Note that the kth equation says yk − 2yk+1 = 0 for k = 1, 2, . . . , N − 2, but in the case of yN −1 , we know that yN −1+1 = yN = 1, so the N − 1st equation is yN −1 = 2. For the moment, let’s ignore the last equation, which is special, as noted above. For the most part, the kth equation involves the kth variable and the k + 1st variable. The kth row in the coefficient matrix for the system looks like 0 · · · 0 1 −2 0 · · · 0 , where the one is in the kth column. The corresponding entry of the right-hand side vector ~b is zero. The last equation says yN −1 = 2, so the last row of the coefficient matrix is 0 ··· 0 1 , and the entry of ~b for this row is 2. Therefore, the system 1 −2 0 · · · 0 0 0 1 −2 0 · · · 0 .. .. . . . .. 0 0 ··· 0 1 −2 0 ··· 0 0 0 1 of equations in matrix form looks like 0 y1 y2 0 = .. . .. . . 2 yN −1 Therefore, we can find the desired sequence by solving the system of equations above. The coefficient matrix has a relatively simple form; it has ones on the main diagonal and -2 on the diagonal above the main diagonal. All we need to do is tell MATLAB how to make the matrix and the right-hand side vector, and then have it solve the system of equations. Here’s how to proceed: • Open MATLAB and from the file menu select new M-file. This will open a new window within the MATLAB text editor. Save the file with the file name that you desire and a .m extension (e.g. prob1.m). Now give the function contained in the file the same name by typing “function prob1”. • In the next line, set the value for N . N=50; The semicolon tells MATLAB to suppress the output. If you want to see the output of a computation, then don’t end the line with a semicolon. • The next thing that we will need is a matrix B that has the nonzero elements of the coefficient matrix A as its elements. The matrix B should have as its first column the elements on the first nonzero diagonal in A counted from the bottom left corner. The second column should be elements of the next nonzero diagonal, and so forth. In MATLAB, the main diagonal of a matrix is numbered 0, and the diagonals above the main diagonal are numbered 1, 2, . . .. The diagonals below the main diagonal are numbered −1, −2, . . .. In this case, we have 1 B=[ones(N-1,1) -2*ones(N-1,1)]; where the command ones(m,n) generates a matrix of size m × n, all of whose entries are 1. Thus, the first column of B is all ones, and the second is all twos. • We use the function spdiags to construct A from B. The function name is short for “sparse diagonals”. Sparse means that MATLAB is expecting a matrix that has a lot of zero entries, so it will only store the nonzero entries and their locations in order to save space. The command is A=spdiags(B,[0 1],N-1,N-1); The first two arguments tell MATLAB to put the first column of B on the 0th diagonal of A (the main diagonal), and the second column of B on the 1st diagonal. The last two arguments say that the resulting matrix should be N − 1 × N − 1. (For more information, type “doc spdiags” into the MATLAB command line. • Next, we form the right-hand side vector ~b. To do this, we first initialize all of the elements of ~b to zero with the command b=zeros(N-1,1); Then we set the last entry of ~b to 2 with the command b(N-1)=2; • Now that we have constructed A and ~b, we can solve the system of equations A~y = ~b. To do this, we use the command y=A\b; • We have now solved for the values of y1 , y2 , . . . , yN −1 , and we already know yN , so we append it to our solution vector y=[y;1] Think of this as saying that the new ~y is the old ~y stacked on top of 1. Notice that I omitted the semicolon here so that my final version of the vector ~y would be printed out on the MATLAB command window. • In order to see a plot of our vector, we put in the command plot(y) • Now, we save the m-file and return to the MATLAB command window. Make sure that the directory in which you saved the m-file that you just created is the current directory for MATLAB. Then simply type the name of the m-file (minus the .m extension). The next two problems can be solved by making modifications to the code that you generated for problem 1. 2. Find a sequence of numbers y1 , y2 , . . . , yN so that yk = yk−1 + yk+1 2 for k = 2, 3, . . . , N − 1 satisfying y1 = 5 and yN = 10. Generate a plot of yk versus k for N = 50. 3. Find a sequence of numbers y1 , y2 , . . . , yN so that yk = yk−2 + yk+2 2 for k = 3, 4, . . . , N − 2 satisfying y1 = 10, y2 = 2, yN −1 = 1 and yN = 5. Generate a plot of yk versus k for N = 50. For each problem, print out the code that you used, a list of the elements of the solution ~y, and a plot of its elements. 2