matlab编写程序问题

2024-12-02 14:20:39
推荐回答(2个)
回答1:

clc;clear
[th1,th2]=solve('l2*cos(th1+th2)+l1*cos(th1)=px','l2*sin(th1+th2)+l1*sin(th1)=px','th1','th2')
l1=9;l2=9;
%px=[1,2,3,4,5];
%py=[10,11,12,13,14];
for px=[1,2,3,4,5]
TH1(:,px)=subs(th1);
TH2(:,px)=subs(th2);
end
TH1,TH2

结果:
th1 =

atan(1/2*(1/2*l1^2+px^2-1/2*l2^2-1/2*(-l1^4+4*px^2*l1^2+2*l1^2*l2^2-4*px^4+4*px^2*l2^2-l2^4)^(1/2))/px/l1,(1/4*l1^2+1/2*px^2-1/4*l2^2+1/4*(-l1^4+4*px^2*l1^2+2*l1^2*l2^2-4*px^4+4*px^2*l2^2-l2^4)^(1/2))/l1/px)
atan(1/2*(1/2*l1^2+px^2-1/2*l2^2+1/2*(-l1^4+4*px^2*l1^2+2*l1^2*l2^2-4*px^4+4*px^2*l2^2-l2^4)^(1/2))/px/l1,(1/4*l1^2+1/2*px^2-1/4*l2^2-1/4*(-l1^4+4*px^2*l1^2+2*l1^2*l2^2-4*px^4+4*px^2*l2^2-l2^4)^(1/2))/l1/px)

th2 =

atan(1/4*(4*(1/4*l1^2+1/2*px^2-1/4*l2^2+1/4*(-l1^4+4*px^2*l1^2+2*l1^2*l2^2-4*px^4+4*px^2*l2^2-l2^4)^(1/2))*px^2-2*(1/4*l1^2+1/2*px^2-1/4*l2^2+1/4*(-l1^4+4*px^2*l1^2+2*l1^2*l2^2-4*px^4+4*px^2*l2^2-l2^4)^(1/2))*l2^2+2*(1/4*l1^2+1/2*px^2-1/4*l2^2+1/4*(-l1^4+4*px^2*l1^2+2*l1^2*l2^2-4*px^4+4*px^2*l2^2-l2^4)^(1/2))*l1^2-l1^4+4*px^2*l2^2-l2^4-4*px^4+2*l1^2*l2^2)/l1/l2/(1/4*l1^2+1/2*px^2-1/4*l2^2+1/4*(-l1^4+4*px^2*l1^2+2*l1^2*l2^2-4*px^4+4*px^2*l2^2-l2^4)^(1/2)),-1/2*(-2*px^2+l2^2+l1^2)/l2/l1)
atan(1/4*(4*(1/4*l1^2+1/2*px^2-1/4*l2^2-1/4*(-l1^4+4*px^2*l1^2+2*l1^2*l2^2-4*px^4+4*px^2*l2^2-l2^4)^(1/2))*px^2-2*(1/4*l1^2+1/2*px^2-1/4*l2^2-1/4*(-l1^4+4*px^2*l1^2+2*l1^2*l2^2-4*px^4+4*px^2*l2^2-l2^4)^(1/2))*l2^2+2*(1/4*l1^2+1/2*px^2-1/4*l2^2-1/4*(-l1^4+4*px^2*l1^2+2*l1^2*l2^2-4*px^4+4*px^2*l2^2-l2^4)^(1/2))*l1^2-l1^4+4*px^2*l2^2-l2^4-4*px^4+2*l1^2*l2^2)/l1/l2/(1/4*l1^2+1/2*px^2-1/4*l2^2-1/4*(-l1^4+4*px^2*l1^2+2*l1^2*l2^2-4*px^4+4*px^2*l2^2-l2^4)^(1/2)),-1/2*(-2*px^2+l2^2+l1^2)/l2/l1)

TH1 =

-0.7067 -0.6276 -0.5475 -0.4657 -0.3817
2.2775 2.1984 2.1183 2.0365 1.9525

TH2 =

2.9843 2.8260 2.6657 2.5022 2.3342
-2.9843 -2.8260 -2.6657 -2.5022 -2.3342

回答2:

修改:
TH1(:,i)=ischar( subs(th1,{'px(i)','py(i)','9','9'},{'px','py','l1','l2'}));
TH2(:,i)=ischar( subs(th2,{'px(i)','py(i)','9','9'},{'px','py','l1','l2'}));

因为subs是一个符号函数,它返回一个符号表达式。尽管看似数字,实质上是一个符号常数。为了得到数字,最后我们需要使用函数ischar来转换字符串。