Project

Transient behaviour of a simple pendulum

Function Program:-

function [dtheta_dt] = ode_func(t,theta,b,g,l,m)

     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
         

Explanation- A function program is defined and input is given as theta1 and theta2.

clear all
close all
clc

b=0.05;
g=9.81;
l=1;
m=1;

%Initial condition
theta_0 = [0,3];

%Time points
t_span = linspace(0,20,500);

%Solve ODE
[t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);

%Plotting
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
grid on 
axis([0 20 -5 5])

xlabel(\'Time\')
ylabel(\'Plot\')
legend(\'displacement\',\'velocity\')
title(\'Plot(displacement & velocity) vs. Time\')

%Animation
ct = 1;

for i=1:length(t)
    
    x0 = 0;
    y0 = 0;
    
    x1=l*sin(results(i,1));
    y1=-l*cos(results(i,1));
    
    figure(2)
    plot([-1 1],[0 0],\'linewidth\',5,\'color\',\'k\')
    hold on
    
    plot([x0 x1],[y0 y1],\'linewidth\',3,\'color\',\'k\')
    hold on
    
    plot(x1,y1,\'marker\',\'o\',\'markersize\',30,\'markerfacecolor\',\'r\',\'markeredgecolor\',\'k\');
    hold off
    title(\'Transient mption of a simple damped pendulum\')
    
    axis ([-1.5 1.5 -2 0.5])
    pause(0.002)
    
    M(ct) = getframe(gcf);
    ct = ct+1;
    
end

movie(M);
videofile = VideoWriter(\'Pendulum_animation.avi\',\'Uncompressed AVI\');
open(videofile);
writeVideo(videofile,M);
close(videofile);

Explanation : -1. Program is started with clear all,close all,clc command.

2. Input is given as values of b,g,l,m & intial condition of theta is defined.

3.t_span is defined to give time points using linspace commsnd.

4. A formula is defined to solve ode as above. (ode@45)

5. Results were plotted using plot command.

6. Using for loop various position of pendulum is calculated, motion of pendulum that what we have got from this. Formulae are written to calculate value of x1,y1.

7.plot command was use to connect string and also marker caommand was used to define a pendulum ball.

8.For making animation and saving it M(ct) = getfram(gcf), and movie,vidowriter commands were used.

Youtube link - https://youtu.be/Yf7qvyGXnz0

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.