variables not being saved?
I just included the code for U here, but neither L nor U are being saved, but they can both be displayed? I’m trying to use the values for L and U as inputs in another function, but it says "unrecognized variable"
function [L,U] = lumine(A)
%lumine: LU decomposition
%input:
%A = coefficient matrix
%output:
%L = lower matrix
%U = upper matrix
[m,n] = size(A); % defines m and n
if m~=n %checks to see if the matrix is square
error(‘Matrix A must be square’) % displays error if matrix is not square
end
if det(A) == 0 %checks to see if the matrix is singular
error(‘Matrix cannot be singular’)
end
% forward elimination
Afake = A;
for k = 1:n % starts at position 1, goes to the last column
for i = k+1:n %starts at the row after k, goes to the second to last row
factor = Afake(i,k)/Afake(k,k); %defines factor as the element directly below one of the elements in the diagonal, divided by that element on the diagonal
Afake(i,1:n) = Afake(i,1:n)-factor*Afake(k,1:n); %for each row of the matrix, the row before is multiplied by factor, which is then subtracted
end
end
U = Afake; % A becomes the upper matrix after every row is completed
EndI just included the code for U here, but neither L nor U are being saved, but they can both be displayed? I’m trying to use the values for L and U as inputs in another function, but it says "unrecognized variable"
function [L,U] = lumine(A)
%lumine: LU decomposition
%input:
%A = coefficient matrix
%output:
%L = lower matrix
%U = upper matrix
[m,n] = size(A); % defines m and n
if m~=n %checks to see if the matrix is square
error(‘Matrix A must be square’) % displays error if matrix is not square
end
if det(A) == 0 %checks to see if the matrix is singular
error(‘Matrix cannot be singular’)
end
% forward elimination
Afake = A;
for k = 1:n % starts at position 1, goes to the last column
for i = k+1:n %starts at the row after k, goes to the second to last row
factor = Afake(i,k)/Afake(k,k); %defines factor as the element directly below one of the elements in the diagonal, divided by that element on the diagonal
Afake(i,1:n) = Afake(i,1:n)-factor*Afake(k,1:n); %for each row of the matrix, the row before is multiplied by factor, which is then subtracted
end
end
U = Afake; % A becomes the upper matrix after every row is completed
End I just included the code for U here, but neither L nor U are being saved, but they can both be displayed? I’m trying to use the values for L and U as inputs in another function, but it says "unrecognized variable"
function [L,U] = lumine(A)
%lumine: LU decomposition
%input:
%A = coefficient matrix
%output:
%L = lower matrix
%U = upper matrix
[m,n] = size(A); % defines m and n
if m~=n %checks to see if the matrix is square
error(‘Matrix A must be square’) % displays error if matrix is not square
end
if det(A) == 0 %checks to see if the matrix is singular
error(‘Matrix cannot be singular’)
end
% forward elimination
Afake = A;
for k = 1:n % starts at position 1, goes to the last column
for i = k+1:n %starts at the row after k, goes to the second to last row
factor = Afake(i,k)/Afake(k,k); %defines factor as the element directly below one of the elements in the diagonal, divided by that element on the diagonal
Afake(i,1:n) = Afake(i,1:n)-factor*Afake(k,1:n); %for each row of the matrix, the row before is multiplied by factor, which is then subtracted
end
end
U = Afake; % A becomes the upper matrix after every row is completed
End variables MATLAB Answers — New Questions