Hi, I have some code written to solve LU decomposition, but the code tells me that there is an error in [n, n], why is this error? Any suggestions?
    6 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
 function matrizlu(A)
[n,n]=size(A);
L=eye(n);
U=eye(n);
for k=1:n
    L(k,k)=1;
    U(1,k)=A(1,k);
    for i=k:n
        U(k,i)=A(k,i)-L(k,1:k-1)*U(1:k-1,i);
    end
    for i=k+1:n
        L(i,k)=A(i,k)-L(i,1:k-1)*U(1:k-1,k)/U(k,k);
    end
end
L
U
0 comentarios
Respuestas (1)
  Steven Lord
    
      
 el 4 de Oct. de 2021
        When you're asking for help understanding the cause of an error message, please include the error message in your post.
I'm going to guess that it is something along these lines:
matrizlu()
If that's the case:
function matrizlu(A)
you've defined matrizlu to accept up to 1 input argument.
[n,n]=size(A);
This line of code uses the input argument, so you must call this function with at least 1 input argument to have it work.
So the function must be called with at least 1 input and it can be called with up to 1 input. Combined these mean that you must call matrizlu with exactly 1 input argument. But in the code above that threw the error I called it with 0 input arguments. That is the reason for the error. The solution is to pass the matrix whose LU factorization you want to receive into the function.
L=eye(n);
U=eye(n);
for k=1:n
    L(k,k)=1;
    U(1,k)=A(1,k);
    for i=k:n
        U(k,i)=A(k,i)-L(k,1:k-1)*U(1:k-1,i);
    end
    for i=k+1:n
        L(i,k)=A(i,k)-L(i,1:k-1)*U(1:k-1,k)/U(k,k);
    end
end
L
U
end
You probably want matrizlu to return the L and U matrices (so you can do something else with them in the workspace from which you called matrizlu) rather than just displaying them. If so update your function declaration:
% Commenting this out so MATLAB Answers can run the code above without this
% affecting it
%{
function [L, U] = matrizlu(A)
%}
0 comentarios
Ver también
Categorías
				Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

