gauasspivot elimination methode important
5 次查看(过去 30 天)
显示 更早的评论
% solving linear equation
clc
clear
%system wanted to be solved
A=[0,0,1,0,-6;
0,0,0,3,-3;
2,-11,8,1,0;
4,0,0,-1,-3;
0,0,9,-1,0] % coefficient matrix
b=[-50;0;0;0;160]% right handside matrix
%Gauss elimination method with pivoting
x=GaussPivot(A,b);
disp('Gauss elimnation method with pivoting')
x
function [x] = GaussPivot(A,b)
%GAUSS - Gauss elimination with pivoting
%to solve a system of linear equations Ax=b
%
n=size(A,1);
x=zeros(n,1);
A=[A,b]; % augmented matrix
for k=1:(n-1)
%pivoting
if A(k,k)==0
MaxPivot=A(k,k);
for i=k+1:n
if abs(A(i,k))>MaxPivot
iMaxPivot=i;
MaxPivot=abs(A(i,k));
end
end
TempA=A(k,:);
A(k,:)=A(iMaxPivot,:);
A(iMaxPivot,:)=TempA;
end
%end pivoting
for i=(k+1):n
A(i,:)=A(i,:)-A(i,k)/A(k,k)*A(k,:);
end
end
b=A(:,n+1);
%backward substitution
x(n)=b(n)/A(n,n);
for i=n-1:-1:1
x(i)=(b(i)-A(i,i+1:n)*x(i+1:n))/A(i,i);
end
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Systems Of Linear Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!