计算具有符号变量的矩阵

时间:2018-09-10 22:36:37

标签: matlab matrix double symbolic-math

我有以下MATLAB脚本:

var_theta = sym('theta', [1,7]);
matrix_DH = computeDH(var_theta);
T_matrix = zeros(4,4,7);
for i = 1:7
    T_matrix(:,:,i) = computeT(matrix_DH(i,1), matrix_DH(i,2), matrix_DH(i,3), matrix_DH(i,4));
end

function matrixT = computeT(alpha, d, r, theta)
  matrixT = zeros(4,4);
  matrixT(1,:) = [cos(theta) -1*sin(theta) 0 r]; % first row 
  matrixT(2,:) = [sin(theta)*cos(alpha) cos(theta)*cos(alpha) -sin(alpha) -d*sin(alpha)]; % 2n row  
  matrixT(3,:) = [sin(theta)*sin(alpha) cos(theta)*sin(alpha) cos(alpha) d*cos(alpha)]; % 3rd row  
  matrixT(4,:) = [0 0 0 1]; % last row
end

function DH = computeDH(theta)
   DH = zeros(7,4);
   L = 0.4;
   M = 0.39;
   DH(1,:) = [0.5*pi 0 0 theta(1,1)];
   DH(2,:) = [-0.5*pi 0 0 theta(2)];
   DH(3,:) = [-0.5*pi L 0 theta(3)];
   DH(4,:) = [0.5*pi 0 0 theta(4)];
   DH(5,:) = [0.5*pi M 0 theta(5)];
   DH(6,:) = [-0.5*pi 0 0 theta(6)];
   DH(7,:) = [0 0 0 theta(7)];
end

我想在不评估theta的情况下获得所需的T_matrix数组。我的目标是在获得矩阵后,通过每个θ推导每个位置,以计算雅可比行列式。因此,最后我希望将所得矩阵作为theta的函数。问题是,每当我将符号变量插入矩阵时,它都会说:

The following error occurred converting from sym to double:
Unable to convert expression into double array.

Error in computeT_robotics>computeDH (line 21)
   DH(1,:) = [0.5*pi, 0, 0, theta(1)];

1 个答案:

答案 0 :(得分:0)

正如安东尼所说,包含我的theta的矩阵也必须声明为sym,以便能够保存符号结果。 最终代码:

var_theta = sym('theta', [1,7]);
matrix_DH = computeDH(var_theta);
T_matrix = sym('T_matrix',[4 4 7]);
for i = 1:7
    T_matrix(:,:,i) = computeT(matrix_DH(i,1), matrix_DH(i,2), matrix_DH(i,3), matrix_DH(i,4));
end

function matrixT = computeT(alpha, d, r, theta)
  matrixT = sym('matrixT', [4 4]);
  matrixT(1,:) = [cos(theta) -1*sin(theta) 0 r]; % first row 
  matrixT(2,:) = [sin(theta)*cos(alpha) cos(theta)*cos(alpha) -sin(alpha) -d*sin(alpha)]; % 2n row  
  matrixT(3,:) = [sin(theta)*sin(alpha) cos(theta)*sin(alpha) cos(alpha) d*cos(alpha)]; % 3rd row  
  matrixT(4,:) = [0 0 0 1]; % last row
end

function DH = computeDH(theta)
   DH = sym('DH',[7 4]);
   L = 0.4;
   M = 0.39;
   DH(1,:) = [0.5*pi 0 0 theta(1,1)];
   DH(2,:) = [-0.5*pi 0 0 theta(2)];
   DH(3,:) = [-0.5*pi L 0 theta(3)];
   DH(4,:) = [0.5*pi 0 0 theta(4)];
   DH(5,:) = [0.5*pi M 0 theta(5)];
   DH(6,:) = [-0.5*pi 0 0 theta(6)];
   DH(7,:) = [0 0 0 theta(7)];
end