matlab单摆程序

matlab单摆程序,第1张

%制作动画

%挂摆横梁

plot([-0.20.2],[00],'color','y','linestyle','-',...

'linewidth',10)

%画初始位置的单摆

g=0.98%重力加速度,可以调节摆的摆速

l=1

theta0=pi/4

x0=l*sin(theta0)

y0=(-1)*l*cos(theta0)

axis([-0.75,0.75,-1.25,0])

axis('off') %不显示坐标轴

%创建摆锤

head=line(x0,y0,'color','r','linestyle','.',...

'erasemode','xor','markersize',40)

%创建摆杆

body=line([0x0],[0y0],'color','b','linestyle','-',...

'erasemode','xor')

%摆的运动

t=0

dt=0.01

while 1

t=t+dt

theta=theta0*cos(sqrt(g/l)*t)

x=l*sin(theta)

y=(-1)*l*cos(theta)

set(head,'xdata',x,'ydata',y)

set(body,'xdata',[0x],'ydata',[0y])

drawnow

end

%单摆程序

%状态量变y[角位置,角速度]

%运动微分方程dy=[y(2),-g/l*y(1)]

function main

TSPAN=0:0.01:10

y0=[0.1475,0]

[t,y]=ode45(@(t,y) yfun2(t,y),TSPAN,y0)

plot(t,y(:,1),'r')

hold on

plot(t,y(:,2),'k')

legend('position','velocity')

grid on

end

function  exer2=yfun2(t,y)

g=9.8

l=25

exer2(1,1)=y(2)

exer2(2,1)=(-g/l)*sin(y(1))

%程序文件

end

%挂摆横梁

plot([-0.20.2],[00],'color','y','linestyle','-',...

'linewidth',10)

%画初始位置的单摆

g=0.098 %重力加速度,可以调节摆的摆速

l1=1

l2=0.8

l3=0.6

theta0=pi/4

x01=l1*sin(theta0)

y01=(-1)*l1*cos(theta0)

x02=l2*sin(theta0)

y02=(-1)*l2*cos(theta0)

x03=l3*sin(theta0)

y03=(-1)*l3*cos(theta0)

axis([-0.75,0.75,-1.25,0])

axis('off') %不显示坐标轴

%创建摆锤

head1=line(x01,y01,'color','r','linestyle','.',...

'erasemode','xor','markersize',40)

head2=line(x02,y02,'color','g','linestyle','.',...

'erasemode','xor','markersize',40)

head3=line(x03,y03,'color','b','linestyle','.',...

'erasemode','xor','markersize',40)

%创建摆杆

body1=line([0x01],[0y01],'color','b','linestyle','-',...

'erasemode','xor')

body2=line([0x02],[0y02],'color','b','linestyle','-',...

'erasemode','xor')

body3=line([0x03],[0y03],'color','b','linestyle','-',...

'erasemode','xor')

%摆的运动

t=0

dt=0.1

while t< 30

    t=t+dt

    theta1=theta0*cos(sqrt(g/l1)*t)

    x1=l1*sin(theta1)

    y1=(-1)*l1*cos(theta1)

    set(head1,'xdata',x1,'ydata',y1)

    set(body1,'xdata',[0x1],'ydata',[0y1])

    

    theta2=theta0*cos(sqrt(g/l2)*t)

    x2=l2*sin(theta2)

    y2=(-1)*l2*cos(theta2)

    set(head2,'xdata',x2,'ydata',y2)

    set(body2,'xdata',[0x2],'ydata',[0y2])

    

    theta3=theta0*cos(sqrt(g/l3)*t)

    x3=l3*sin(theta3)

    y3=(-1)*l3*cos(theta3)

    set(head3,'xdata',x3,'ydata',y3)

    set(body3,'xdata',[0x3],'ydata',[0y3])

      

    drawnow

end


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12060157.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存