function Effect_of_significant_digits
clc
%The input parameters are located after the functions
%For these simulation, 4 subfunctions were created so that matlab computes
%the operations using only the specified significant digits.
%the functions inputs are the two numbers and the number of significant
%digits "d"

%Creating function for addiing
function a=add(a,b,d)
%Creating a disclaimer for values of zero
if a==0
a1=a;
else
%The log value plus one will indicate the number of digits
ab=log10(abs(a))+1;
%Using the floor function to get an integer
fab=floor(ab);
%Using the deried significant digits "d" to shift the number
m=d-fab;
q=a*10^m;
a1=floor(q)*10^(-m);
end
%The same procedure is used for the second number
if b==0
    b1=b;
else
ba=log10(abs(b))+1;
fba=floor(ba);
m=d-fba;
q=b*10^m;
b1=floor(q)*10^(-m);
end
a=a1+b1;
end
%---------------------------------------------
%Creating function for subtracting
function b=sub(a,b,d)
%Creating a disclaimer for values of zero
if a==0
a1=a;
else
%The log value plus one will indicate the number of digits
ab=log10(abs(a))+1;
fab=floor(ab);
%Using the deried significant digits "d" to shift the number
m=d-fab;
q=a*10^m;
a1=floor(q)*10^(-m);
end
%The same procedure is used for the second number
if b==0
    b1=b;
else
ba=log10(abs(b))+1;
fba=floor(ba);
m=d-fba;
q=b*10^m;
b1=floor(q)*10^(-m);
end
b=a1-b1;
end

%---------------------------------------------
%Creating function for multiplying
function c=mul(a,b,d)
%Creating a disclaimer for values of zero
if a==0
a1=a;
else
%The log value plus one will indicate the number of digits
ab=log10(abs(a))+1;
fab=floor(ab);
%Using the deried significant digits "d" to shift the number
m=d-fab;
q=a*10^m;
a1=floor(q)*10^(-m);
end
%The same procedure is used for the second number
if b==0
    b1=b;
else
ba=log10(abs(b))+1;
fba=floor(ba);
m=d-fba;
q=b*10^m;
b1=floor(q)*10^(-m);
end
c=a1*b1;
end

%---------------------------------------------
%Creating function for dividing
function d=div(a,b,d)
%Creating a disclaimer for values of zero
if a==0
a1=a;
else
%The log value plus one will indicate the number of digits
ab=log10(abs(a))+1;
fab=floor(ab);
%Using the deried significant digits "d" to shift the number
m=d-fab;
q=a*10^m;
a1=floor(q)*10^(-m);
end
%The same procedure is used for the second number
if b==0
    b1=b;
else
ba=log10(abs(b))+1;
fba=floor(ba);
m=d-fba;
q=b*10^m;
b1=floor(q)*10^(-m);
end
d=a1/b1;
end
%--------------------------------------------
%These are the input parameters than can be manipulated by the user
n=3;
A1=[10.0,-7.0,0;-3.0,2.099,6.0;5.0,-1.0,5.0];
RHS1=[7,3.901,6];
lower_lim=3;
upper_lim=10;
%-----------------------------------------------------------------
disp(sprintf('Effect of Significant Digits in Solution of Simultaneous Linear Equations'))
disp(sprintf('©2007 Fabian Farelo, Autar Kaw'))
disp(sprintf('University of South Florida'))
disp(sprintf('United States of America'))
disp(sprintf('kaw@eng.usf.edu'))

disp(sprintf('\n\nNOTE: This worksheet demonstrates the use of Matlab to illustrate'))
disp(sprintf('the effect of significant digits in the solution of simultaneous'))
disp(sprintf('linear equations'))
%---------------------------------------------------------------
disp(sprintf('\n\n**************************** Introduction ******************************'))
disp(sprintf('\nThe number of significant digits used in numerical solutions of')) 
disp(sprintf('simultaneous linear equations influences the accuracy of the solution')) 
disp(sprintf('vector, especially if the coefficient matrix is nearly singular. ')) 
disp(sprintf('In this worksheet, the reader can choose a system of equations and ')) 
disp(sprintf('see the influence of significant digitson each element of the\nsolution vector.')) 

