Please help me...
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Walter Roberson
il 15 Dic 2022
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.
Risposte (2)
Askic V
il 14 Dic 2022
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 Commenti
Sam Chak
il 14 Dic 2022
Does not exist in the library. Only you know the code of the function.
help outprod
Walter Roberson
il 14 Dic 2022
outer product would not be mathematically correct at that point: inner product is the correct operation.
Askic V
il 14 Dic 2022
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 Commenti
Vedere anche
Categorie
Scopri di più su Linear Algebra in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!