官术网_书友最值得收藏!

24. Gaussian elimination

Bisection and Newton's method let you find solutions to an equation of the form F(x) = 0. Gaussian elimination lets you find the solution to a system of linear equations of the following form:

For example, consider the following equations where n is 2:

The solution to these equations includes the values for x1 and x2 that make both of the equations true. In this example, you can plug in the values x1 = -1 and x2 = 4 to verify that those values form a solution.

This problem asks you to use Gaussian elimination to solve systems of equations. Unfortunately, you need to know a fair amount of background to use Gaussian elimination.

To perform Gaussian elimination, you first represent the equations as a matrix multiplied by a vector of variables x1, x2, ... , xn giving a vector of constants C1, C2, ... , Cn, as shown in the following matrix equation:

Next, you convert this into an augmented matrix that holds the original coefficients plus two extra columns, one to hold the C values and one to hold the final solution. Initially, that final column should be initialized to all zeros, as shown in the following matrix:

Now you perform row operations to reduce this augmented matrix into an upper triangular form.

In a row operation, you can add multiples of one row to another row. For example, if you add –A2/A1 times the first row to the second row, then the new second row has a 0 in its first position. That row's other positions will depend on the other values in the two rows. For example, the entry in its second column will be B2 - A2/A1 × B1.

The important thing about row operations is that they do not change the truth of the system of equations. If a set of values (x1, x2, ..., xn) solves the original equations, then those values also solve the equations after you perform row operations.

Start by using row operations on the first row to zero out the first entries in all rows after row zero. Next, use row operations with the second row to zero out the entries in the second column below that row. Continue using the kth row to zero out the entries in column k in the rows that follow until the augmented matrix has an upper triangular form, like the following:

You may run into a problem when you try to put the augmented matrix into upper triangular form. When you're considering row k, you may find that it has a 0 in column k. In that case, you cannot use that row to zero out column k in the following rows because that would require you to divide by zero.

In that case, simply swap row k with a later row that does not have a 0 in column k. Then you can continue converting the matrix into upper triangular form. If you ever find that all of the remaining rows have 0 in column k, then there is no unique solution to the system of equations.

After the augmented matrix is in upper triangular form, you are ready to pull out the solution. At this point, the augmented array represents the following system equations:

Now, you can easily solve the last equation and get xn = C'n / N'n. You can save that value in the final column of the augmented matrix's last row. Then, you can plug the value for xn into the second-to-last equation to find xn-1 and save the result in the second-to-last row's final column.

Next, you plug xn and xn-1 into the third-to-last equation to find xn-2.

You continue using the values that you have found so far to find new values farther up in the augmented matrix until you have found all of the values and saved them in the matrix's final column. This process is called backsolving.

Now that you understand how Gaussian elimination works, here's your problem. Write a program similar to the one shown in the following screenshot that allows the user to enter the coefficients for a set of linear equations and then solves them:

Use the program to solve the following systems of equations:

主站蜘蛛池模板: 改则县| 抚松县| 射洪县| 廉江市| 呼伦贝尔市| 罗田县| 甘谷县| 新巴尔虎右旗| 元谋县| 当雄县| 哈密市| 武清区| 观塘区| 台北市| 阜新| 尉犁县| 高尔夫| 云浮市| 信阳市| 陈巴尔虎旗| 黎川县| 邮箱| 普洱| 年辖:市辖区| 罗平县| 呼图壁县| 广东省| 桂平市| 镇宁| 杭锦后旗| 德阳市| 岳西县| 甘泉县| 兰西县| 锡林浩特市| 镇雄县| 苍溪县| 九龙坡区| 蕲春县| 乌兰县| 清水县|