Using MATLAB develop an Mfile to determine LU factorization
Solution
3)
clc
%Click the run bottom and refer to the command window
%these are the inputs that can be modified by the user
%n = number of equations\'))
n=3;
%[A] = nxn coefficient matrix
A=[1,1,5;
-3,-6,2;
10,2,-1];
%[RHS] = nx1 right hand side array
RHS=[-21.5,-61.5,27];
%[X] = nx1 initial guess of the solution vector
X=[1,1,1];
%maxit = maximum number of iterations
maxit=15;
%initializing the absolute relative approximate error array
abs_ea=zeros(n);
%Iterations
%--------------------------------------------------------------------------
%defining the number of iterations to be conducted.
for k=1:maxit
disp(sprintf(\'Iteration number%d\',k))
disp(sprintf(\'Previous iteration values of the solution vector\'))
Xold=X;
Xold
%The following i loop generates the new solution vector:
for i=1:n
%initializing the series sum to zero.
summ=0;
for j=1:n
%Only adding i<> terms.
if (i<j)
%Generating the summation term.
summ=summ+A(i,j)*X(j);
end
if (i>j)
%generating the summation term.
summ=summ+A(i,j)*X(j);
end
end
%Using Gauss-Seidel method to calculate new [X] term.
X(i)=(RHS(i)-summ)/A(i,i);
end
%Initializing the maximum absolute relative percentage approximate error to zero.
Max_abs_ea=0.05;
%The following i loop generates the maximum absolute relative percentage approximate error for the kth step:
for i=1:n
%Calculating absolute relative percentage approximate error for each Xi.
abs_ea(i)=abs((X(i)-Xold(i))/X(i))*100.0;
%Defining the maximum value of the relative approximate errors.
if abs_ea(i)>Max_abs_ea
Max_abs_ea=abs_ea(i);
end
end
disp(sprintf(\'New iterative values of the solution vector\'))
X
disp(sprintf(\'Absolute relative percentage approximate error\'))
Y=rot90(abs_ea);
abs_e=Y(n,1:n)
disp(sprintf(\'Maximum absolute relative percentage approximate error\'))
Max_abs_ea
end
Output :
Iteration number1
Previous iteration values of the solution vector
Xold = 1 1 1
New iterative values of the solution vector
X = -29.500 25.333 -271.333
Absolute relative percentage approximate error
abs_e = 103.390 96.053 100.369
Maximum absolute relative percentage approximate error
Max_abs_ea = 103.39
-----------------------------------------------------------------------------
Iteration number2
Previous iteration values of the solution vector
Xold = -29.500 25.333 -271.333
New iterative values of the solution vector
X = 1.2592e+03 -7.0978e+02 1.1145e+04
Absolute relative percentage approximate error
abs_e = 102.34 103.57 102.43
Maximum absolute relative percentage approximate error
Max_abs_ea = 103.57
--------------------------------------------------------------------------------
Iteration number3
Previous iteration values of the solution vector
Xold = 1.2592e+03 -7.0978e+02 1.1145e+04
New iterative values of the solution vector
X = -5.3618e+04 3.0534e+04 -4.7514e+05
Absolute relative percentage approximate error
abs_e = 102.35 102.32 102.35
Maximum absolute relative percentage approximate error
Max_abs_ea = 102.35
----------------------------------------------------------------------------------
Iteration number4
Previous iteration values of the solution vector
Xold = -5.3618e+04 3.0534e+04 -4.7514e+05
New iterative values of the solution vector
X = 2.2841e+06 -1.3004e+06 2.0240e+07
Absolute relative percentage approximate error
abs_e = 102.35 102.35 102.35
Maximum absolute relative percentage approximate error
Max_abs_ea = 102.35
----------------------------------------------------------------------------------
Iteration number5
Previous iteration values of the solution vector
Xold = 2.2841e+06 -1.3004e+06 2.0240e+07
New iterative values of the solution vector
X = -9.7298e+07 5.5395e+07 -8.6218e+08
Absolute relative percentage approximate error
abs_e = 102.35 102.35 102.35
Maximum absolute relative percentage approximate error
Max_abs_ea = 102.35



