FOR loop with subs :: NEED HELP

Hi All,
I've been trying to implement a for loop to obtain different values of the following matrix (T_x_new), it's working perfect till obtaining normal T_x_new, so I just would like to add different theta for T_x_new to check different values but it's not working
Would any one mind helping me to run this code:
clear all;
syms l1 m1 n1 l2 m2 n2 l3 m3 n3 theta real;
T_x= [l1^2 m1^2 n1^2 2*m1*n1 2*l1*n1 2*l1*m1;
l2^2 m2^2 n2^2 2*m2*n2 2*l2*n2 2*l2*m2;
l3^2 m3^2 n3^2 2*m3*n3 2*l3*n3 2*l3*m3;
l2*l3 m2*m3 n2*n3 m2*n3+n2*m3 l2*n3+n2*l3 l2*m3+m2*l3;
l1*l3 m1*m3 n1*n3 m1*n3+n1*m3 l1*n3+n1*l3 l1*m3+m1*l3;
l1*l2 m1*m2 n1*n2 m1*n2+n1*m2 l1*n2+n1*l2 l1*m2+m1*l2];
T_x_new=subs(T_x,{l1,m1,n1,l2,m2,n2,l3,m3,n3},{1,0,0,0,cos(theta),sin(theta),0,-sin(theta),cos(theta)});
for i=45:90;
theta=i*(pi/180);
T_x_new(:,:,i)
end

 Accepted Answer

for i=45:90;
theta=i*(pi/180);
subs(T_x_new)
end
This will produce 46 matrices of output, so you may wish to reconsider using 45:90 rather than (say) 45:15:90

8 Comments

Thanks, Walter it's working, but I wanna solve the matrix, I mean still the output written cos() and sin().... I wanna final answer with the theta angle also, with degrees, I mean I can remove (pi/180) but I got an error if I wrote cosd()....
Looking to hearing your answer !
Thanks again,
syms l1 m1 n1 l2 m2 n2 l3 m3 n3 theta real
syms pi
Cosd = @(theta) cos(theta*pi/180);
Sind = @(theta) sin(theta*pi/180);
T_x= [l1^2 m1^2 n1^2 2*m1*n1 2*l1*n1 2*l1*m1;
l2^2 m2^2 n2^2 2*m2*n2 2*l2*n2 2*l2*m2;
l3^2 m3^2 n3^2 2*m3*n3 2*l3*n3 2*l3*m3;
l2*l3 m2*m3 n2*n3 m2*n3+n2*m3 l2*n3+n2*l3 l2*m3+m2*l3;
l1*l3 m1*m3 n1*n3 m1*n3+n1*m3 l1*n3+n1*l3 l1*m3+m1*l3;
l1*l2 m1*m2 n1*n2 m1*n2+n1*m2 l1*n2+n1*l2 l1*m2+m1*l2];
T_x_new=subs(T_x, {l1,m1,n1,l2,m2,n2,l3,m3,n3}, {1, 0, 0, 0, Cosd(theta), Sind(theta), 0, -Sind(theta), Cosd(theta)});
for Theta = 45 : 90
subs(T_x_new, theta, Theta)
end
You need to define your own sind and cosd functions because the MATLAB sind and cosd are for numeric inputs only.
... but I am going to guess that what you mean is that you want
for Theta = 45 : 90
double( subs(T_x_new, theta, Theta) )
end
These will only be decimal approximations of the answers.
Thanks again... how smart you're .. I just do not understand excatly what do you mean by defining my own sind and cosd functions, `I thought the following two lines for that purpose
Cosd = @(theta) cos(theta*pi/180);
Sind = @(theta) sin(theta*pi/180);
May I ask you what shall I do if I wanna get the accurate answer with cosd and sind ?
Thank again
What I am also trying to say: how it work normally when I calculate cosd(45) or something like that ?
cosd(45) works. But
syms Theta
subs( cosd(Theta), Theta, 45 )
does not work. This is because cosd is not defined for symbolic inputs.
If you must use cosd for some reason you will need to loop:
syms l1 m1 n1 l2 m2 n2 l3 m3 n3 theta real;
T_x= [l1^2 m1^2 n1^2 2*m1*n1 2*l1*n1 2*l1*m1;
l2^2 m2^2 n2^2 2*m2*n2 2*l2*n2 2*l2*m2;
l3^2 m3^2 n3^2 2*m3*n3 2*l3*n3 2*l3*m3;
l2*l3 m2*m3 n2*n3 m2*n3+n2*m3 l2*n3+n2*l3 l2*m3+m2*l3;
l1*l3 m1*m3 n1*n3 m1*n3+n1*m3 l1*n3+n1*l3 l1*m3+m1*l3;
l1*l2 m1*m2 n1*n2 m1*n2+n1*m2 l1*n2+n1*l2 l1*m2+m1*l2];
for theta = 45:90;
T_x_new = subs(T_x, {l1,m1,n1,l2,m2,n2,l3,m3,n3}, {1,0,0,0,cosd(theta),sind(theta),0,-sind(theta),cosd(theta)})
end
This evaluates cosd() and sind() with numeric inputs, giving floating point results, and then substitutes those floating point results into T_x after converting the floating point into algebraic numbers. You will get out results such as
[ 1, 0, 0, 0, 0, 0]
[ 0, 9860332038599659652432850501889/1298074214633706907132624082305024, 20128341915548551236140856573601/20282409603651670423947251286016, 14088013865473437438305199633233/81129638414606681695789005144064, 0, 0]
[ 0, 20128341915548551236140856573601/20282409603651670423947251286016, 9860332038599659652432850501889/1298074214633706907132624082305024, -14088013865473437438305199633233/81129638414606681695789005144064, 0, 0]
[ 0, -14088013865473437438305199633233/162259276829213363391578010288128, 14088013865473437438305199633233/162259276829213363391578010288128, -1278353550556507619460581970208575/1298074214633706907132624082305024, 0, 0]
[ 0, 0, 0, 0, 3140116564492417/36028797018963968, -4486462071114449/4503599627370496]
[ 0, 0, 0, 0, 4486462071114449/4503599627370496, 3140116564492417/36028797018963968]
I understood what you mean by cosd not defined for symbolic inputs. Therefore, I will explain you what excatly I am looking for:
  • First to obtain the transformation matrix with symbloic inputs (T_x_new) (already done)
  • Then I wanna a final result with different angles (in degree ) variation (e.g. 0, 45, 30, 20, and 90).
  • So may you have a look about the following code, my result should be a normal 6*6,but I have no idea why it's 18*6
