Я новичок в Matlab. У меня три функции. Когда я звоню co.m и pol_det.m они оба работают нормально. Однако, когда я вызываю minor.m, который сам по себе вызывает pol_det, который, в свою очередь, вызывает co.m, я получаю ошибку, ссылающуюся на функцию co.m: Undefined или переменную "new_m".
Я работаю с версией R2007b. Три функции ниже. Первоначально они написаны каждый в отдельном документе .m.
function [ k ] = pol_det(a)
%calculates the determinant of a general matrix (not just consisting of
%numbers)
dim=size(a); %dimensions of a matrix
if dim(1)~= dim(2)
disp('Only Square Matrices, please')
end
m=length(a);
k=0;
if(m==2)
k=sum_p(conv(a(1,1),a(2,2)),- conv(a(2,1),a(1,2))); %calc. the determinant of a 2x2 m.
else
for i=1:m
k=k+((-1)^(1+i))*conv(a(1,i),co(a,1,i)); %calc. the determinant using cofactor expansion
end
end
if (k==0)
disp('Matrix non-invertible')
end
end
function [ out ] = co( a,i,j )
%cofactor expansion,
%http://people.math.carleton.ca/~kcheung /math/notes/MATH1107/wk07/07_cofactor_expansion.html
[m,n]=size(a);
%create a new matrix by eliminating the row and column in which the %element is present
%new_m=zeros(m,n)
row=1;
col=1;
for i1=1:m
for j1=1:n
if(i1~=i && j1~=j)
new_m(row,col)=a(i1,j1);
col=col+1;
end
end
if(col~=1)
row=row+1;
end
col=1;
end
%new_m
out=pol_det(new_m);
end
function [ m ] = minor(a)
dim=size(a); %dimensions of a matrix
if dim(1)~= dim(2)
disp('Only Square Matrices, please')
end
a=a.';
for i=1:dim(1)
for j=1:dim(1)
a(i,:)=[];
a(:,j)=[];
m(i,j)= pol_det(a);
end
end
end