Specifying Significant Digits and Displaying values at each iteration of a Function with a For Loop.
Show older comments
I am attempting to use the gaussian elimination and have defined the function as a set of for loops that eliminate and back substitute at each iteration. I need to do the calculation with 3 significant digit arithmetic as well as display the result of the augmented matrix for each iteration. The purpose of this is to have a user input the initial Matrix value for matrices A and b and have the function output the answer. At each iteration I need the eliminated column along with the remaining columns and rows to display on the command window. I need to be able to determine which iterations I want to display on the command window. The second portion of this code I need to find the absolute error from using 3 significant digit arithmetic and using the computer's full capacity.
function x= Gaussianbasic(A,b)
%uses basic Gaussian elimination with backwards substitution
%of n linear equations in n unknowns
%Inut variables
%A is the coeffcient vector
%b is the constants vector
%Output Variables
%x is the solution
Aug=[A b];
[n,ncol]=size(Aug);
x=zeros(n,1); %initializing the column vector x
%Elimination Steps
for k=1:n-1
pivot= Aug(k,k);
for row= k+1:n
m= Aug(row,k)/pivot;
Aug(row,k+1:ncol)= Aug(row, k+1:ncol)-m*Aug(k,k+1: ncol);
end
end
% Backward substitution steps
x(n)=Aug(n,ncol)/ Aug(n,n);
for k= n-1:-1:1
x(k)=(Aug(k,ncol))- Aug(k,k+1:n)*x(k+1:n)/Aug(k,k);
end
end
The Function is saved in a separate file and called when the user inputs Matrix A and b.
%%Prompt User Input
A= input('Input the values for matrix A with size 3x3: ');
b= input('Input the values for the Matrix b with size 1x3: ');
Aug=[A b];
disp('The value of the Augmented Matrix Ab is');
disp (Aug);
x=Gaussianbasic(A,b)
1 Comment
James Tursa
on 10 Oct 2016
What is your specific question? Are you having trouble with some part of your code? Are you asking how to limit the number of digits used in a floating point calculation? Or ...?
Answers (0)
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!