syms l1 m1 n1 l2 m2 n2 l3 m3 n3 theta real
syms pi
Cosd = @(theta) cos(theta*pi/180); % May I ask, is it a function or ?
Sind = @(theta) sin(theta*pi/180);
T_x= [l1^2 m1^2 n1^2 2*m1*n1 2*l1*n1 2*l1*m1;
l2^2 m2^2 n2^2 2*m2*n2 2*l2*n2 2*l2*m2;
l3^2 m3^2 n3^2 2*m3*n3 2*l3*n3 2*l3*m3;
l2*l3 m2*m3 n2*n3 m2*n3+n2*m3 l2*n3+n2*l3 l2*m3+m2*l3;
l1*l3 m1*m3 n1*n3 m1*n3+n1*m3 l1*n3+n1*l3 l1*m3+m1*l3;
l1*l2 m1*m2 n1*n2 m1*n2+n1*m2 l1*n2+n1*l2 l1*m2+m1*l2];
T_x_new=subs(T_x, {l1,m1,n1,l2,m2,n2,l3,m3,n3}, {1, 0, 0, 0, Cosd(theta), Sind(theta), 0, -Sind(theta), Cosd(theta)});
angle(1,:)=0;
angle(2,:)=45;
angle(3,:)=90;
num=size(angle,1);
for i=1:num;
w(:,:,i)=double( subs(T_x_new, theta, angle) );
% I wanna here, to sub. every theta with 0, 45, and 90.. and wanna 6*6 % matrix (w) for each trial.. Hope you understand what I mean now
end
But anyway, thank you so much.. I really appericate your kind help.. & hope you can find a solution for what I mentioned.
Maybe I have to add angle(i)?, and I think I can get the result without the problem of sind and cosd..
angle = [0 45 30 20 90];
num = length(angle);
for i=1:num;
w(:,:,i)=double( subs(T_x_new, theta, angle(i)) );
end
Or alternately,
angle = [0 45 30 20 90];
w = double(subs(T_x_new, theta, reshape(angle, 1, 1, [])));

Sign in to comment.

More Answers (0)

Categories

Find more on Historical Contests 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!