Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here are some more information Write a simple function Gauss_Elimination () using matlab to implement the nave Gauss elimination for solving AX = b, where

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Here are some more information

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Write a simple function Gauss_Elimination () using matlab to implement the nave Gauss elimination for solving AX = b, where A is an N * N matrix, X is an N * 1 column vector, and b is an N * 1 column vector. The function should have the following format function solution_vec = Gauss_Elimination (A_mat, b_vec) where A_mat is the coefficient matrix A, b_vec is the right-hand side vector b, and the return solution_vec is the solution to vector X. Your function should be able to take systems of any size (i.e., different values of N). It should check that the matrix A_mat is square, and that the constant vector b_vec has the appropriate number of elements, printing an error message and returning the zero vector if these solvability conditions are violated. Your program should also print an error message and return the zero vector if it runs into a zero pivot. In general, zero pivots may not be obvious, given the initial coefficient matrix. If au = 0, that is obvious; but the process of the first round of the forward elimination process may set a 22 to zero when it was non-zero before, and so on. The potential pivot element must be checked as the first step for each round of the forward elimination (the Forward_Elimination () function described below). Your Gaussian_Elimination () function should use secondary functions called by your main function as a structuring mechanism. A good place to start is to note that after the initial parameter checking, the process consists of two main steps. First, the forward elimination process that reduces the coefficient matrix to upper triangular form (modifying the constant vector in parallel). Second, the back-substitution that obtains the solution vector from the upper triangular matrix and the (modified) constant vector. We can write a secondary function to perform each step. Our main function thus starts out looking like this: function solution_vec = Gauss_Elimination (A_mat, b_vec) & check for consistent size of A_mat and b_vec = % reduce coefficient matrix to upper triangular form, modifying the % right-hand side vector appropriatly [ut_mat, new_b_vec] Forward_Elimination (A_mat, b_vec); & Compute the solution vector using back substitution solution_vec = Back_Substitution (ut_mat, new_b_vec); % we are done end Forward elimination The Forward_Elimination () function uses its own subsidiary functions. Specifically, you should write a function called reduce_column() with prototype function [new_mat, new_b_vec] = reduce_column (A_mat, b_vec, column) that returns a modified coefficient matrix (and right-hand side vector) in which the input matrix has been reduced so that all the elements below the (col, col) diagonal element are zero. Initially it checks to see the (col, col) element is NOT zero, of course! This can be used iteratively to modify the original matrix and constant vector to produce an upper triangular form. Use the following format when calling your reduce_column() function so that you do not have to define too many new variables: [cur_mat, cur_vec] = reduce_column (cur_mat, cur_vec, cur_col); The reduce_column() function might itself call yet another function to reduce a specified column in a row to 0 by adding a multiple of another row, returning again, both a modified coefficient matrix and a modified constant vector. The prototype would look like function [new_mat, new_b_vec] = reduce_row_at_col (A_mat, b_vec, col, row_added, row_reduced); This function adds a multiple of row_added to row_reduced so that the specified col in row_reduced is 0. The same operation is carried out on the corresponding position of the right- hand side vector. Back substitution The Back_Substitution () function can similarly be constructed using a subsidiary function that takes the upper triangular matrix, the (modified) constant vector, and a partially-filled-in- from-the-end solution vector, (entries from col+1 to the end already known) and produces a modified partial solution vector with entry col now filled in. The prototype would look like function new_part_solution_vec = back_subst_for_col (ut_mat, new_b_vec,... column, part_solution_vec) Partial Pivoting Write a second version of your function called Gauss_Elimination_Pivoting () that uses partial pivoting. Write a secondary function that identifies and swaps in the correct pivot row for a specified working column (modifying a matrix and constant vector) to help you do this. The prototype would be something like function [new_mat, new_b_vec] = pivot (A_mat, b_vec, col); Write a function to swap a specified pair of rows (modifying both the coefficient matrix and constant vector) with prototype function [new_mat, new_vec] = swap_rows (mat, vec, rowl, row2); 3 Gauss Elimination Example: (11 11 + 01222 = bi 02121 + 02222 = b2 (5) (6) (5) X 021 21122121 + a122216'2 = b021 (7) (6) Xa11 = Q1142141 +21102222 = b2011 (8) (7)-(8) (a12021 411022).X2 = b[@21 b2011 - (9) - 22 b1a21 b2011 012021 - 011022 (10) Substituting back to (7), b1022 b2012 T1 (11) (12021 - 011022 Gauss elimination steps: Forward elimination - n unknowns: n - 1 rounds of elimination The first round is to eliminate x1 from equations (2) to (n) The second round is to eliminate 22 from equations (3) to (n) The (n 1)th round is to eliminate In-1 from equation (n) Back substitution First find In from the nth equation then find &n1 from the (n 1)th equation then find 22 from equation (2) finally find x from equation (1) Forward elimination Original set of equations: 01111+ 412X2+ (13.23+ ... + 21,n-12n-1+ Alnxn Qinlin 22101+ 22222+ 2323+ ... + 42,11.In-1+ a2nIn = + bi (1) b2 (2) . (12) 011 di; = dij - (1) (1.11 (111 ail an1.11+ An222+ An3.13+ ... + An,n12n-1+ Anniken = 1+ + bn (n) The first round of elimination: (i) (1) Gill, where (i) is from (2) to (n). Then x the new equation (i) becomes 412X2 + 01323 + ... + di-m-12n1 + dimin 2.02 2n where Q1; ba = b; b x for i = 2,3,...,n, j = 2,3,..., n. n Pivot element: 211. The full set of new equations after the first round of elimination is 21121+ 41222+ A1323+ ... + a1.n-1.xn-1+ ainilen b (1) 22222+ 02313+ ... + 02,n14n-1+ aanilin 62 (2) (13) 0m2:12+ am3:23+ ... + dn.n12n1+ annen = 6% (n') (111 2 - 10 Pivot eq. Round Eliminate From equations 1st X 1 (2)-(n) 2nd X2 (3)-(n) 3rd X3 |(42))-(n) (1) (2) (3(-)) kth XK ((k+1)(k-1)-(n(k-1)) (k(k-1)). (n- (n-1)th xn-1 (n(1-2) |((n-1) (1-2)) dik In general, the kth round of elimination eliminates xk from the (k+1)th equation to the nth equation. That is, (k-1) (i(k-1)) (k:(k-1)) (k-1) Pivot equation where (i) is from (k + 1) to (n). Then the new equation (i) (or equation (j(k))) becomes (k) (k) a +1%k+1 + ... + a_12n1 + amin = 5), where (k-1) (k-1) (k-1) 9 (k) (k-1) dik j Wij - ORM ack (k-1) -1) 6.4") = 541) B.22 6.4 dik (k-1) Dr X (k-1) akk . for i = k +1, k + 2, ..., n, j = k +1, k +2, ..., n. (k-1) Pivot element: akk. After the (n 1)th round of elimination: 01121+ 212.22+ (13.23+ ... + 01,n-12n-1+ ainen 0.2212+ 023.23+ .+ 12,1-1.xn-17 02n1n bi b. (1) (2) (14) (n-1) ann In (n-1) bir (n(n-1)) Back substitution From equation (n(n-1)), we have an (n-1) by (n-1) ann From equation ((n 1)(n2)), we have lin-1 6(n-2-a (n-2) ) bm-1-an-1,nen (n-2) an-1,6-1 In general, (i-1) Ci= 1 (i-1) - [ (i-1) 0,i+1 Xi+1 (i-1) (i-1) an-1.3n-1 ain In ai for i = n 1, n 2, ..., 1. Comment: Most operations are for eliminations. As n increases, computational load increases

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What are Measures in OLAP Cubes?

Answered: 1 week ago

Question

How do OLAP Databases provide for Drilling Down into data?

Answered: 1 week ago

Question

How are OLAP Cubes different from Production Relational Databases?

Answered: 1 week ago