フィルターのクリア

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 Center および File ExchangeData Import and Export についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by