Question

In: Advanced Math

Make the program(such as matlab) for solving the linear system Ax = b using the Naive-Gaussian...

Make the program(such as matlab) for solving the linear system
Ax = b
using the Naive-Gaussian elimination & also the Gaussian elimination with partial pivoting.

Solutions

Expert Solution

Main program files :

#############################################################################################

function[x]=solve_gaussElimin(A,b)

n=length(b);% number of elements in b vector

% We start LU decomposition
for k = 1:n-1 % At every stage (column)
  
% Compute the corresponding multipliers of L
% and store it in A(To save the memory)
J=k+1:n;
A(J,k) = A(J,k) / A(k,k);
  
% update submatrix by outer product
A(J,J) = A(J,J) - A(J,k) * A(k,J);
end
% Here we get the factorisation PA=LU.
L=tril(A,-1)+eye(n);
U=triu(A);

y=forward_sub(L,b);

x=backward_sub(U,y);

################################################################################

function [x]=solve_Gauss_partial_pivot(A,b)

% A = [1 1 1 1 1;
% -2 -1 0 1 2;
% 4 1 0 1 4;
% -8 -1 0 1 8;
% 16 1 0 1 16]; % The A matrix
%   
% b = [2 0 8/3 0 32/5]'; %b vector

[P,L,U] =Partial_pivoting(A);

% Ax=b => PAx=Pb => LUx=Pb. This can be solved in 2 steps

% Step 1 : solve y for Ly=Pb
% Step 2 : solve x for Ux=y

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Step 1

% permute b according to P
b1=P*b;
y=forward_sub(L,b1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Step 2

x=backward_sub(U,y);

##########################################################################################

Necessary files :

##########################################################################################

function[x]=forward_sub(L,b)
% forward substitution
n=size(L,1);
x=zeros(n,1);

x(1) = b(1)/L(1,1);
for k = 2:n
x(k) = (b(k) - L(k,1:k-1) * x(1:k-1))/ L(k,k);
end

###########################################################################################

function[x]=backward_sub(U,b)
% forward substitution
n=size(U,1);

x=zeros(n,1); % Initiating
% backward substitution
x(n) = b(n) / U(n,n);
for k = n-1:-1:1
x(k) = ( b(k) - U(k,k+1:n)*x(k+1:n) ) / U(k,k);
end

############################################################################################

function [P,L,U] =Partial_pivoting(A)

n = size(A,1);

% Order in which the Identity matrix is at the begining.
p = 1:n;
P=eye(n);
% We start LU decomposition with partial pivoting
for k = 1:n-1 % At every stage (column)
  
% find the row index such that it has the absolute maximum in that column
[val,q] = max(abs(A(k:n,k)));
q = q + k-1;
  
% swap the rows "k" and "q" and do the same permutation in "p"
A([k,q],:)=A([q,k],:);
p([k,q])=p([q,k]);
  
% Compute the corresponding multipliers of L and store it in A(To save the memory)
J=k+1:n;
A(J,k) = A(J,k) / A(k,k);
  
% update submatrix by outer product
A(J,J) = A(J,J) - A(J,k) * A(k,J);
end
% Here we get the factorisation PA=LU.
L=tril(A,-1)+eye(n);
U=triu(A);
P((1:n),:)=P(p,:);
end


Related Solutions

2. (Solving linear systems) Consider the linear system Ax = b with A =[14 9 14...
2. (Solving linear systems) Consider the linear system Ax = b with A =[14 9 14 6 -10;-11 -11 5 8 6;15 -2 -14 8 -15;14 13 11 -3 -7;0 9 13 5 -14], and . b = [-4;8;6;0;10]. a) Verify that the linear system has a unique solution. Hint: use rref, rank, det, or any other Matlab method. Briefly explain the answer please. You'll now solve Ax = b in three different ways. Store the three different solutions in...
Exercise 3 Part 1. Solving a system Ax = b **Create a function in MATLAB that...
Exercise 3 Part 1. Solving a system Ax = b **Create a function in MATLAB that begins with: function [C,N]=solvesys(A) [~,n]=size(A); b=fix(10*rand(n,1)) format long We are using format long to display a number in exponent format with 15 digit mantissas. If later on, you will need to switch back to the default format, type and run format The input is an matrix A. If A is invertible, the outputs are the matrix C, whose 3 columns x1, x2, x3 are...
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...
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...
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...
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...
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 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 !
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
Write a function in Python that solves the linear system ??=? using Gaussian Elimination, taking ?,?...
Write a function in Python that solves the linear system ??=? using Gaussian Elimination, taking ?,? as input. The function should have two phases: the elimination phase, and the back substitution phase. You can use numpy library.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT