如果输入表达式包含符号变量,请改用VPA函数? [英] If the input expression contains a symbolic variable, use the VPA function instead?
问题描述
我想用eigval
减去对角线值,并将新值存储在矩阵Diagonal
中:
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)=sym(Diagonal);
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
但是因为我使用的是符号变量,所以会出现错误:
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.
推荐答案
以下是显示您相同错误的简单代码,因此也许可以帮助您澄清问题:
Here's a simple bit of code that exhibits your same error so maybe it will help clarify the issue:
syms x; % Create symbolic variable
a1 = rand(2); % Floating point array 1
a2 = rand(2); % Floating point array 2
d = a1(1)-x; % This is now a symbolic expression
a2(1) = d; % Error: you can't store a symbolic expression in a double array
在R2013b(和R2015b)中返回的
which in R2013b (and R2015b) returns
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.
vpa
在这里不能使用,因为x
是尚未定义的符号变量,而不是符号值(vpa(d)
的影响不大).
vpa
can't be used here because x
is a symbolic variable that hasn't been defined rather than a symbolic value (vpa(d)
has an inconsequential effect).
对于您的代码,该错误可能发生在以下行:
For your code, the error likely occurs on this line:
Fix_Diagonal_2(loop2,store)=Diagonal;
您不能使用vpa
,因为eigval
是没有值的符号变量.您可以通过将Fix_Diagonal_2
强制转换为sym
:
You can't use vpa
because eigval
is a symbolic variable with no value. You can possibly solve your problem by casting Fix_Diagonal_2
to sym
:
Fix_Diagonal_2 = sym(Fix_Diagonal_2);
您可能想在for
循环之外执行此操作.我也看不到为什么您要在外循环的每次迭代中重新定义eigval
.
You'll probably want to do that outside of the for
loops. I also don't see why you're redefining eigval
on every iteration of the outer loop.
这篇关于如果输入表达式包含符号变量,请改用VPA函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!