3

I have the following problem:

I want to multiply two matrices and sum them to another matrix C. A.B+ 2B Easy! The matrix A varies as a function of three inputs a,b and c. The second matrix, B, is a 2x2 identity matrix.

here is the code

     Grid[{{"a", "b", "c"}, 
      {InputField[Dynamic[a], FieldSize -> 1], 
       InputField[Dynamic[b], FieldSize -> 1], 
       InputField[Dynamic[c], FieldSize -> 1]}}]

        A = {{Dynamic[a]*Dynamic[b], 0}, {0, Dynamic[c]}};
        B = IdentityMatrix[2];
        A.B + 2 B

Let's say a=c=10 and b=2.

Result

But I don't want to get 10 2+2 and 10+2, I need 22 and 12! It just shows the value of a and c without multiplying.

Using

A = {{Dynamic[a*b], 0}, {0, Dynamic[c]}};

instead of

A = {{Dynamic[a]*Dynamic[b], 0}, {0, Dynamic[c]}};

solves the first problem. I got 20+2 and 10+2, which is good but not enough.

It seems that it not possible to simplify operation when using Dynamic... How can I solve the problem? Thx!

UserK
  • 133
  • 5

2 Answers2

8

The Dynamic should wrap the expresion you want to display. In your case it's the last line:

Clear[a,b,c];
Grid[{{"a", "b", "c"}, {InputField[Dynamic[a], FieldSize -> 2], 
   InputField[Dynamic[b], FieldSize -> 2], 
   InputField[Dynamic[c], FieldSize -> 2]}}]

A = {{a*b, 0}, {0, c}};
B = IdentityMatrix[2];
Dynamic[A.B + 2 B]
Jens
  • 97,245
  • 7
  • 213
  • 499
0

Thank you for the answer. I've applied the solution to the real problem and it worked! Now it will be easier to find errors in that!

Avz[\[Theta]z_, 
   dz_] := {{Cos[\[Theta]z], -Sin[\[Theta]z], 0, 0}, {Sin[\[Theta]z], 
    Cos[\[Theta]z], 0, 0}, {0, 0, 1, dz}, {0, 0, 0, 1}};

Avx[\[Theta]x_, 
   dx_] := {{1, 0, 0, dx}, {0, Cos[\[Theta]x], -Sin[\[Theta]x], 
    0}, {0, Sin[\[Theta]x], Cos[\[Theta]x], 0}, {0, 0, 0, 1}};

Linea[tz_, dz_, tx_, dx_] := Avz[tz, dz].Avx[tx, dx]; 

    Print[Grid[     
         {
        {"", "Theta", "d", "Alpha", "a"},
        {"Link1", InputField[Dynamic[t1], FieldSize -> 3], 
    InputField[Dynamic[d1], FieldSize -> 2], 
    InputField[Dynamic[alpha1], FieldSize -> 5], 
    InputField[Dynamic[x1], FieldSize -> 2]},
        {"Link2", InputField[Dynamic[t2], FieldSize -> 3], 
    InputField[Dynamic[d2], FieldSize -> 2], 
    InputField[Dynamic[alpha2], FieldSize -> 5], 
    InputField[Dynamic[x2], FieldSize -> 2]},
        {"Link3", InputField[Dynamic[t3], FieldSize -> 3], 
    InputField[Dynamic[d3], FieldSize -> 2], 
    InputField[Dynamic[alpha3], FieldSize -> 5], 
    InputField[Dynamic[x3], FieldSize -> 2]},
        {"Link4", InputField[Dynamic[t4], FieldSize -> 3], 
    InputField[Dynamic[d4], FieldSize -> 2], 
    InputField[Dynamic[alpha4], FieldSize -> 5], 
    InputField[Dynamic[x4], FieldSize -> 2]}
        }, Frame -> Darker[Gray, .6], Alignment -> {{Left, {Center}}}]
    ];

Print[Rcin = 
  MatrixForm[
   Dynamic[Linea[t1,d1,alpha1,x1].Linea[t2,d2,alpha2,x2].Linea[t3,d3,alpha3,x3].Linea[t4,d4,alpha4,x4]]]];

Cheers mate!

UserK
  • 133
  • 5