Week 3 - Solving second order ODEs
%%
function xdot=Untitled(t,x) %using x instead of theta and xdot is an output variable of function
b=0.05;%given valus
m=1;%mass
g=9.81;
L=1;
xdot=zeros(2,1);%assining null matrix to xdot
xdot(1)=x(2);
xdot(2)=(-b/m)*x(2)-(g/L)*sin((x(1));
end
code
L=1;
t=linspace(0,20,150);
x0=[0 3];
%this below steps is for solving diffrential equation using ode45(matlab
%inbuilt function)
[t x] = ode45('pendulum_2nd_order',t,x0);
%ploting for tracing of angular velocity and displacement
%subplot(2,1,1)
plot(t,x(:,1),'-b','linewidth',3);
xlabel('time t in sec');
ylabel('angular displacement');
hold on;
plot(t,x(:,2),'-k','linewidth',3);
grid on;
%animation
x0=0;
y0=0;
ct=1;%counter actually represent each and every frame
for i=l:length(t)
x1=L*sin(x(i,1));
y1==L*sin(x(i,1));
figure(2)
plot([-1 1],[0 0],'linewidth',6,'color','g');
axis([-2 2 -2 2]);
line([x0 x1]),[y0 y1],'linewidth',2,'color','b');
hold on;
plot(x0,y0,'o','markersize',3,'markerfacecolor','y');
plot(x0,y0,'o','markersize',3,'markerfacecolor','y');
plot(x1,y1,'o','markersize',20,'markerfacecolor','r');
title('damping of simple pendulum');
legend('support rod','length of pendulum','fixed point','bod of pendulum');
grid on;
hold off;
m(ct)=getframe(gcf);
ct=ct+1;
end
movie(m);
videofile=videowriter('simple_pendulum','uncompressed avi');
open(videofile);
writevideo(videofile,m);
close(video)