MATLAB Answers

Implementing Gauss Elimination by pivoting with Matlab

15 views (last 30 days)
Hi, I am receiving the following error for the code trying to use Gauss elimination by pivoting. I am stuck on what I am doing incorrectly.
ERROR:
Gauss([1,2,-1;5,2,2;-3,5,-1],[2;9;1],3,[1;2;3],1000,0.001) %Inputted Values
I just keep receiving
1
2
3 as outputs versus the answer of all ones
CODE:
function Gauss(a,b,n,x,tol,er)
length s(n);
er=0;
for i=1:n
s(i)=abs(a(i,1));
for j=2:n
if abs(a(i,j))>s(i)
s(i)=abs(a(i,j));
end
end
end
Eliminate(a,s,n,b,tol,er)
if er~= -1
Substitute(a,n,b,x)
disp(x)
end
end
function Eliminate(a,s,n,b,tol,er)
for k=1: n-1
Pivot(a,b,s,n,k)
if abs(a(k,k)/s(k))<tol
er=-1;
end
end
for i=k+1:n
factor=a(i,k)/a(k,k);
for j=k+1:n
a(i,j)=a(i,j)-factor*a(k,j);
end
b(i)=b(i)-factor*b(k);
end
if abs(a(n,n)/s(n))<tol
er=-1;
end
end
function Pivot(a,b,s,n,k)
p=k;
big=abs(a(k,k)/s(k));
for ii=k+1:n
dummy=abs(a(ii,k)/s(ii));
if dummy>big
big=dummy;
p=ii;
end
end
if p~=k
for jj=k:n
dummy=a(p,jj);
a(p,jj)=a(k,jj);
a(k,jj)=dummy;
end
dummy=b(p);
b(k)=dummy;
dummy=s(p);
s(p)=s(k);
s(k)=dummy;
end
end
function Substitute(a,n,b,x)
x(n)=b(n)/(a(n,n));
for i=n-1:1:-1
sum=0;
for j=i+1:n
sum=sum+a(i,j)*x(j);
end
x(n)=(b(n)-sum)/a(n,n);
end
end

  2 Comments

Stephen Cobeldick
Stephen Cobeldick on 20 Feb 2020
None of your functions do anything, as they do not return any output arguments (or use any other methods to change data, e.g. via nested functions). For example, the functions called inside Gauss function appear to do nothing, and the variables er and x never change, thus making the Gauss function's purpose rather moot.
As Steven Lord wrote, it really looks like you are writing in another language, perhaps one which uses pass-by-reference (which MATLAB does not) or something similar. You should probably do the introductory tutorials to learn basic MATLAB concepts, such as how to call functions and return arguments from them.
Writing lots and lots of code without testing or understanding what it does is a common way that beginners get stuck. You should not write your second lilne of code until your first line of code is checked and tested and does exactly what you need it to do. Only then should you move on to writing the second line. Then you will not end up with one page of code and no idea what it is doing.
Your code has quite a few mlint messages shown in the Editor, which indicate likely mistakes and bugs. For example, the er input arguments are unused. This line does nothing:
length s(n);
Steven Lord
Steven Lord on 20 Feb 2020
Another technique that can help is to start not by writing any code but by writing comments outlining the program you're going to create. Write at a high level each step you want to execute and make sure you haven't forgotten anything then start implementing each of those high-level steps. Break steps that are too vague, broad, or complicated into smaller steps.
function makePeanutButterAndJellySandwich
% Put two slices of bread on a plate
% Get peanut butter from jar using knife
% Spread peanut butter on one slice of bread
% Get jelly from jar using knife
% Spread jelly on other slice of bread
% Eat
end
There's (at least) one missing step -- can you tell what it is?
char([112 117 116 116 105 110 103 32 116 104 101 32 116 119 111 32 115 108 105, ...
99 101 115 32 111 102 32 98 114 101 97 100 32 116 111 103 101 116 104, ...
101 114])
Finding this missing step at the planning phase could avoid some mess during the implementation.

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 20 Feb 2020
That's not MATLAB code. I'm not quite sure what language it is, but it's not MATLAB.
If you're new to MATLAB, I recommend going through the free 2-hour MATLAB Onramp course in the Tutorials section of the Support page (https://www.mathworks.com/support.html) to learn the basics of how to work with MATLAB.

  1 Comment

William Donovan
William Donovan on 20 Feb 2020
Sorry, I copied the wrong thing. It should be good now.

Sign in to comment.

More Answers (0)

Sign in to answer this question.