is there a way to get generalised code to take numerical inputs after a general solution is shown?

조회 수: 1 (최근 30일)
syms th2 d1 L1 d3 L2 ;
a=[0 L2 0];
alp=[90 -90 0]
d=[(L1+d1) 0 d3];
th=[0 th2 0]
T=eye(4);
n= 3 ;
for i=1:n
T =T*[cosd(th(i)) -sind(th(i))*cosd(alp(i)) sind(th(i))*sind(alp(i)) a(i)*cosd(th(i));
sind(th(i)) cosd(th(i))*cosd(alp(i)) -sind(alp(i))*cosd(th(i)) a(i)*sind(th(i));
0 sind(alp(i)) cosd(alp(i)) d(i) ;
0 0 0 1]
end
Tf=T %this will give a generised matrix with mentioned parameters like th1 L1 etc.
I want to put values now for these parameters.... to do so i have to run the same thing again with my inputs , which makes the code big (to an extent) ;
just wanted to know if there is any simmpler method to call above steps in a line or two.
% this is my second part (particular solution) for now
clear Tf i;
th2=0;
d1=50; d3=100;
L2=150; L1=100;
a=[0 L2 0];
alp=[90 -90 0]
d=[(L1+d1) 0 d3];
th=[0 th2 0]
T=eye(4);
n= 3 ;
for i=1:n
T =T*[cosd(th(i)) -sind(th(i))*cosd(alp(i)) sind(th(i))*sind(alp(i)) a(i)*cosd(th(i));
sind(th(i)) cosd(th(i))*cosd(alp(i)) -sind(alp(i))*cosd(th(i)) a(i)*sind(th(i));
0 sind(alp(i)) cosd(alp(i)) d(i) ;
0 0 0 1];
end
Tf=T
%want these part in 1-2 lines !

답변 (2개)

Naveen Venkata Krishnan
Naveen Venkata Krishnan 2019년 10월 9일
Hello Mayank,
As far I understood from the problem statement, you want to calculate a matrix Tf which is a function of th2 d1 L1 d3 L2 (which are your inputs, in this case ). Try converting the code you have given into a function that takes th2 d1 L1 d3 L2 as input and returns Tf as ouput, like as shown.
function Tf = cal(th2,d1,L1,d3,L2)
a=[0 L2 0];
alp=[90 -90 0];
d=[(L1+d1) 0 d3];
th=[0 th2 0];
T=eye(4);
n= 3 ;
for i=1:n
T =T*[cosd(th(i)) -sind(th(i))*cosd(alp(i)) sind(th(i))*sind(alp(i)) a(i)*cosd(th(i));
sind(th(i)) cosd(th(i))*cosd(alp(i)) -sind(alp(i))*cosd(th(i)) a(i)*sind(th(i));
0 sind(alp(i)) cosd(alp(i)) d(i) ;
0 0 0 1];
end
Tf=T;
end
And then
cal(th2,d1,L1,d3,L1) % d1=50; d3=100;L2=150;L1=100;

Steven Lord
Steven Lord 2019년 10월 9일
For this particular problem, you can perform the computations numerically as Naveen Venkata Krishnan suggested. But if you need/want to do everything symbolically, use the subs function to substitute values into the symbolic expression you received at the end of your for loop. You may also need or want to use the vpa or double functions once you've substituted.

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by