Multiplication of matrices with Multiple variables

I have 4 matrices, each with 4 different variables. i.e. Matrix 1 with variable x, 2 with y and so on. How do I multiply them for different values of each variable?
T1 = [cos(th1) 0 -sin(th1) 0; sin(th1) 0 -cos(th1) 0; 0 -1 0 0; 0 0 0 1];
T2 = [cos(th2) -sin(th2) 0 (260*cos(th2)); sin(th2) cos(th2) 0 (260*sin(th2)) ; 0 0 1 0; 0 0 0 1];
T3 = [cos(th3) 0 sin(th3) (-30*cos(th3)); sin(th3) 0 -cos(th3) (-30*sin(th3)) ; 0 1 0 0; 0 0 0 1];
T4 = [cos(th4) 0 sin(th4) 0; sin(th4) 0 -cos(th4) 0; 0 1 0 270; 0 0 0 1];
T5 = [cos(th5) 0 sin(th5) 0; sin(th5) 0 -cos(th5) 0; 0 1 0 0; 0 0 0 1];
T6 = [cos(th6) -sin(th6) 0 0; sin(th6) cos(th6) 0 0; 0 0 1 90; 0 0 0 1];
T = T1 * T2 * T3 * T4 * T5 * T6;
%case I
th1 = 0;
th2 = 0;
th3 = 0;
th4 = 0;
th5 = 0;
th6 = 0;
display("Case 1");
display(T);
%case II
th1 = pi/2;
th2 = 0;
th3 = pi;
th4 = 0;
th5 = 0;
th6 = 0;
display("Case 2");
display(T);
Returns the same value

4 commentaires

Raj
Raj le 15 Avr 2019
Your problem statement is not clear. Can you elaborate possibly with data?
Eg:
M1 = [ 0 0 1 x]
M2 = [0 1 0 y]
M3 = [0 0 1 z]
M4 = [ 1 1 1 w]
calculate M5= M1*M2*M3*M4 for 2 cases:
  1. x = 1, y=2, z=3, w=4;
  2. x = 0, y = 3, z = 3, w =5.
Raj
Raj le 15 Avr 2019
ok so what exactly is the problem here?
Anush Karki
Anush Karki le 15 Avr 2019
Modifié(e) : Anush Karki le 15 Avr 2019
T1 = [cos(th1) 0 -sin(th1) 0; sin(th1) 0 -cos(th1) 0; 0 -1 0 0; 0 0 0 1];
T2 = [cos(th2) -sin(th2) 0 (260*cos(th2)); sin(th2) cos(th2) 0 (260*sin(th2)) ; 0 0 1 0; 0 0 0 1];
T3 = [cos(th3) 0 sin(th3) (-30*cos(th3)); sin(th3) 0 -cos(th3) (-30*sin(th3)) ; 0 1 0 0; 0 0 0 1];
T4 = [cos(th4) 0 sin(th4) 0; sin(th4) 0 -cos(th4) 0; 0 1 0 270; 0 0 0 1];
T5 = [cos(th5) 0 sin(th5) 0; sin(th5) 0 -cos(th5) 0; 0 1 0 0; 0 0 0 1];
T6 = [cos(th6) -sin(th6) 0 0; sin(th6) cos(th6) 0 0; 0 0 1 90; 0 0 0 1];
T = T1 * T2 * T3 * T4 * T5 * T6;
%case I
th1 = 0;
th2 = 0;
th3 = 0;
th4 = 0;
th5 = 0;
th6 = 0;
display("Case 1");
display(T);
%case II
th1 = pi/2;
th2 = 0;
th3 = pi;
th4 = 0;
th5 = 0;
th6 = 0;
display("Case 2");
display(T);
Both the time command Returns the same value

Connectez-vous pour commenter.

 Réponse acceptée

