Please help me...

1 次查看(过去 30 天)
NOR AZIERA
NOR AZIERA 2022-12-14
How to solve this to get same answer with Matrix A
n=3; A=randn(n); A=A'*A; [V,D]=eig(A); X=zeros(n);. for i=1:n X=outprod(V,D)*V'; display(X); display(A'); end
  2 个评论
NOR AZIERA
NOR AZIERA 2022-12-14
how about this coding???
function [X]=outprod(V,D)
n=3;
A=randn(n); %random square matrix
A=A'*A;
[V,D]=eig(A);%find eigenvalue and eigenvector
X=zeros(n); %define the matrix
for i=1:n
X=X+D(i,i)*outprod(V(:,i),V(:,i));
display(X);
display(A');
end
end
Walter Roberson
Walter Roberson 2022-12-15
Your outprod function accepts up to two parameters, V, and D. It then generates a random 3 x 3 matrix and does eigenvalue decomposition on the generated matrix, overwriting any V and D that was passed in. For the moment we will call that generated 3 x 3 matrix as A1.
Then function then loops over the columns of the calculated V matrix, asking to call outprod() on each column against it self. The column content is passed in to outprod.
But outprod is the same function you are defining. So the first thing outprod does is build a new 3 x 3 matrix, which for the moment we will refer to as A2, and overwrite the inputs V and D with the eigenvalue decomposition of the random matrix. In then loops over the columns of V, calling outprod on each one.
But outprod is the same function you are defining. So the first thing outprod does is build a new 3 x 3 matrix, which for the moment we will refer to as A3, and overwrite the inputs V and D with the eigenvalue decomposition of the random matrix. In then loops over the columns of V, calling outprod on each one.
But outprod is the same function you are defining. So the first thing outprod does is build a new 3 x 3 matrix, which for the moment we will refer to as A4, and overwrite the inputs V and D with the eigenvalue decomposition of the random matrix. In then loops over the columns of V, calling outprod on each one.
... and so on.
For any given column, say i = 1, you cannot proceed to the next column until you have finished the work on the current column. But the work on the current column involves invoking outprod, having outprod ignore the inputs and generate a new matrix and loop over its columns. But you can't proceed to column 2 until you get through the work on column 1, which involves calling outprod to generate another new matrix and loop over its columns...
MATLAB will keep having outprod call outprod again and again and again until your reach the maximum recursion limit, which is 500; the code will then fail.
If you must have your code call itself, then your code must be written in a way that there is some certainty that your code will complete some work and return within 500 nested iterations. There are two ways of doing that:
  • have the code detect that the input is trivially small or otherwise has a trivial answer and have it return an answer immediately, and if not have each nested iteration always work on a smaller subset so that you can be sure that you will reach the trivial size within 500 calls; or
  • have the code detect that the input meets some criteria for returning after a trivial calculation, and if not have the have the code transform the input somehow and call nested -- but only do this if you have established some mathematical proof that the desired result will be reached at some stage.
Either way, recursive code should always start with a test to determine whether you have finished, and if so return an output directly, only calling the function again if you are not finished.
Your code fails to test for any termination condition, and so forms an infinite loop.

请先登录,再进行评论。

回答(2 个)

Askic V
Askic V 2022-12-14
First of all, you need to read this:
regarding your actualquestion, it seems to me that you're doing and checking eigendecomposition of a matrix. This xould be checked much simpler:
n = 3;
A = randn(n);
A = A'*A;
[V,D] = eig(A);
X = V*D*V';
display(X)
X = 3×3
2.9408 1.4318 2.8492 1.4318 3.6694 0.5439 2.8492 0.5439 7.8258
display(A)
A = 3×3
2.9408 1.4318 2.8492 1.4318 3.6694 0.5439 2.8492 0.5439 7.8258
  3 个评论
Sam Chak
Sam Chak 2022-12-14
Does not exist in the library. Only you know the code of the function.
help outprod
--- outprod not found. Showing help for dotprod instead. --- DOTPROD Dot product weight function. Weight functions apply weights to an input to get weighted inputs. dotprod(W,P) returns the dot product W * P of a weight matrix W and an input P. Here we define a random weight matrix W and input vector P and calculate the corresponding weighted input Z. W = rand(4,3); P = rand(3,1); Z = dotprod(W,P) See also SIM, DDOTPROD, DIST, NEGDIST, NORMPROD. Documentation for dotprod doc dotprod
Walter Roberson
Walter Roberson 2022-12-14
outer product would not be mathematically correct at that point: inner product is the correct operation.

请先登录,再进行评论。


Askic V
Askic V 2022-12-14
I can only imagine that you're trying to do something like this:
n = 3;
A = randn(n);
A = A'*A;
[V, D] = eig(A);
X = zeros(n);
for i = 1:n
T1 = D(:,i)*V(:,i)';
T2 = V(:,i)*T1(i,:);
X = X+T2;
end
M = V*D*V';
display(X)
X = 3×3
3.1535 -2.3389 0.5956 -2.3389 5.4403 -4.3884 0.5956 -4.3884 6.9882
display(M)
M = 3×3
3.1535 -2.3389 0.5956 -2.3389 5.4403 -4.3884 0.5956 -4.3884 6.9882

类别

Help CenterFile Exchange 中查找有关 Linear Algebra 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by