Novice Matlab Question Gaussian Elimination Method

조회 수: 7 (최근 30일)
Joseph Wilder
Joseph Wilder 2020년 2월 23일
편집: Sai Bhargav Avula 2020년 2월 23일
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