Novice Matlab Question Gaussian Elimination Method

5 ビュー (過去 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 Center および File ExchangeMathematics についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by