Due by 8:00 AM on Monday, November 8, 1999 Clarifications: This assignment is somewhat vague about the format for printing output, so the graders will accept a range of output formats. However, be sure to display your output in a way that is easy for the graders to read. In particular, make your numbers line up in columns and limit precision to relatively few (e.g. 2 or 3) digits after the decimal point. In this assignment you will write a program called Gauss which solves linear equations using a technique called Gaussian Elimination. This program will consist of a series of methods, each doing part of the task. Don't worry, it's not nearly as hard as it sounds! There are just a few simple steps. To help you understand, let's look at the process in terms of an example. Consider the following set of three linear equations in three unknowns (the variables x, y, and z):
Our goal is to do a series of operations such that the coefficients along the diagonal all end up 1, and all the other coefficients end up 0. When we have reached this configuration, we will be able to read off the values of the variables directly. Obviously, the operations we perform must be such that they do not disturb the meaning of the equations. Throughout this example, we'll always show all the variables and coefficients, even when the coefficient is 0 or 1, just to make things clear. Also, in each step we'll draw a pointer in front of each equation that has changed since the previous step.
The first step is to divide the first equation by 2, which is the coefficient of x in that equation. That will make the coefficient of x in the first equation 1:
Now, notice that if we subtract twice the new first equation from the second equation, the coefficient of x in the second equation will become 0. Similarly for the third equation, if we use thrice the new first equation. So that's what we do:
Next we repeat the process, dividing the second equation by 3 so that its y coefficient becomes 1:
Then we subtract twice the second equation from the first, and minus-one of it from the third. This will get the coefficient of y to 0 in the first and third equations (notice that since we have zeroed out the coefficients of x in all but the first equation, we won't mess up the x coefficients by doing this):
Finally, we divide the third equation by -3.83 to get its z coefficient to 1, and subtract 3.16 and -0.33 of the result from the first and second equations, respectively, to zero their z coefficients:
Thus, the solution of these equations is:
Now, the thing to notice is that the variables play no role in this process; only the coefficients matter. If we take the variables out, all we are doing to solve a system of 3 equations in 3 unknowns is manipulating a 3 by 4 array of coefficients.
Here is a description of the methods you need to write to implement the linear equation solver.
All the methods operate on
The first method takes no parameters, and returns the coefficient array as input by
the user.
All of the remaining methods take a coefficient array and manipulate its contents in place.
Therefore, they have return type
Use these methods to write a program that will input coefficients from the user, solve the corresponding system of equations, and print the solution array. NOTE: In general Gaussian Elimination can be tricky as the input may not be well behaved. In particular, you may find that you don't actually have n distinct equations because one may be a linear combination of some of the others. If this happens there is not enough information to solve the system completely, and Gaussian elimination will lead one of the rows of the array to be all zeroes. This in turn will likely lead to a division by zero error. You need not worry about this and related problems. Just implement the naive algorithm; we will only test it on well behaved input. If you are interested in the more general solution, see me and I can point you at some references.
|
Last modified August 28 for Fall 99 cs5 by fleck@cs.hmc.edu
This page copyright ©1998 by Joshua S. Hodas. It was built with Frontier on a Macintosh . Last rebuilt on Wed, Oct 21, 1998 at 1:31:16 AM. | |
http://www.cs.hmc.edu/~hodas/courses/cs5/week_10/homework.html | |