Novice Matlab Question Gaussian Elimination Method

18 views (last 30 days)
Joseph Wilder
Joseph Wilder on 23 Feb 2020
Edited: Sai Bhargav Avula on 23 Feb 2020
I keep getting this error, and I tried looking it up but I've had no success fixing it.
It is the error: Not enough input arguments in line 4
Here is the code:
function [x,U]=gausselim(A,b)
% function to perform gauss eliminination
%FORWARD ELIMINATION
n=length(b); %This is line 4
m=zeros(n,1);
x=zeros(n,1);
for k =1:n-1;
%compute the kth column of M
m(k+1:n) = A(k+1:n,k)/A(k,k);
%compute
%An=Mn*An-1;
%bn=Mn*bn-1;
for i=k+1:n
A(i, k+1:n) = A(i,k+1:n)-m(i)*A(k,k+1:n);
end;
b(k+1:n)=b(k+1:n)-b(k)*m(k+1:n);
end
U= triu(A);
%BACKWARD ELIMINATION
x(n)=b(n)/A(n,n);
for k =n-1:-1:1;
b(1:k)=b(1:k)-x(k+1)* U(1:k,k+1);
x(k)=b(k)/U(k,k);
end
b1=hilb(5)*[1;1;1;1;1];
Hx=hilb(5)
[x,U] = gausselim(Hx,b1);
end

Answers (1)

Sai Bhargav Avula
Sai Bhargav Avula on 23 Feb 2020
Hi,
The issue is the input arguments are defined within the function(b1,Hx). This part of the code should not be with in the function
b1=hilb(5)*[1;1;1;1;1];
Hx=hilb(5)
[x,U] = gausselim(Hx,b1);
You function will be something like this and needs to named as gausselim.
function [x,U]=gausselim(A,b)
% function to perform gauss eliminination
%FORWARD ELIMINATION
n=length(b); %This is line 4
m=zeros(n,1);
x=zeros(n,1);
for k =1:n-1;
%compute the kth column of M
m(k+1:n) = A(k+1:n,k)/A(k,k);
%compute
%An=Mn*An-1;
%bn=Mn*bn-1;
for i=k+1:n
A(i, k+1:n) = A(i,k+1:n)-m(i)*A(k,k+1:n);
end;
b(k+1:n)=b(k+1:n)-b(k)*m(k+1:n);
end
U= triu(A);
%BACKWARD ELIMINATION
x(n)=b(n)/A(n,n);
for k =n-1:-1:1;
b(1:k)=b(1:k)-x(k+1)* U(1:k,k+1);
x(k)=b(k)/U(k,k);
end
end
Hope this helps!
  3 Comments
Sai Bhargav Avula
Sai Bhargav Avula on 23 Feb 2020
The first part of the code should be in a different file lets say 'gm.m'
b1=hilb(5)*[1;1;1;1;1];
Hx=hilb(5)
[x,U] = gausselim(Hx,b1);
The rest of the code needs to be saved as gausselim.m and should be saved in the same folder as that of 'gm.m'or on any of the folder that is added to the path. For now save it in the same folder. I have also added the gausselim function file and the gm file. you just save them and run the gm file . Hope this helps
function [x,U]=gausselim(A,b)
% function to perform gauss eliminination
%FORWARD ELIMINATION
n=length(b);
m=zeros(n,1);
x=zeros(n,1);
for k =1:n-1;
%compute the kth column of M
m(k+1:n) = A(k+1:n,k)/A(k,k);
%compute
%An=Mn*An-1;
%bn=Mn*bn-1;
for i=k+1:n
A(i, k+1:n) = A(i,k+1:n)-m(i)*A(k,k+1:n);
end;
b(k+1:n)=b(k+1:n)-b(k)*m(k+1:n);
end
U= triu(A);
%BACKWARD ELIMINATION
x(n)=b(n)/A(n,n);
for k =n-1:-1:1;
b(1:k)=b(1:k)-x(k+1)* U(1:k,k+1);
x(k)=b(k)/U(k,k);
end
end

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!