Challenge

Week 3 - Solving second order ODEs

INTRODUCTION

the pendulum is a body or mass suspened from a fixed point so that it can swing back and forth under the influence of gravity. pendulum are used to regulate the movement of clocks because the interval of time for each complet oscillation,called the period, is constannt.the period depends on the length of the pendulum and also to a slight degree on the amplitude,the width of the pendulums swing.

AIM

to solve the second order ode functiopn using matlab which depicts the motion of the pendulum and to create an animation of the same.

OBJECTIVE

to write a program that solves the following ode this ode represents the equation of motion of a simple pendulum with damping

d^theta/dt^2+b/m dtheta/dt+g/L sintheta = 0

in the above equation

g = gravity in m/s^2

l = length of the pendulum in m,

m = mass of the ball in kg;

b=damping coefficient

b = damping

m = mass

g= gravity

l = length

FIXED PARAMETERS

length(L) = 1 meter

mass(m) = 1kg

damping coiefficient(b)= 0.05

acceleration due to gravity(g) = 9.81m/s^2

for time period = 0-20sec

angular displacement=0,angular

velocity= 3 rad/sec at time t=0

FUNCTION

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/1)*sin(theta1);
    dtheta_dt = [dtheta1_dt; dtheta2_dt];
end

EXPLANATION

*IN THIS FUNCTION PROGRAM THE SECOND ORDER ODE IS SIMPLIFIED TO THE FIRST ORDER ODE BY INTRODUCING THE TERM THETA1&THETA2.

*THEN THETAL1&THETAL2 IS DIFFERENTIATED AND THE DTHETA_DT GIVES A COLUMN MATRIX.

PROGRAM

clear all
close all
clc


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

% initial condition
theta_0 = [0;5];

% time points
t_span = linspace(0,10,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('angular velocity,angular displacement')
xlabel('time in sec')
grid on
legend('angular velocity','angular displacement')

%fixed parameters 

% 1.Damping Co-eff.:
b = 0.05;
%2.Acceleration due to gravity(m/s^2):
g = 9.81;
%3.Mass(kg) :
m = 1;
% 4.Length(m) :
l = 1;

% initial condition
theta_0 = [0;3];


%Time points 
t_span = linspace(0,20,200);
% Solving 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))
xlabel('time')
ylabel('velocity (red) & Displacement (blue)')

% Animation
ct = 1;
for i = 1:length(results(:,1))
x0 = 0;
y0 = 0;
x1 = 1*sin(results(i,1)+pi);
y1 = 1*cos(results(i,1)+pi);
figure(2)

plot([x0 x1],[y0 y1],'-o', 'MarkerSize',10,'MarkerFaceColor','r','MarkerEdgeColor','r','linewidth',5)
axis([-2 2 -2 2])
hold on 
plot([-1 1],[0 0],'linewidth',5,'color','[0 0.3 0]')
M(ct) = getframe(gcf)
hold off
ct = ct + 1;
end

% Movie & videofile 
movie(M)
v_f = VideoWriter('pendulum_challenge.avi','Uncompressed AVI')
open(v_f )
writeVideo(v_f,M)
close(v_f)





EXPLANATION

*in the above program the fixed parameters are length mass damping coefficient and acceleration due to gravity

*time and angle are given as the initial condition

*in the tspan command 200 values are taken in between 0 to 20 times period

*using the ode 45 command ode function is introduced

*using the plot command velocity and displacement is plotted in the y-axis against time in the y-axis

*using the for loop command the mass at various points and various times are marked in the graph and are plotted

*using the movie command the plot is saved as an uncompressed avi file

ANIMATION

https://youtu.be/6am4hJOvK6g

CONCLUSION

*the graph of displacement and angular velocity is plotting in velocity v/s time diagram

*animation of simple pendulum is crated

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.