Cannot plot time vs displacement graph
Info
This question is closed. Reopen it to edit or answer.
Show older comments
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 Comment
Jan
on 5 Oct 2014
I do not understand what kind of "graph" you are looking for.
Answers (0)
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!