Project

Simple Pendulum

%Simle pendulam
close all;
clc;
 
%input variables
g = 9.8;
l = 1;
b = 0.5;
m = 1;
 
%intial conditions
theta_0 = [0;3];
 
t_span = linspace(0,20,500);
 
%ODE function 
[t,results] = ode45(@(t,theta)odefunction(t,theta,g,l,m,b),t_span,theta_0);
 
%plot for Velovity and displacement  vs Time
 
figure(1)
plot(t,results(:,1));
hold on;
plot(t,results(:,2));
xlabel(\'Time\');
ylabel(\'Angular Velocity/ Displacement\');
legend(\'Displacement\',\'Velocity\');
A = getframe(gcf);
saveas(gcf,\'plot1.png\');
M = repmat(A,1,500);
 
 
 
for i = 1:length(t_span)
     x1 = 0;
     y1 = 0;
     y2 = -l * cos(results(i,1));
     x2 = l*sin(results(i,1));
     figure(2)
     plot([x1 x2],[y1 y2]);
     hold on;
     plot(x2,y2,\'ko\');
     axis([-1 1 -1.5 0]);
     M(i) = getframe(gcf);
     pause(0.04);
     clf
end
 
%creating  a video file 
 
movie(M);
videofile = VideoWriter(\'pendulum.avi\',\'Uncompressed AVI\');
open(videofile);
writeVideo(videofile,M);
close(videofile);
%ODE function
function dtheta_dt = odefunction(t,theta,g,l,m,b)
         Theta1 = theta(1);
         Theta2 = theta(2);
         dtheta1_dt = Theta2;
         dtheta2_dt = -(b/m)*Theta2 -(g/l)*sin(Theta1);
         dtheta_dt = [dtheta1_dt;dtheta2_dt];
end
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.