Question

In: Computer Science

Write a function to solve a system of linear equations of the form Ax= b using...

Write a function to solve a system of linear equations of the form Ax= b using the iterative Gauss-Seidel method. You are free to use any basic MATLAB operation to implement the algorithm (i.e. you may use any combination of loops, indexing, math, etc.), but avoid “built-in” solution methods — you would not be allowed to use the GaussSeidel function if such a function existed. The function must also test for a number of possible issues. If an issue is encountered, you should use the error() command to issue a reasonable error message. Use distinct error messages for each of the following cases: • If the dimensions of the input matrices do not conform with each other • If the matrix of coefficients is not square

Solutions

Expert Solution

We need to solve a system of linear equations represented as

The general Gauss-Seidel algorithm for this system of equation is written as,

where for k =0, vector is known. a is the element of matrix A. bi is the element of vector b. i represents the row and j represents the column.

MATLAB code.

clc;clear; close all;

%% Take the input
A = input('Enter the matrix of coefficients, A: ')
% Check for error
[row_A, column_A] = size(A); 
if row_A ~= column_A
    error('Matrix of coeffecient is not square.')
end

b = input('Enter the column vector, b: ')
% Check for error
[row_b, column_b] = size(b)
if column_A ~=size(b)
    error('Dimension of input matrices do not confirm with each other.')
end

x0 = input('Enter the initial guess vector, x0 :')
nItr = input('Enter the max no. of iterations :')

%% Gauss-Seidel iterations
for k=1:nItr % k=>iteration
    for i=1:length(b) % i=>unknown
        x(i,1) = (b(i)/A(i,i)) - (A(i,[1:i-1, i+1:length(b)])*x0([1:i-1, i+1:length(b)]))/...
            A(i,i);
        x0(i,1) = x(i,1); % update to use for next unknown(xi)
    end
    fprintf('Iteration no. %d\n', k)
    x
end

Related Solutions

Solve the linear system of equations Ax = b, and give the rank of the matrix...
Solve the linear system of equations Ax = b, and give the rank of the matrix A, where A = 1 1 1 -1 0 1 0 2 1 0 1 1 b = 1 2 3
write a Matlab function file to solve system Ax=b by using the output of the function...
write a Matlab function file to solve system Ax=b by using the output of the function lufac2a your function should have inputs f=matrix return from lufac2a, piv=array return by lufac2a and b=right hand side of your system.the only output for your system should be x guideline 1.use the column access for the matrix entries 2. do not create any other matrix in your function-get your data directly from the matrix passed into your function 3.do not use Matlab command designed...
A linear system of equations Ax=b is known, where A is a matrix of m by...
A linear system of equations Ax=b is known, where A is a matrix of m by n size, and the column vectors of A are linearly independent of each other. Please answer the following questions based on this assumption, please explain all questions, thank you~. (1) Please explain why this system has at most one solution. (2) To give an example, Ax=b is no solution. (3) According to the previous question, what kind of inference can be made to the...
Write a MATLAB function function = pivGauss(.....) to solve linear equations using Gaussian Elimination with Partial...
Write a MATLAB function function = pivGauss(.....) to solve linear equations using Gaussian Elimination with Partial Pivoting. You'll need to employ Nested Loops. Thank you !
Write your own routine to solve a system of n linear equations and n unknowns using...
Write your own routine to solve a system of n linear equations and n unknowns using the LU decomposition. Input should take an augmented matrix and the number of equations n. Output should be the augmented matrix. L and U matrices and the solution. Also compare your results to those of Matlab’s built in LU decomposition. Use your code to solve the systems given as (a) and (b) below: a) 3a − 2b + c = −3, a − 4b...
Write a python program that can solve system of linear equations in three variables using input...
Write a python program that can solve system of linear equations in three variables using input function. Paste your program in a word document or notepad. Note that I am using pycharm. please use a not really complex codes, thanks
Write a system of equations that represents the situation. Then, solve the system using the inverse...
Write a system of equations that represents the situation. Then, solve the system using the inverse of a matrix. Students were asked to bring their favorite fruit to class. 93% of the fruits consisted of bananas, apples, and oranges. If oranges were twice as popular as bananas, and apples were 19 percentage points less popular than bananas, what are the percentages of each individual fruit?
in parts a and b use gaussian elimination to solve the system of linear equations. show...
in parts a and b use gaussian elimination to solve the system of linear equations. show all algebraic steps. a. x1 + x2 + x3 = 2 x1 - x3 = -2 2x2 + x3 = -1 b. x1 + x2 + x3 = 3 3x1 + 4x2 + 2x3 = 4 4x1 + 5x2 + 3x3 = 7 2x1 + 3x2 + x3 = 1
If an SPL ( LINEAR EQUATION SYSTEM ) is known: Ax = b. A is a...
If an SPL ( LINEAR EQUATION SYSTEM ) is known: Ax = b. A is a matrix sized m × n and b is a vector sized m × 1, with the component values of matrix A and vector b known. The x vector is n × 1 and the component values are unknown. Explain how the possible solution of SPL Ax = b. i want answer for the question , and what he mean by (the component values of...
1) Solve the system of linear equations, using the Gauss-Jordan elimination method. (If there is no...
1) Solve the system of linear equations, using the Gauss-Jordan elimination method. (If there is no solution, enter NO SOLUTION. If there are infinitely many solutions, express your answer in terms of the parameters t and/or s.) 3y + 2z = 1 2x − y − 3z = 4 2x + 2y − z = 5 (x, y, z) = 2) Solve the system of linear equations, using the Gauss-Jordan elimination method. (If there is no solution, enter NO SOLUTION....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT