FOR loop with subs :: NEED HELP
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
채택된 답변
Walter Roberson
2019년 2월 7일
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
Ali Tawfik
2019년 2월 7일
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.
Ali Tawfik
2019년 2월 8일
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
Ali Tawfik
2019년 2월 8일
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.
Ali Tawfik
2019년 2월 8일
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, [])));
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Historical Contests에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
