Novice Matlab Question Gaussian Elimination Method

5 次查看(过去 30 天)
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

回答(1 个)

Sai Bhargav Avula
Sai Bhargav Avula 2020-2-23
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 个评论
Joseph Wilder
Joseph Wilder 2020-2-23
If I put the code like that, it says Function Definitions are not permitted in this context
b1=hilb(5)*[1;1;1;1;1];
Hx=hilb(5)
[x,U] = gausselim(Hx,b1);
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
Sai Bhargav Avula
Sai Bhargav Avula 2020-2-23
编辑:Sai Bhargav Avula 2020-2-23
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by