필터 지우기
필터 지우기

variables not being saved?

조회 수: 49 (최근 30일)
Olivia
Olivia 2024년 8월 10일 16:47
편집: Umar 2024년 8월 10일 18:25
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
  댓글 수: 7
dpb
dpb 2024년 8월 10일 17:53
You didn't save the results you calculated to be used in the next piece of the code...see Answer for revised code.
Read the function documentation and especially the <section on workspaces>. The scope of function variables is local to the function.
Umar
Umar 2024년 8월 10일 18:12
편집: Umar 2024년 8월 10일 18:25

Hi @Olivia,

I have updated both functions. The updated provided code consists of two functions: lumine and lusolver.The lumine function performs the LU decomposition of a given coefficient matrix A and returns the lower matrix L and upper matrix U. It follows the following steps:Checks if the matrix A is square. If not, it throws an error and also if the matrix A is singular. If yes, it throws an error.Then, it initializes L as an identity matrix and U as A.Performs the LU decomposition using nested loops.Updates the rows of U and store the factors in L and sets the diagonal elements of L to 1.The lusolver function takes the lower matrix L, upper matrix U, and a vector B as inputs and solves the system of linear equations using LU decomposition. It follows the following steps:Initializes vectors y and x with zeros., performs forward substitution to solve Ly = B and backward substitution to solve Ux = y. Afterwards, returns the solution vector x.

Lumine Function

    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 if the matrix is square
        error('Matrix A must be square');
    end 
    if det(A) == 0 % checks if the matrix is singular
        error('Matrix cannot be singular');
    end
    % Initialize L as an identity matrix and U as A
    L = eye(n); 
    U = A;
    for k = 1:n % starts at position 1, goes to the last column
        for i = k+1:n % starts at row after k, 
         goes to second to last row
            factor = U(i,k)/U(k,k); % defines factor
            U(i,k:n) = U(i,k:n) - factor * U(k,k:n); 
            % update row i of U
            L(i,k) = factor; % store factor in L
        end
     end
    % The diagonal of L should be set to 1 (identity property)
    for i = 1:n
        L(i,i) = 1; 
     end
     end
     q = [2 1 -1; 4 0 2; 8 1 0];
     [L,U] = lumine(q); 
     % Now both L and U will be defined correctly.
     disp('Matrix L:');
     disp(L); % Display the lower matrix L
     disp('Matrix U:');
     disp(U); % Display the upper matrix U

Lusolver function

    function solution = lusolver(L, U, B)
    n = length(B);
    y = zeros(n, 1);
    x = zeros(n, 1);
    % Forward Substitution (Ly = B)
    for i = 1:n
        y(i) = B(i) - L(i,1:i-1)*y(1:i-1);
    end
    % Backward Substitution (Ux = y)
    for i = n:-1:1
        x(i) = (y(i) - U(i,i+1:n)*x(i+1:n)) / U(i,i);
    end
    solution = x;
    end
    B = [-1;-2;-3];
    solution = lusolver(L,U,B);
    disp(solution);

Please see attached.

Hope, after this detailed explanation and provided two updated functions, your problem is resolved. Please let me know if you still have any further questions.

댓글을 달려면 로그인하십시오.

답변 (2개)

dpb
dpb 2024년 8월 10일 17:48
이동: dpb 2024년 8월 10일 17:48
As I suspected, you didn't return the values you calculated when you called lumine; L and U are local variables insidee the function and unless they are stored when the function is called, they're lost....
q = [2 1 -1; 4 0 2;8 1 0];
[L,U]=lumine(q);
B = [-1;-2;-3];
lusolver(L,U,B)
...

Walter Roberson
Walter Roberson 2024년 8월 10일 17:51
lumine(q)
That call executes lumine with parameter q. Then it assigns the first output to a variable named ans and displays the first output.
function [L,U] = lumine(A)
When that code is called, the variables L and U are not assigned to in the caller. L and U here are "dummy" variables, placeholders for "the first output argument" and "the second output argument". Everything in MATLAB happens positionally.
You need to assign the outputs of calling lumine, such as
[L_out, U_out] = lumine(q);
or possibly even
[L, U] = lumine(q);

카테고리

Help CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by