필터 지우기
필터 지우기

Calling a function

조회 수: 2 (최근 30일)
Ben
Ben 2012년 6월 3일
I have a code that converts a matrix into its upper form and lower form counterparts.
I want to use this M file script in another M file script, where I take the L and U matrices for this new script that will take these L and U and use them to find the inverse of the matrix.
The problem is when I call the function, I can't use anything from the function I am calling.
Ie: I have the code:
function [L, U] = LU_decomp(A)
A=input('Enter the square matrix to be factorized ');
[m,n]=size(A);
if m~=n
disp('Matrix must be square')
end
U=zeros(m);
L=zeros(m);
for j=1:m
L(j,j)=1;
end
for j=1:m
U(1,j)=A(1,j);
end
for i=2:m
for j=1:m
for k=1:i-1
s1=0;
if k==1
s1=0;
else
for p=1:k-1
s1=s1+L(i,p)*U(p,k);
end
end
L(i,k)=(A(i,k)-s1)/U(k,k);
end
for k=i:m
s2=0;
for p=1:i-1
s2=s2+L(i,p)*U(p,k);
end
U(i,k)=A(i,k)-s2;
end
end
end
disp('The matrix to be decomposed is')
A
disp('The Lower Triangular Matrix is')
L
disp('The Upper Triangular Matrix is')
U
So when a matrix is entered, it is split into its L and U form.
Now I have this other script:
function A =inverse(A)
LU_decomp(A)
L
U
I haven't completed the code for the inverse yet, but f I try the above script, entering in the matrix I want to use, LU_decomp calculates L and U forms, but then if I am trying using L and U in the body of my inverse script, L and U will be unrecognized. Why is this?Am I not calling the function correctly? (I'd rather split this up into two separate m-files than keep it in one.)

채택된 답변

Walter Roberson
Walter Roberson 2012년 6월 3일
Call
[L, U] = LU_decomp(A);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by