create a loop then solve system of equations
5 views (last 30 days)
Show older comments
I will explain what i want then i will show my code which is not work as what i want
I have a matrix A given,
n=size(A,1) %l let say n=3
esu=triu(A,1)
[row,col,v]=find(esu) % to spicify only the nonzeros element in the strictly upper diagonal
m=size(v,1)
and I want to define new n×n matrix E_ij such that all elements in E_ij equal zeros except at the position (i,j) and the position(j,i) equal to 1/sqrt(2)
Then I want to apply that each value of v at the position k is equal to trace (E_ij* L(X) )where L(X) lets call it M is n×n matrix with unknown elements
note : X is n-1×n-1 unknown matrix and this is my goal to find it
at the end I want to solve these equations togethor
so for example :
if v=[3 ;2]
so i will have two equations:
v(1)= trace (E_12*M)
thus , 3=trace([0 1/sqrt(2) 0; 1/sqrt(2) 0 0; 0 0 0]*[m11 m12 m13;m21 m22 m23; m31 m32 m33])
similarly
v(2)= trace (E_13*M)
thus 2=trace([0 0 1/sqrt(2) ; 0 0 0; 1/sqrt(2) 0 0]*[m11 m12 m13;m21 m22 m23; m31 m32 m33])
A=[0 1 0;1 0 2; 0 2 0]
n= size(A,1)
E = zeros(n,n);
E(i,j) = 1/sqrt(2);
E(j,i) = 1/sqrt(2);
esu=triu(A,1);
[row,col,v]=find(esu); % corresp. subscripts
m=size(v,1);
A= zeros(m, n^2)
for k = 1:m
[i,j] = deal(row(k), col(k))
E = zeros(n,n)
E(i,j) = 1/sqrt(2)
E(j,i) = 1/sqrt(2)
M = trace(E*L(X))-v(k)==0
M*x=b
M(k,:) = reshape(E, 1, [])
end
b = v
x = pinv(M) * b
X = reshape(x, n, n)
0 Comments
Answers (1)
Shushant
on 14 Mar 2023
According to my understanding, you want to find an Unknown Matrix "X" of size "nxn" using a set of equations which are stored in "M". To accomplish this, I suggest that you use "syms" to solve the system of equations.
Here is the documentation for "syms" and documentation for solving system of equations.
Below is a sample code which might give you some idea on how to use syms and solve the system of equations .
A=[0 1 0;1 0 2; 0 2 0];
n= size(A,1);
syms X [n,n];
esu=triu(A,1);
[row,col,v]=find(esu); % corresp. subscripts
m=size(v,1);
A = zeros(m, n^2);
for k = 1:m
[i,j] = deal(row(k), col(k));
E = zeros(n,n);
E(i,j) = 1/sqrt(2);
E(j,i) = 1/sqrt(2);
M(k) = trace(E*X)==v(k);
end
sol = solve([M(1:k)], X);
x = struct2cell(sol);
x = double(vertcat(x{:}));
x = reshape(x, n, n)
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!