Message Boards Message Boards

How to save matrices from each iteration of i.?

Posted 5 years ago

I am trying to get the transformation matrices from DH parameter table and would like to save each matrix (Ai) getting after each iteration of i. How can I do that?

DH = {{0, T1, 0, 0}, {0, T2, L1, Pi/2}, {0, T3, L2, 0}, {0, 0, L3, 0}} 
For[i = 1, i <= 4, i++, 
A = {
 {Cos[DH[[i, 2]]], -Sin[DH[[i, 2]]], 0, DH[[i, 3]]}, 
 {Sin[DH[[i, 2]]] Cos[DH[[i, 4]]], Cos[DH[[i, 2]]] Cos[DH[[i, 4]]], -Sin[
  DH[[i, 4]]], -Sin[DH[[i, 4]]] DH[[i, 1]]}, 
 {Sin[DH[[i, 2]]] Sin[DH[[i, 4]]], Cos[DH[[i, 2]]] Sin[DH[[i, 4]]], Cos[DH[[i, 4]]], Cos[DH[[i, 4]]] DH[[i, 1]]}, 
 {0, 0, 0, 1}
}; 
Print["A = ", MatrixForm[A]];
]
POSTED BY: Anshuman Singh
6 Replies

I would simply make the dependence of the matrix on i explicit:

a[i_] := {{Cos[DH[[i, 2]]], -Sin[DH[[i, 2]]], 0, 
    DH[[i, 3]]}, {Sin[DH[[i, 2]]] Cos[DH[[i, 4]]], 
    Cos[DH[[i, 2]]] Cos[DH[[i, 4]]], -Sin[
      DH[[i, 4]]], -Sin[DH[[i, 4]]] DH[[i, 1]]}, {Sin[DH[[i, 2]]] Sin[
      DH[[i, 4]]], Cos[DH[[i, 2]]] Sin[DH[[i, 4]]], Cos[DH[[i, 4]]], 
    Cos[DH[[i, 4]]] DH[[i, 1]]}, {0, 0, 0, 1}};
MatrixForm[a[3]]
POSTED BY: Gianluca Gorni

Another option is to get away from For and use Table and return a list of your A's

a = Table[{a expression},{i,1,4}]

Regards

Neil

POSTED BY: Neil Singer
Posted 5 years ago

Hi. Thanks, it worked. However, when I'm trying to take the 4th column of OT4 with OT4[[All, 4]] I am not getting the appropriate result ie the 4th column of OT4.

DH = {{0, T1, 0, 0}, {0, T2, L1, Pi/2}, {0, T3, L2, 0}, {0, 0, L3, 0}}
For[i = 1, i <= 4, i++, 
  a[i_] := {{Cos[DH[[i, 2]]], -Sin[DH[[i, 2]]], 0, 
     DH[[i, 3]]}, {Sin[DH[[i, 2]]] Cos[DH[[i, 4]]], 
     Cos[DH[[i, 2]]] Cos[DH[[i, 4]]], -Sin[
       DH[[i, 4]]], -Sin[DH[[i, 4]]] DH[[i, 1]]}, {Sin[
       DH[[i, 2]]] Sin[DH[[i, 4]]], Cos[DH[[i, 2]]] Sin[DH[[i, 4]]], 
     Cos[DH[[i, 4]]], Cos[DH[[i, 4]]] DH[[i, 1]]}, {0, 0, 0, 1}}];
Print["A1 = ", MatrixForm[a[1]]];
Print["A2 = ", MatrixForm[a[2]]];
Print["A3 = ", MatrixForm[a[3]]];
Print["A4 = ", MatrixForm[a[4]]];
Print["0T3 = ", Simplify[MatrixForm[a[1].a[2].a[3]]]];
OT4 = Simplify[MatrixForm[a[1].a[2].a[3].a[4]]];
Print["0T4 = ", MatrixForm[OT4]];
Print["0P4 = ", MatrixForm[OT4[[All, 4]]]];
POSTED BY: Anshuman Singh

MatrixForm is for display only. You should not use it for assignments. Delete matrixform and it will work.

POSTED BY: Neil Singer
Posted 5 years ago

Okay it worked, however, when I'm trying to take the partial derivative of OP4(1,1) with respect to T1, I'm only getting -Sin[T1]. I should be getting -(L1+L2 Cos[T2]+L3 Cos[T2+T3]) Sin[T1] instead.

DH = {{0, T1, 0, 0}, {0, T2, L1, Pi/2}, {0, T3, L2, 0}, {0, 0, L3, 0}}
For[i = 1, i <= 4, i++, 
  a[i_] := {{Cos[DH[[i, 2]]], -Sin[DH[[i, 2]]], 0, 
     DH[[i, 3]]}, {Sin[DH[[i, 2]]] Cos[DH[[i, 4]]], 
     Cos[DH[[i, 2]]] Cos[DH[[i, 4]]], -Sin[
       DH[[i, 4]]], -Sin[DH[[i, 4]]] DH[[i, 1]]}, {Sin[
       DH[[i, 2]]] Sin[DH[[i, 4]]], Cos[DH[[i, 2]]] Sin[DH[[i, 4]]], 
     Cos[DH[[i, 4]]], Cos[DH[[i, 4]]] DH[[i, 1]]}, {0, 0, 0, 1}}];
Print["OT1 = ", MatrixForm[a[1]]];
Print["1T2 = ", MatrixForm[a[2]]];
Print["2T3 = ", MatrixForm[a[3]]];
Print["3T4 = ", MatrixForm[a[4]]];
Print["0T3 = ", Simplify[MatrixForm[a[1].a[2].a[3]]]];
OT4 = Simplify[a[1].a[2].a[3].a[4]];
Print["0T4 = ", MatrixForm[OT4]];
OP4 = OT4[[All, 4]];
Print["0P4 = ", MatrixForm[OP4]];
J0trans = Table[0, {3}, {3}] ;
For[i = 1, i <= 3, i++,
  J0trans[[i, 1]] = D[Part[OP4, i, 1],T1]
  ];

For[i = 1, i <= 3, i++,
  J0trans[[i, 2]] = D[Part[OP4, i, 1], T2]
  ];

For[i = 1, i <= 3, i++,
  J0trans[[i, 3]] = D[Part[OP4, i, 1], T3]
  ];
Print["J0trans = ", MatrixForm[J0trans]];
POSTED BY: Anshuman Singh

Your OP4 is a vector, not a matrix. With OP4[[1,1]] you extract a subexpression of the first element. The derivative of the first element is D[OP4[[1]], T1].

POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract