最简单的原型符号正交矩阵的方法

时间:2012-08-10 10:03:59

标签: matlab math matrix linear-algebra symbolic-math

我有8个罪和余弦,我试图用Matlab象征性地定义如下所示。我的目标是使用所有这些sins和余弦来象征性地构建8x8的矩阵H(累积的Givens旋转矩阵),并最终看到这个H正交投影矩阵的公式是什么。我可以使用下面的代码G7*G6*...*G0*I来做到这一点,其中我是Identity 8x8,Gi是对应于元素的Givens旋转(i:i + 1,i:i + 1)。

c_0 = sym('c_0');
c_1 = sym('c_1');
c_2 = sym('c_2');
c_3 = sym('c_3');
c_4 = sym('c_4');
c_5 = sym('c_5');
c_6 = sym('c_6');
c_7 = sym('c_7');

s_0 = sym('s_0');
s_1 = sym('s_1');
s_2 = sym('s_2');
s_3 = sym('s_3');
s_4 = sym('s_4');
s_5 = sym('s_5');
s_6 = sym('s_6');
s_7 = sym('s_7');

% create H orthogonal matrix using the sin and cos symbols
% filling in the first rotation
I=eye(9,9)
H = I;
H(1:2,1:2) = [c_0 -s_0; s_0 c_0]

% build the 2nd rotation and update H
G = I;
G(2:3,2:3) = [c_1 -s_1; s_1 c_1]
H = G*H

% build the 3rd rotation and update H
G = I;
G(3:4,3:4) = [c_2 -s_2; s_2 c_2]
H = G*H

% build the 4rth rotation and update H
G = I;
G(4:5,4:5) = [c_3 -s_3; s_3 c_3]
H = G*H

% build the 5th rotation and update H
G = I;
G(5:6,5:6) = [c_4 -s_4; s_4 c_4]
H = G*H

% build the 6th rotation and update H
G = I;
G(6:7,6:7) = [c_5 -s_5; s_5 c_5]
H = G*H

% build the 7th rotation and update H
G = I;
G(7:8,7:8) = [c_6 -s_6; s_6 c_6]
H = G*H

% build the 8th rotation and update H
G = I;
G(8:9,8:9) = [c_7 -s_7; s_7 c_7]
H = G*H

代码失败,出现以下错误,无法找到解决方法:

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.

Error in build_rotH_test (line 26)
H(1:2,1:2) = [c_0 -s_0; s_0 c_0]

1 个答案:

答案 0 :(得分:1)

我这样解决了。注意我意识到我需要每个旋转的转置因此我可以构建并应用H'* x即G7'*G6'*...*G0'*I这就是为什么在解决方案中翻转sin符号的原因。

clear all;

% defining 0 and 1 as symbols too, solves the problem
sym_0 = sym('0');
sym_1 = sym('1');

c0 = sym('c0');
c1 = sym('c1');
c2 = sym('c2');
c3 = sym('c3');
c4 = sym('c4');
c5 = sym('c5');
c6 = sym('c6');
c7 = sym('c7');

s0 = sym('s0');
s1 = sym('s1');
s2 = sym('s2');
s3 = sym('s3');
s4 = sym('s4');
s5 = sym('s5');
s6 = sym('s6');
s7 = sym('s7');

% create H orthogonal matrix using the sin and cos symbols
% filling in the first rotation
I = repmat(sym_0,9,9);
for i=1:9
    I(i,i)=sym_1;
end
H = I
H(1:2,1:2) = [c0 s0; -s0 c0]

% build the 2nd rotation and update H
G = I;
G(2:3,2:3) = [c1 s1; -s1 c1]
H = G*H;

% build the 3rd rotation and update H
G = I;
G(3:4,3:4) = [c2 s2; -s2 c2]
H = G*H;

% build the 4rth rotation and update H
G = I;
G(4:5,4:5) = [c3 s3; -s3 c3]
H = G*H;

% build the 5th rotation and update H
G = I;
G(5:6,5:6) = [c4 s4; -s4 c4]
H = G*H;

% build the 6th rotation and update H
G = I;
G(6:7,6:7) = [c5 s5; -s5 c5]
H = G*H;

% build the 7th rotation and update H
G = I;
G(7:8,7:8) = [c6 s6; -s6 c6]
H = G*H;

% build the 8th rotation and update H
G = I;
G(8:9,8:9) = [c7 s7; -s7 c7]
H = G*H

,输出为:

H =

[ 1, 0, 0, 0, 0, 0, 0, 0, 0]
[ 0, 1, 0, 0, 0, 0, 0, 0, 0]
[ 0, 0, 1, 0, 0, 0, 0, 0, 0]
[ 0, 0, 0, 1, 0, 0, 0, 0, 0]
[ 0, 0, 0, 0, 1, 0, 0, 0, 0]
[ 0, 0, 0, 0, 0, 1, 0, 0, 0]
[ 0, 0, 0, 0, 0, 0, 1, 0, 0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0, 1, 0, 0, 0, 0,   0,  0]
[ 0, 0, 0, 1, 0, 0, 0,   0,  0]
[ 0, 0, 0, 0, 1, 0, 0,   0,  0]
[ 0, 0, 0, 0, 0, 1, 0,   0,  0]
[ 0, 0, 0, 0, 0, 0, 1,   0,  0]
[ 0, 0, 0, 0, 0, 0, 0,  c7, s7]
[ 0, 0, 0, 0, 0, 0, 0, -s7, c7]


H =

[                       c0,                       s0,                     0,                  0,               0,            0,         0,      0,  0]
[                   -c1*s0,                    c0*c1,                    s1,                  0,               0,            0,         0,      0,  0]
[                 c2*s0*s1,                -c0*c2*s1,                 c1*c2,                 s2,               0,            0,         0,      0,  0]
[             -c3*s0*s1*s2,              c0*c3*s1*s2,             -c1*c3*s2,              c2*c3,              s3,            0,         0,      0,  0]
[           c4*s0*s1*s2*s3,          -c0*c4*s1*s2*s3,           c1*c4*s2*s3,          -c2*c4*s3,           c3*c4,           s4,         0,      0,  0]
[       -c5*s0*s1*s2*s3*s4,        c0*c5*s1*s2*s3*s4,       -c1*c5*s2*s3*s4,        c2*c5*s3*s4,       -c3*c5*s4,        c4*c5,        s5,      0,  0]
[     c6*s0*s1*s2*s3*s4*s5,    -c0*c6*s1*s2*s3*s4*s5,     c1*c6*s2*s3*s4*s5,    -c2*c6*s3*s4*s5,     c3*c6*s4*s5,    -c4*c6*s5,     c5*c6,     s6,  0]
[ -c7*s0*s1*s2*s3*s4*s5*s6,  c0*c7*s1*s2*s3*s4*s5*s6, -c1*c7*s2*s3*s4*s5*s6,  c2*c7*s3*s4*s5*s6, -c3*c7*s4*s5*s6,  c4*c7*s5*s6, -c5*c7*s6,  c6*c7, s7]
[  s0*s1*s2*s3*s4*s5*s6*s7, -c0*s1*s2*s3*s4*s5*s6*s7,  c1*s2*s3*s4*s5*s6*s7, -c2*s3*s4*s5*s6*s7,  c3*s4*s5*s6*s7, -c4*s5*s6*s7,  c5*s6*s7, -c6*s7, c7]