Please help me...
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
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
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)
display(A)
3 个评论
Sam Chak
2022-12-14
Does not exist in the library. Only you know the code of the function.
help outprod
Walter Roberson
2022-12-14
outer product would not be mathematically correct at that point: inner product is the correct operation.
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)
display(M)
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!