求一个matlab程序 10
就是用高斯约当消元法求逆矩阵,输入一个可逆矩阵A,输出它的逆矩阵,如果A不可逆,输出0求逆部分已经写好,问一下输出0那里怎么写...
就是用高斯约当消元法求逆矩阵,输入一个可逆矩阵A,输出它的逆矩阵,如果A不可逆,输出0
求逆部分已经写好,问一下输出0那里怎么写 展开
求逆部分已经写好,问一下输出0那里怎么写 展开
1个回答
展开全部
function C = gauss_elimination(A,B) % defining the function
A= [ 1 2; 4 5] % Inputting the value of coefficient matrix
B = [-1; 4] % % Inputting the value of coefficient matrix
i = 1; % loop variable
X = [ A B ];
[ nX mX ] = size( X); % determining the size of matrix
while i <= nX % start of loop
if X(i,i) == 0 % checking if the diagonal elements are zero or not
disp('Diagonal element zero') % displaying the result if there exists zero
return
end
X = elimination(X,i,i); % proceeding forward if diagonal elements are non-zero
i = i +1;
end
C = X(:,mX);
function X = elimination(X,i,j)
% Pivoting (i,j) element of matrix X and eliminating other column
% elements to zero
[ nX mX ] = size( X);
a = X(i,j);
X(i,:) = X(i,:)/a;
for k = 1:nX % loop to find triangular form
if k == i
continue
end
X(k,:) = X(k,:) - X(i,:)*X(k,j); % final result
end
A= [ 1 2; 4 5] % Inputting the value of coefficient matrix
B = [-1; 4] % % Inputting the value of coefficient matrix
i = 1; % loop variable
X = [ A B ];
[ nX mX ] = size( X); % determining the size of matrix
while i <= nX % start of loop
if X(i,i) == 0 % checking if the diagonal elements are zero or not
disp('Diagonal element zero') % displaying the result if there exists zero
return
end
X = elimination(X,i,i); % proceeding forward if diagonal elements are non-zero
i = i +1;
end
C = X(:,mX);
function X = elimination(X,i,j)
% Pivoting (i,j) element of matrix X and eliminating other column
% elements to zero
[ nX mX ] = size( X);
a = X(i,j);
X(i,:) = X(i,:)/a;
for k = 1:nX % loop to find triangular form
if k == i
continue
end
X(k,:) = X(k,:) - X(i,:)*X(k,j); % final result
end
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询