MATLAB Answers

URGENT!!! Matlab Symbolic Variable Error???

2 views (last 30 days)
Tanya
Tanya on 17 Feb 2014
Commented: Walter Roberson on 9 Mar 2014
Im tring to substract the diagonal values with eigval and store the new value in the matrix Diagonal :
CovarianceMatrix=[8 -3 1;2 1 0;3 4 5];
Col=3;
Row=3;
store=1;
syms eigval;
for loop1= Col:-1:1
Rw=1;
syms eigval;
for loop2= 1:Row
if Rw==loop1
Diagonal= (CovarianceMatrix(Rw,loop1)-eigval);
Fix_Diagonal_2(loop2,store)=Diagonal;
%Fix_Diagonal_2(loop2,store)=sym(Diagonal); % I already test it
else
Diagonal= CovarianceMatrix(Rw,loop1);
Fix_Diagonal_2(loop2,store)=Diagonal;
end
Rw=Rw+1;
loop1=loop1-1;
if loop1==0
loop1=3;
end
end
store=store+1;
end
That is a part of my program to calculate the eigen value from the matrix. And that code using to keep the second diagonal (det=1st diag- 2nd diag) and then multiply and sum all of the value. So it will be use to be substracted with First Diag Value to get the eigen Value.
But because I'm using a symbolic variable it gives an error:
The following error occurred converting from sym to double:
Error using mupadmex
Error in MuPAD command: DOUBLE cannot convert the input expression into a double
array.
If the input expression contains a symbolic variable, use the VPA function
instead.
How can I solve this? I would like to copy the new substracted value into the diagonal matrix.

Answers (1)

Matt Tearle
Matt Tearle on 17 Feb 2014
The reason you're getting the error is because one branch of the if construct defines an element of Diagonal directly from CovarianceMatrix, which is a double, but the other branch defines an element of Diagonal from the difference of CovarianceMatrix and eigval. Because eigval is a symbolic variable, the result of this calculation is a symbolic variable as well. Consequently, you're trying to assign a symbolic variable as an element of a double array. That's a type mismatch.
I don't know if this is how you want to proceed, but a simple fix is to make CovarianceMatrix a symbolic variable from the outset:
CovarianceMatrix=sym([8 -3 1;2 1 0;3 4 5]);

Community Treasure Hunt

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

Start Hunting!