Multiplication of matrices with Multiple variables

33 views (last 30 days)
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 Comments
Raj
Raj on 15 Apr 2019
ok so what exactly is the problem here?
Anush Karki
Anush Karki on 15 Apr 2019
Edited: Anush Karki on 15 Apr 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

Sign in to comment.

Accepted Answer

Raj
Raj on 15 Apr 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 Comments
Raj
Raj on 15 Apr 2019
Edited: Raj on 15 Apr 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!!

Sign in to comment.

More Answers (0)

Categories

Find more on Audio I/O and Waveform Generation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!