Solving System of Equations using Gauss Elimination with pivoting

1 次查看(过去 30 天)
Hi, I am receiving the following error for the code trying to use Gauss elimination by pivoting. I am stuck on what I am doing incorrectly.
ERROR:
Gauss([1,2,-1;5,2,2;-3,5,-1],[2;9;1],3,[1;2;3],1,0.001) %Inputted Values
I just keep receiving
1
2
3 as outputs versus the answer of all ones
CODE:
function Gauss(a,b,n,x,tol,er)
length s(n);
er=0;
for i=1:n
s(i)=abs(a(i,1));
for j=2:n
if abs(a(i,j))>s(i)
s(i)=abs(a(i,j));
end
end
end
Eliminate(a,s,n,b,tol,er)
if er~= -1
Substitute(a,n,b,x)
disp(x)
end
end
function Eliminate(a,s,n,b,tol,er)
for k=1: n-1
Pivot(a,b,s,n,k)
if abs(a(k,k)/s(k))<tol
er=-1;
end
end
for i=k+1:n
factor=a(i,k)/a(k,k);
for j=k+1:n
a(i,j)=a(i,j)-factor*a(k,j);
end
b(i)=b(i)-factor*b(k);
end
if abs(a(n,n)/s(n))<tol
er=-1;
end
end
function Pivot(a,b,s,n,k)
p=k;
big=abs(a(k,k)/s(k));
for ii=k+1:n
dummy=abs(a(ii,k)/s(ii));
if dummy>big
big=dummy;
p=ii;
end
end
if p~=k
for jj=k:n
dummy=a(p,jj);
a(p,jj)=a(k,jj);
a(k,jj)=dummy;
end
dummy=b(p);
b(k)=dummy;
dummy=s(p);
s(p)=s(k);
s(k)=dummy;
end
end
function Substitute(a,n,b,x)
x(n)=b(n)/(a(n,n));
for i=n-1:1:-1
sum=0;
for j=i+1:n
sum=sum+a(i,j)*x(j);
end
x(n)=(b(n)-sum)/a(n,n);
end
end

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differential Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by