matlab单摆运动模拟程序?可以反映物体运动规律的程序!
展开全部
l=input('输入摆长:');
theta=input('输入初始角度:');
tfinal=input('输入仿真时间:');
m=10;
g=9.8;
theta=theta*pi/180;
y0=[theta,0];
[t,y]=ode45('danbai',[0:0.01:tfinal],y0,[],l,g);
theta2=-g/l*sin(y(:,1));
y=[y,theta2];
x1=l*sin(y(:,1));
y1=-l*cos(y(:,1));
% y2=y(:,2);
fx=m*l*cos(y(:,1)).*y(:,3)-m*l*sin(y(:,1)).*y(:,2).^2;
fy=m*l*cos(y(:,1)).*y(:,2).^2+m*l*sin(y(:,1)).*y(:,3)+m*g;
% f=sqrt(fx.^2+fy.^2);
h=figure('units','normalized');
axis([-l l,-l,0]);
axis equal;
axis off;
h1=uicontrol('parent',h,'units','normalized','style','text','position',[.15 .81 .2 .07],'string','fx','fontsize',16);
h2=uicontrol('parent',h,'units','normalized','style','text','position',[.65 .81 .2 .07],'string','fy','fontsize',16);
h3=uicontrol('parent',h,'units','normalized','style','text','position',[.15 .74 .25 .06],'fontsize',16);
h4=uicontrol('parent',h,'units','normalized','style','text','position',[.65 .74 .25 .06],'fontsize',16);
line(0,0,'color','b','marker','.','markersize',20);
line([0,0],[1,-l-1],'color','k','linestyle','--','linewidth',1);
line([-2,2],[0.05,0.05],'color','b','linestyle','-','linewidth',3);
for i=-1.8:0.5:1.8
line([i,i+0.5],[0.05,0.55],'color','b','linestyle','-','linewidth',1);
end
b=line(x1(1),y1(1),'color','r','marker','.','markersize',20,'erasemode','xor');
g=line([0,x1(1)],[0,y1(1)],'color','y','linestyle','-','linewidth',2,'erasemode','xor');
n=length(t);
for i=1:n
set(b,'xdata',x1(i),'ydata',y1(i));
set(h4,'string',num2str(fy(i)));
set(g,'xdata',[0,x1(i)],'ydata',[0,y1(i)]);
set(h3,'string',num2str(fx(i)));
drawnow
end
theta=input('输入初始角度:');
tfinal=input('输入仿真时间:');
m=10;
g=9.8;
theta=theta*pi/180;
y0=[theta,0];
[t,y]=ode45('danbai',[0:0.01:tfinal],y0,[],l,g);
theta2=-g/l*sin(y(:,1));
y=[y,theta2];
x1=l*sin(y(:,1));
y1=-l*cos(y(:,1));
% y2=y(:,2);
fx=m*l*cos(y(:,1)).*y(:,3)-m*l*sin(y(:,1)).*y(:,2).^2;
fy=m*l*cos(y(:,1)).*y(:,2).^2+m*l*sin(y(:,1)).*y(:,3)+m*g;
% f=sqrt(fx.^2+fy.^2);
h=figure('units','normalized');
axis([-l l,-l,0]);
axis equal;
axis off;
h1=uicontrol('parent',h,'units','normalized','style','text','position',[.15 .81 .2 .07],'string','fx','fontsize',16);
h2=uicontrol('parent',h,'units','normalized','style','text','position',[.65 .81 .2 .07],'string','fy','fontsize',16);
h3=uicontrol('parent',h,'units','normalized','style','text','position',[.15 .74 .25 .06],'fontsize',16);
h4=uicontrol('parent',h,'units','normalized','style','text','position',[.65 .74 .25 .06],'fontsize',16);
line(0,0,'color','b','marker','.','markersize',20);
line([0,0],[1,-l-1],'color','k','linestyle','--','linewidth',1);
line([-2,2],[0.05,0.05],'color','b','linestyle','-','linewidth',3);
for i=-1.8:0.5:1.8
line([i,i+0.5],[0.05,0.55],'color','b','linestyle','-','linewidth',1);
end
b=line(x1(1),y1(1),'color','r','marker','.','markersize',20,'erasemode','xor');
g=line([0,x1(1)],[0,y1(1)],'color','y','linestyle','-','linewidth',2,'erasemode','xor');
n=length(t);
for i=1:n
set(b,'xdata',x1(i),'ydata',y1(i));
set(h4,'string',num2str(fy(i)));
set(g,'xdata',[0,x1(i)],'ydata',[0,y1(i)]);
set(h3,'string',num2str(fx(i)));
drawnow
end
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询