问题描述
我是Matlab的新手,所以有很多我不理解的地方;我有三个问题,如下:
我遇到的最大问题是function B= upper_triang(A)
,它指出它是“未使用的函数”。通过阅读Matlab工具提示,我猜想这与一个尚未定义的值有关吗? AN和B当然是未设置的,但是我认为我将B定义为一个函数,那么为什么它未设置?我以为B是U,但是在数学部分是ahh,所以我不确定它将是什么。
%%%This is for part a for problem 1.
A=zeros(8,8);%%%For the size column and row size of matrix A.
for j=1:8
for k=1:8
if j==k
A(j,k)=-2;
end
if abs(j-k)==1
A(j,k)=1;
end
end
end
[L,U]=lu(A); %%%Proving L and U.
L*U; %%%Proving that L*U=A,thus it is correct.
%%%This is for part b for problem 1
function B= upper_triang(A)
%%%Input A--> Matrix for m*n for the size
%%%Output B--> Is the upper triangular matrix of the same size
[m,n]=size(A);
for k=1:m-1
for i=k+1:m
u=A(i,k)/A(k,k);
for j=(k):n
A(i,j)=A(i,j)-u*A(k,j);
end
end
end
B-A;
end
解决方法
未使用功能
这里的问题是您仅创建了一个函数,但从未使用过。 function B = upper_triang(A)
之后的所有内容都告诉Matlab万一该函数被调用而您却从不执行,该怎么做。
我猜想,在LU分解之后但在函数声明之前,您应该添加一行
B = upper_triang(A);
未设置输出
第二个问题是,尽管您声明B应该是函数upper_triang()
的输出,但它并未在函数本身的任何位置“创建”。
B = something;
现在,我的线性代数非常生锈,但是我认为您想要做的应该是:
%%%This is for part b for problem 1
function B= upper_triang(A)
%%%Input A--> Matrix for m*n for the size
%%%Output B--> Is the upper triangular matrix of the same size
[m,n]=size(A);
B = A; % copy the input matrix in B,and work with it from now on
for k=1:m-1
for i=k+1:m
u=B(i,k)/B(k,k);
for j=(k):n
B(i,j)=B(i,j)-u*B(k,j);
end
end
end
B = B - A; % not really sure about this
end