Question

In: Computer Science

Implement Gaussian elimination(with partial pivoting) and backward substitu- tion in MATLAB. You need to submit your...

Implement Gaussian elimination(with partial pivoting) and backward substitu- tion in MATLAB. You need to submit your code on moodle page.

Solutions

Expert Solution

MATLAB code for the Gaussian elimination with partial pivoting and backward substitution to find the roots are provided below, please comment if any doubts:

Code:

%set the value of A anf b
A = [1 1 -2; 2 -3 1; 3 1 4]
B = [1; -8; 7]

%%Call the function
Guass_partial_back(A,B)


%%The function to peform the guassian eliminatio
%by partial pivoting and back substitution
function [myroots,nrow,AugmentedMat]= Guass_partial_back(A,b);
   %create the augmented matrix
   augMat=[A b];
  
   %find the rank of the matrx
   ra=rank(A);
  
   %create a row vector of size rank
   for itr=1:ra
       nrow(itr)=itr;
   end
  
   %fibnd the transverse
   nrow=nrow';
   for i=1:ra-1;
   largest=0;
   numIndx=0;
   %find maximum in a row and return it
   for k=i:ra
       if abs(augMat(nrow(k),i))>largest
           largest=abs(augMat(nrow(k),i));
           numIndx=k;
       end
   end
  
   %row exchange procedure
   if nrow(i)~=nrow(numIndx)
       tempRow=nrow(i);
       nrow(i)=nrow(numIndx);
       nrow(numIndx)=tempRow;
   end
  
   %perform the Gaussian elimination
   for itr=(i+1):ra
       n(nrow(itr),i)=augMat(nrow(itr),i)/augMat(nrow(i),i);
     
       for k=i:ra+1
           augMat(nrow(itr),k)=augMat(nrow(itr),k)-n(nrow(itr),i)*augMat(nrow(i),k);
       end
          
   end
   end
  
   %the backward subsitution to find the roots
   x(ra)=0;
   x=x';
   x(ra)=augMat(nrow(ra),ra+1)/augMat(nrow(ra),ra);
   itr=ra-1;
  
   %find all x values
   while itr>0
       x(itr)=(augMat(nrow(itr),ra+1)-augMat(nrow(itr),itr+1:ra)*x(itr+1:ra))/(augMat(nrow(itr),itr));
   itr=itr-1;
   end
  
   %return the solution
   myroots=x;
  
   %augmented matrix
   AugmentedMat=augMat;

end

Output:


Related Solutions

Can you explain in detail what Gaussian Elimination with pivoting is? and how is it different...
Can you explain in detail what Gaussian Elimination with pivoting is? and how is it different from Gaussian Elimination without pivoting?
MATLAB script Gaussian Elimination without pivoting, not working no matter what I try. So far my...
MATLAB script Gaussian Elimination without pivoting, not working no matter what I try. So far my script is : function [x] = myGauss( A, b ) n = size(A,1); % getting n Ab = [A,b];      % produces the augmented matrix x = zeros(n,1); % solution fprintf('Augmented matrix \n') %FORWARD ELIMINATION   for k=1:n-1     for i=k+1:n         lambda = A(i,k)/A(k,k);         for j=k+1:n             A(i,j) = A(i,j) - lambda*A(k,j);         end;         b(i) = b(i) - lambda*b(i)     end; end; %Backwards...
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 !
Need a MATLAB code for LU factorization(both partial and complete pivoting) of 10 random matrices of...
Need a MATLAB code for LU factorization(both partial and complete pivoting) of 10 random matrices of order 5x5. Please do not comment like I don't have computer or I don't know matlab. If you please answer otherwise you can skip.
Need a python code for LU factorization( for partial pivoting and complete pivoting) of a random...
Need a python code for LU factorization( for partial pivoting and complete pivoting) of a random matrix size 5x5.
Write a python function which takes input matrix A and applies gaussian elimination with pivoting strategy...
Write a python function which takes input matrix A and applies gaussian elimination with pivoting strategy which returns P,Q (permutation matrices) L (lower triangular matrix with 1's on the diagonal) and U (upper triangular matrix) such that (P^t)(A)(Q)=LU is correct,
When using Gaussian elimination to solve a system of linear equations, how can you recognize that...
When using Gaussian elimination to solve a system of linear equations, how can you recognize that the system has no solution?
Implement the steffenson method in matlab. The code must be in MATLAB DOnt answer if you...
Implement the steffenson method in matlab. The code must be in MATLAB DOnt answer if you cannot give correct MATLAB
Instructions: 1. Solve the following problems in a MATLAB script. You will be allowed to submit...
Instructions: 1. Solve the following problems in a MATLAB script. You will be allowed to submit only one script, please work all the problems in the same script (Hint: careful with variables names) 2. Show your work and make comments in the same script (Use %). Please write your name in the first line of the scrip with % 3. It is OK to work with other students, but what you submit should be your own work – not something...
Instructions: 1. Solve the following problems in a MATLAB script. You will be allowed to submit...
Instructions: 1. Solve the following problems in a MATLAB script. You will be allowed to submit only one script, please work all the problems in the same script (Hint: careful with variables names) 2. Show your work and make comments in the same script (Use %). Please write your name in the first line of the scrip with % 3. It is OK to work with other students, but what you submit should be your own work – not something...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT