Project

Creating simple pendulum motion

Aim :
solving second order ODEs to animate motion of simple pendulum.

Introduction:
Understandinng simplependulum- a simple pendulum is a mechanical device that oscilates to a fixed point.
It consist of mass 'm' hanging by a string of length 'L' in which one end is attached to a fixed point 'o' as shown in fig.
most important force that helps in working of simple pendulum is the earths accileration of gravity 'g'.

so, when the pendulum is moved to a angle θ,note the length from pivot poit to pendulum must remain same ie, there must not be slack in the sting. as the pendulum is lifted to an angle θ ,a potential energy is formed .when relesed it tends to go to an equilibrium position.But to desepate that energy a series of oscilations will occur which slowly reduces the amplitude to zero.

above figure represent a simple pendulum.(credits:www.sciencefacts.net)

Governing Equation:

The main equation to solve the pendulum swinging

`(d^2theta)/dt^2 +(b/m(d theta)/dt)+g/Lsin theta=0` ---->eq(A)

where,

`(d theta)/dt` is the first derivative of θ with respect to time which is also termed as angulat displacement

`(d^2theta)/dt^2` is the second derivative of θ with respect to time also termed as angular velocity

lets assume,

`theta =theta1`

so,

`(d theta)/dt = (d theta1)/dt` -----> eq(1) first derivative

also lets assume,

`(d theta1)/dt =theta2`

so,

`(d^2 theta)/dt^2 = (d^2 theta1)/dt^2 =d/dt((d theta1)/dt) = (d theta2)/dt` ------>eq(2) second derivative

now apply eq(1) & eq(2) in eq(A),

`(d theta2)/dt + b/m theta2 + g/L sin(theta1)=0`

so,

`(d theta2)/dt =-( b/m theta2) -( g/L sin(theta1))`

we can write,

`d/dt[[theta1] , [theta2]] = [[theta2],[-(b/m theta2)-(g/L sintheta)]] =d/dttheta` ----->eq(B)

equation B is the equation that will provide us with data on matlab.

Maincode

% Solving second order ODEs 
close all
clear all
clc


% few given parameters:
%length of pendulum (L) in meter
L=1;
%mass of ball (M) in kg
m=1;
%damping coefficient (b) 
b=0.05;
%accelaration due to gravity (g) in m/s^2
g=9.81;
%vaiable to cpunt frames
ct=1;
%time span of ossilaton (t_span) 
t_span=linspace(0,20,500);
%angular displacement at time t=0 from timespan of occilation
ang_disp0=0;
%angular velocity at time t=0 from timespan of occilation in rad/sec
ang_vel0=3;
%create an ode vector theta_0[displacement,velocity] at initial conditon
theta_0=[ang_disp0;ang_vel0];

%using an inbuilt function ode45 to get value from function
[t,results]= ode45(@(t,theta) ode_fun(t,theta,b,g,L,m),t_span,theta_0);

%plot graph for angular displacement
plot(t,results(:,1)) 

%hold values on graph
hold on

%plot graph for angular velocity
plot(t,results(:,2))

%label y-axis as plot
ylabel('plot')

%label x-axis as time
xlabel('time')


%to create animation of pendulum
%let us take the value from 1stode 
thetax=results(:,1);

%creating forloop for the plotswit i value from 1 to length(thetax)
for i=1:length(thetax)
    theta_x=thetax(i);
%   fixed point of pendulum (x0,y0)
    x0=0;
    y0=0;

%   posission of the other end of pendulum(x1,y1)at angle theta_x
    x1=L*sin(theta_x);
    y1=-L*cos(theta_x);

%   creating a second plot
    figure(2)

%   ploting a base line which represent a celing or stucture where one end
%   of pendulum is attached
    plot([-1 1],[0 0], 'Color','r','LineWidth',4);
    hold on

%   ploting aline from atached point of sting to length of pendulumat an angle    
    plot([x0 x1],[y0 y1], 'Color','g','LineWidth',4);
    hold on
%   ploting a o-shaped marker to repesent weight of pendulum
    plot(x1,y1,'o','Markers',15,'MarkerFaceColor','b');
    hold off

    %label y-axis as plot
    ylabel('y axis')

%   label x-axis as time
    xlabel('x axis')
    grid on
%   fix the axis value to the graph
    axis([-1.5 1.5 -1.5 0.5]);
%   pausethe proseses for a brief period   
    pause(0.001);

%   adding frames
    M(ct)=getframe(gcf);
    ct=ct+1;


end

% to create a video file of the animation

%use function movie() to process the data from M(ct)
movie (M)

%create new video file with video name and format uncompressed avi 
videofile=VideoWriter("ode2challenge1.avi","Uncompressed AVI")

%command to open the video file
open(videofile);

%write the plots/frames to video file
writeVideo(videofile,M);

%close the videofile and save
close(videofile);

takes inputvalues, puts the value in loop in wich a fuction is called to get the angle atwhich the pedulum swings at a perticulat itteration, values are stored in a vector and then exicuted in equation to get xy cordinates.

the x1y1 cordinate are found from the angle and using the toght angle formed by the angle and string.Lsintheta and Lcostheta.

Fuction code

[t,y] = ode45(@(t,y) 2*t, tspan, y0)   ode45 is abuitin solver used to solve y′=f(t,y) such equations
or wich ve lot of matricies M(t,y)y′=f(t,y)
% creating a function ode2_function with input time,theta,damping
% coefficient,gravity,length,mass
%save value to dtheta_dt
function [dtheta_dt]=ode_function(t,theta,b,g,L,m)
%   assign 1st oder differntial equation
    theta1=theta(1)
%   assign 2nd oder differential equation
    theta2=theta(2)
%   so we can solve  1st oder  
    dtheta1_dt=theta2;
%   so we can solve  2st oder
    dtheta2_dt=-(b/m)*theta2-(g/L)*sin(theta1);
%represent value of 1st and 2nd oder in a colum vecrtor
    dtheta_dt=[dtheta1_dt;dtheta2_dt];
end

Explanation:

first L,m,b,g,ct,t_span,ang_vel0,ang_disp0. is input.

Create a coloum matrix theta_0 with input initian conditon [0; 3]

Calling a matsolver function ode45 with input vatiables

he fuction solves the equation and outputs a colum matrics with first and second oder defrential equation

Polt graph with results from ode45 function

To create animation take the angle from min to max with requred interval

Plot base point, using trignomatric function solve end point of string and draw a line. store in figure in a variable

The figures are added ito a sequence and stored in uncompressed avi fomat

Note:detailed explenation stepsive provided with in the code as comments.

Output:

below given is the output screenshot and video link to youtube

https://youtu.be/dhPgFOfW5aA

Error

below is the error screenshot. error caused due to decleration of variable.

solved : mass as 'm' and movie variable 'M'

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.