matlab编写程序问题
在做作业,用matlab编写了一段程序,程序如下:[th1,th2]=solve('l2*cos(th1+th2)+l1*cos(th1)=px','l2*sin(th1...
在做作业,用matlab编写了一段程序,程序如下:
[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;
x=[1,2,3,4,5];
y=[10,11,12,13,14];
TH1=zeros(2,5);
TH2=zeros(2,5);
for i=1:5
px(i)=x(i);py=y(i);
TH1(:,i)=subs(th1,{'px(i)','py(i)','9','9'},{'px','py','l1','l2'});
TH2(:,i)=subs(th2,{'px(i)','py(i)','9','9'},{'px','py','l1','l2'});
end
程序的功能就是先第一句求出来的两个方程的符号解,然后逐个读入符号变量的数值,求出数值解结果总是出错,提示??? Conversion to double from sym is not possible.我把里边的subs()函数提出单独进行赋值运算也是不行,虽然运算,但并不是最中的数值结果,而我同学的用的跟我一样的方程符号解,单独用subs()函数进行运算就能得到数值解,而且我的程序在没有添加TH2(:,i)= 那句话之后他电脑上还运行出了一次结果。不过也就出了一次结果,以后总是报错,希望个位高手大侠能给小弟一点帮助啊~~我是个刚注册百度的新手,开始系统只给了25分,虽很想表示诚意,也只能悬赏20分,不好意思,还是希望各位高手不要见怪。。。 展开
[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;
x=[1,2,3,4,5];
y=[10,11,12,13,14];
TH1=zeros(2,5);
TH2=zeros(2,5);
for i=1:5
px(i)=x(i);py=y(i);
TH1(:,i)=subs(th1,{'px(i)','py(i)','9','9'},{'px','py','l1','l2'});
TH2(:,i)=subs(th2,{'px(i)','py(i)','9','9'},{'px','py','l1','l2'});
end
程序的功能就是先第一句求出来的两个方程的符号解,然后逐个读入符号变量的数值,求出数值解结果总是出错,提示??? Conversion to double from sym is not possible.我把里边的subs()函数提出单独进行赋值运算也是不行,虽然运算,但并不是最中的数值结果,而我同学的用的跟我一样的方程符号解,单独用subs()函数进行运算就能得到数值解,而且我的程序在没有添加TH2(:,i)= 那句话之后他电脑上还运行出了一次结果。不过也就出了一次结果,以后总是报错,希望个位高手大侠能给小弟一点帮助啊~~我是个刚注册百度的新手,开始系统只给了25分,虽很想表示诚意,也只能悬赏20分,不好意思,还是希望各位高手不要见怪。。。 展开
2个回答
展开全部
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
[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*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-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
展开全部
修改:
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来转换字符串。
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来转换字符串。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询