Raj
Raj le 15 Avr 2019
Not sure how you are getting the 'same value'. Your code as it is will not run in the first place.
Try this:
%case I
th1 = 0;
th2 = 0;
th3 = 0;
th4 = 0;
th5 = 0;
th6 = 0;
display('Case 1');
T1 = [cos(th1) 0 -sin(th1) 0; sin(th1) 0 -cos(th1) 0; 0 -1 0 0; 0 0 0 1];
T2 = [cos(th2) -sin(th2) 0 (260*cos(th2)); sin(th2) cos(th2) 0 (260*sin(th2)) ; 0 0 1 0; 0 0 0 1];
T3 = [cos(th3) 0 sin(th3) (-30*cos(th3)); sin(th3) 0 -cos(th3) (-30*sin(th3)) ; 0 1 0 0; 0 0 0 1];
T4 = [cos(th4) 0 sin(th4) 0; sin(th4) 0 -cos(th4) 0; 0 1 0 270; 0 0 0 1];
T5 = [cos(th5) 0 sin(th5) 0; sin(th5) 0 -cos(th5) 0; 0 1 0 0; 0 0 0 1];
T6 = [cos(th6) -sin(th6) 0 0; sin(th6) cos(th6) 0 0; 0 0 1 90; 0 0 0 1];
T = T1 * T2 * T3 * T4 * T5 * T6;
display(T);
%case II
th1 = pi/2;
th2 = 0;
th3 = pi;
th4 = 0;
th5 = 0;
th6 = 0;
display('Case 2');
T1 = [cos(th1) 0 -sin(th1) 0; sin(th1) 0 -cos(th1) 0; 0 -1 0 0; 0 0 0 1];
T2 = [cos(th2) -sin(th2) 0 (260*cos(th2)); sin(th2) cos(th2) 0 (260*sin(th2)) ; 0 0 1 0; 0 0 0 1];
T3 = [cos(th3) 0 sin(th3) (-30*cos(th3)); sin(th3) 0 -cos(th3) (-30*sin(th3)) ; 0 1 0 0; 0 0 0 1];
T4 = [cos(th4) 0 sin(th4) 0; sin(th4) 0 -cos(th4) 0; 0 1 0 270; 0 0 0 1];
T5 = [cos(th5) 0 sin(th5) 0; sin(th5) 0 -cos(th5) 0; 0 1 0 0; 0 0 0 1];
T6 = [cos(th6) -sin(th6) 0 0; sin(th6) cos(th6) 0 0; 0 0 1 90; 0 0 0 1];
T = T1 * T2 * T3 * T4 * T5 * T6;
display(T);
Works fine. If you want an optimized code and dont want to repeat the T1 to T6 equations, you can write them in a function and just call the function multiple times.

3 commentaires

Thank you for your reply. I have already gotten the answer using the method you suggested. I actually want an optimised code. I am new to MATLAB. How do write them in function? I tried writing them in function but have no idea regarding the code to call them.
Raj
Raj le 15 Avr 2019
Modifié(e) : Raj le 15 Avr 2019
Open a new script and copy this:
function T= calculation(th1,th2,th3,th4,th5,th6)
T1 = [cos(th1) 0 -sin(th1) 0; sin(th1) 0 -cos(th1) 0; 0 -1 0 0; 0 0 0 1];
T2 = [cos(th2) -sin(th2) 0 (260*cos(th2)); sin(th2) cos(th2) 0 (260*sin(th2)) ; 0 0 1 0; 0 0 0 1];
T3 = [cos(th3) 0 sin(th3) (-30*cos(th3)); sin(th3) 0 -cos(th3) (-30*sin(th3)) ; 0 1 0 0; 0 0 0 1];
T4 = [cos(th4) 0 sin(th4) 0; sin(th4) 0 -cos(th4) 0; 0 1 0 270; 0 0 0 1];
T5 = [cos(th5) 0 sin(th5) 0; sin(th5) 0 -cos(th5) 0; 0 1 0 0; 0 0 0 1];
T6 = [cos(th6) -sin(th6) 0 0; sin(th6) cos(th6) 0 0; 0 0 1 90; 0 0 0 1];
T = T1 * T2 * T3 * T4 * T5 * T6;
end
Now save it with lets say 'calculation' itself as file name. This becomes your function.
Open a new script and copy this:
%case I
th1 = 0;
th2 = 0;
th3 = 0;
th4 = 0;
th5 = 0;
th6 = 0;
display('Case 1');
T = calculation(th1,th2,th3,th4,th5,th6);
display(T);
%case II
th1 = pi/2;
th2 = 0;
th3 = pi;
th4 = 0;
th5 = 0;
th6 = 0;
display('Case 2');
T = calculation(th1,th2,th3,th4,th5,th6);
display(T);
Now save this and run. This becomes your script which calls the "calculation" function.
Also read about basics of functions from here and here. Pretty useful stuff!!
Thank you very much.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Software Development dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by