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
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