Info

此问题已关闭。 请重新打开它进行编辑或回答。

Cannot plot time vs displacement graph

1 次查看(过去 30 天)
Gazza Dazzle
Gazza Dazzle 2014-9-5
关闭: Steve Miller 2022-12-21
Hi I am struggling to plot time vs displacement graph for my pendulum program, i can only plot the animation of the pendulum but not a graph of it. Any ideas?
thanks
l=0.4;
m=0.5;
g=9.81;
b=0.05;
sampling=0.01;
runningstep=200;
timespan=sampling*runningstep;
x0=[pi/4
0];
%main program for numberical solution
k=1;
while(k<1000)
[t,y]=ode45(@pendulum,[0 sampling],x0,[],l,b,m);
if k==1
yp(1,:)=y(1,1:2);
tp(1,:)=t(1,1);
else
yp(k,:)=y(size(y,1),1:2);
tp(k,1)=sampling*k;
end
%update date
x0=y(size(y,1),1:2); %change initial value
k=k+1;
end
%Time Displacement Graph
plot(t,y)
title('time displacement graph')
%animation
xa=l*sin(yp(:,1));
ya=-l*cos(yp(:,1));
hp=plot([0,0],[xa(1),ya(1)],'linewidth',2,'Marker','o','Erasemode','xor');
axis([-l-0.1 l+0.1 -l-0.1 l+0.1]);
title('Pendulum problem');
xlabel('Displacement (degrees)');
ylabel('Angular velocity (radians/sec.)');
for i=1:size(tp)
set(hp,'xdata',[0,xa(i)],'ydata',[0,ya(i)]);
drawnow
pause(0.01);
grid on
mov(i)=getframe;
end
movie2avi(mov,'pendulum.avi')
funtion file
function dy=pendulum(t,y,l,m,b)
dy(1,1)=y(2,1);
dy(2,1)=-9.81/l*sin(y(1,1)-b/(m*l^2)*y(2,1));
  1 个评论
Jan
Jan 2014-10-5
I do not understand what kind of "graph" you are looking for.

回答(0 个)

此问题已关闭。

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by