Novice Matlab Question Gaussian Elimination Method

5 visualizaciones (últimos 30 días)
Joseph Wilder
Joseph Wilder el 23 de Feb. de 2020
Editada: Sai Bhargav Avula el 23 de Feb. de 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

Respuestas (1)

Sai Bhargav Avula
Sai Bhargav Avula el 23 de Feb. de 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 comentarios
Joseph Wilder
Joseph Wilder el 23 de Feb. de 2020
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 el 23 de Feb. de 2020
Editada: Sai Bhargav Avula el 23 de Feb. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Mathematics en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by