Use Matlab Use Gaussian Elimination Backward Substitution Solve Following Linear System Pr Q37130981

(Use MATLAB) Use Gaussian elimination with backward substitutionto solve the following linear system. For this problem you willhave to do scaled partial pivoting. The matrix A and the vector bare in the Matlab code shown below

A=[3 -13 9 3;-6 4 1 -18;6 -2 2 4;12 -8 6 10];
display(A);
b=[-19;-34;16;26];
display(b);


Solution


Please find the required MATLAB script as the following:

%==================================================

clear all;
clc;

% Given data
A=[3 -13 9 3;-6 4 1 -18;6 -2 2 4;12 -8 6 10];
display(A);
b=[-19;-34;16;26];
display(b);

% Preliminary calculation
n=length(A(:,1));
A=[A,b];
x=zeros(n,1);
flag=0;
NROW=1:n;

for i=1:n-1
    if flag==0
       M=max(abs(A(NROW(i:n),i)));
        if M==0
           flag=1;
        else
           I=find(abs(A(NROW(i:n),i))==M);
           p=I(1)+i-1;
           if p>i
               NCOPY=NROW(i);
               NROW(i)=NROW(p);
               NROW(p)=NCOPY;
           end
        end
        if flag==0
           for j=i+1:n
               m=A(NROW(j),i)/A(NROW(i),i);
               A(NROW(j),i:n+1)=A(NROW(j),i:n+1)-m*A(NROW(i),i:n+1);
               clear m;
           end
        end
    end
end
if flag==0
   if A(NROW(n),n)==0
      flag=1;
   end
end
if flag==0
    x(n,1)=A(NROW(n),n+1)/A(NROW(n),n);
    for i=n-1:-1:1
       x(i,1)=(A(NROW(i),n+1)-sum(x(i+1:n,1)’.*A(NROW(i),i+1:n)))/A(NROW(i),i);
    end
else
    x=’no

OR
OR

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.