Project

Simple Pendulum

clear all
close all
clc
b= 0.05;
g=9.81;
l=1;
m=1;
%initial condition
theta_0= [0; 3];
%timepoints
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);
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel(\'Plot\')
xlabel(\'Time\')
legend(\'Angular Displacement\',\'Angular Velocity\')
ct=1;
for i=1:length(t_span)
    THETA1(i)= results(i,1);
    x0=0;
    y0=0;
    y1=-l*cos(THETA1(i));
    x1=l*sin(THETA1(i));
    plot([x0 x1],[y0 y1],\'linewidth\',3);
    axis([-2 2 -1.5 0.5]);
    hold on
    plot(x1,y1,\'marker\',\'o\',\'markersize\',20,\'markerfacecolor\', \'c\');
    hold off
    M(ct)= getframe(gcf)
    ct= ct+1;
end
movie(M)
videofile= VideoWriter(\'simple_pendulum.avi\',\'uncompressed AVI\');
open(videofile)
writeVideo(videofile,M)
close(videofile)


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

Function takes time, angular displacement, angular velocity, damping constant, acceleration due to gravity, length of the string and mass of the bob as inputs. It then assigns the values of angular displacement and angular velocity to theta1 and theta2. Using that outputs dtheta1_dt and dtheta2_dt can be found.

First we declare the values of b,g,l, and m.

Then we declare an array that contains initial angular displacement and angular velocity.

Then we declare the timespan.

Then to find the variation of angular dispacement and angular velocity with time we call the function. ode45 is a predefined function.

We then plot the graph of angular dispacement and angular velocity varying with time.

Then, to make the animation we declare a counter variable ct.

We begin a for loop going from 1 till the span of time period.

For every value of i, we store the angular displacement of that particular row in an array THETA1.

The co-ordinates of the support of the pendulum are given as (0,0).

y= -lcos(THETA1(i)). Minus symbol so that the thread will be below the support.

x= lsin(THETA1(i))

We then plot the thread of that particular displacement.

We define the range of the x and y axis and then we press hold on.

We draw the bob of the pendulum using markers in the coordinates (x1 y1).

We store each frame in an array M and then increase ct by 1 to go store the next frame.

To make a movie, we use the movie function.

We enter the name of the the video and and type of video and transfer the contents of M to the video.

We then close the videofile.

Errors Faced:

1)In the function, used sind instead of sin,

Output:

\"\"

2) Forgot to enter hold off.

Output:

\"\"

3) Entered figure(ct)

Output:

\"\"

Outputs:

\"\"

\"\"

Link to youtube video: https://www.youtube.com/watch?v=sJvRs60CGww&feature=youtu.be

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.