disp(sprintf('\nThe following simulation uses Naïve Gaussian Elimination method to')) 
disp(sprintf('demonstrate the effect that significant digits have on the accuracy of')) 
disp(sprintf('the solution.')) 
%----------------------------------------------------------------
disp(sprintf('\n\n**************************** Input data ******************************'))
disp(sprintf('\nThe following are the input parameters to begin the simulation.')) 
disp(sprintf('This is the only section that requires user input. In the simulation,'))  
disp(sprintf('Naïve Gaussian Elimination method is used to solve a set of simultaneous'))  
disp(sprintf('linear equations, [A] [X] = [RHS], where [A]nxn is the square coefficient'))  
disp(sprintf('matrix, [X]nx1 is the solution vector, and [RHS]nx1 is the right hand'))  
disp(sprintf('side array. To demonstrate the effect that significant digits have on the'))  
disp(sprintf('accuracy of solution, Matlab will return a list of solution vectors that'))  
disp(sprintf('were calculated using the significant digits within the range of your')) 
disp(sprintf('choice. It will then plot each element of the solution vector as a')) 
disp(sprintf('function of the number of significant digits used.'))  

disp(sprintf('Input parameters:'))
disp(sprintf('n = number of equations'))
disp(sprintf('[A] = nxn coefficient matrix'))
disp(sprintf('[RHS] = nx1 right hand side array'))
disp(sprintf('lower_lim =  lower limit of range of significant digits')) 
disp(sprintf('upper_lim = upper limit of range of significant digits'))
disp(sprintf('\n these are the default parameters which can be changed at the top'))
disp(sprintf('of the M-File'))
n
A=A1
RHS=RHS1
lower_lim
upper_lim
disp(sprintf('\n\n*************** Naive Gaussian Elimination Method ******************')) 
disp(sprintf('\nThe Naive Gaussian Elimination Method is permormed with significant'))
disp(sprintf('digits from the lower limit to the upper limit. The results obtained for')) 
disp(sprintf('each procedure are stored in a matrix [X_Values] to be ploted further on')) 
disp(sprintf('the simulation. Refer to the M-file to see how the procedure works'))
%Creating the matrix to store the X values
X_Values=zeros(upper_lim-lower_lim,n);
for d=lower_lim:upper_lim
X=ones(n,1);
A=A1;
RHS=RHS1;
%Forward Elimination Steps:
%Conducting (n-1) steps of forward elimination.
for k=1:n-1
%Using elementary row operations to transform the coefficient matrix into upper triangular form. 
 for i=(k+1):n
%Calculating the value that is multiplied to each equation.
   multiplier=div(A(i,k),A(k,k),d);
        for j=(k+1):n
           %Reducing the number of unknowns in the jth row.
           A(i,j)=sub(A(i,j),mul(multiplier,A(k,j),d),d);
       end 
     %Conducting forward elimination steps on the [RHS] vector.
     RHS(1,i)=sub(RHS(1,i),mul(multiplier,RHS(1,k),d),d);
 end 
end 
 
 %Back Substitution Steps:
 %Solving for the nth equation as it has only one unknown.
 X(n,1)=div(RHS(1,n),A(n,n),d);
 %Solving for the remaining (n-1) unknowns working backwards from the (n-1)th equation to the first equation.
 for i=(n-1):-1:1
    %Initializing the series sum to zero.
    su=0;
    for j=(i+1):n
       %Substituting known values into the ith equation.
       t=A(i,j)*X(j);
       su=add(su,t,d);       
   end 
 %Solving for the ith unknown.
  X(i,1)=div(sub(RHS(1,i),su,d),A(i,i),d);
end
format long
X_Values(d-lower_lim+1,1:n)=X';
end

disp(sprintf('\n\n************************** Results ******************************'))
disp(sprintf(' Each [X] solution vector is stored in the Matrix and the number of'))
disp(sprintf('significant digits used increases with the rows'))

X_Values

disp(sprintf('The exact solution to the system of equations with default number')) 
disp(sprintf('of significant digits in Matlab is:'))
U=horzcat(A1,RHS1');
X_e=rref(U);
X_exact=X_e(1:n,n+1)'

disp(sprintf('The graphs also demonstrate the effect that the number of'))
disp(sprintf('significant digits has on the solution. Each element of the')) 
disp(sprintf('solution vector is plotted as a function of the number of')) 
disp(sprintf('significant digits used.')) 

XX=(lower_lim:upper_lim);
for i=1:n
     
    X_v=X_Values(1:(upper_lim-lower_lim+1),i);
    figure
    plot(XX,X_v,'linewidth',3)
    xlabel('Number of significant digits')
    ylabel(['X',num2str(i)])
    title(['Value of X',num2str(i),' as a function of the number of significant digits used'])
     
 end
end

