Week 3 - Solving second order ODEs
Aim
to Simulate the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.
Solution
for solving the second order derivative defining the equation of motion for simple pendulum with damping
Inputs
g = gravity in m/s2,
l = length of the pendulum in m,
m = mass of the ball in kg,
b=damping coefficient.
Write a program in Matlab\Octave that will simulate the pendulum motion, just like the one shown in the start of this challenge.
use,
l=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2.
main Code
clear all
close all
clc
%input parameters
l= 1; %length of pendulum in m
m=1; %mass attached at the end of pendulum in Kg
b= 0.05; %coefficent of dampping no unit
g=9.81; %accelaration due to gravity in m/s^2
%condition to be simulatedbetween 0-20 sec,
%for angular displacement=0,angular velocity=3 rad/sec at time t=0
theta_0 =[0;3]% theta is in radian and theta increase by 3 radian per sec
t_span=linspace(0,20,200)%time starting from 0 to 20 seconds with 100 value points
%solving ODE equation
[t,results]= ode45(@(t,theta)ode_pendulum(t,theta,l,m,b,g),t_span,theta_0);
ct=1
for i=1:length(results(:,1))
x0=0
y0=0
x1=l*sin(results(i,1))
y1=l*cos(results(i,1))
figure(2)
plot([x0 x1],[y0 y1],x1,y1,'O','markersize',40,'markerfacecolor','r','color','b','linewidth',3)
axis([-1 1 0 1.2])
pause(0.02)
M(ct)=getframe(gcf)
ct=ct+1
end
movie(M)
videofile= VideoWriter('pendulum_motion','uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)
Sub function code for solving ode45 a library function
% dtheta_dt is a function of time 't', angle theta , length 'l', mass 'm',
% damping factor 'b', accelaration due to gravity 'g'
function [dtheta_dt] = ode_pendulum(t,theta,l,m,b,g)
theta1=theta(1);
theta2=theta(2);
%first derivative
dtheta1_dt=theta2;
%second derivative function with damping factor
dtheta2_dt=-((b/m)*theta2)-((g/l)*sin(theta1));
dtheta_dt=[dtheta1_dt;dtheta2_dt]
end
I could not find a way to invert the video
google drive link for the video of the pendulum
https://drive.google.com/file/d/1Qf-LDthr1gqx4vuOHqj6UsSUbPwyXUff/view?usp=sharing