Week 3 - Solving second order ODEs
Simple Pendulum
Aim:
Program that will simulate the pendulum motion by using Matlab.
Objective :
- Solve the ODE function by the MATLAB program
- Also write a program to simulate the simple pendulum.
What is simple pendulum?
A point of mass attached to light inextensible string and suspended from fixed support is called pendulum. The vertical line passing through the fixed support is the mean position of a simple pendulum. The vertical distance between the point of suspension and the Centre of mass of the suspended body is called the length of the simple pendulum.
The pendulum moves depends on the Newton second law of motion, so we get a second order ordinary differential equation that describes the position of the ball. ODE represents the equation of motion of a simple pendulum with damping
`(d^2theta)/dt^2+b/m(d theta)/dt+g/lsintheta=0`
Where ;
Length of the pendulum(L) =1m;
Mass of the ball(m)=1kg;
Damping coefficient(b)=0.05;
Gravity(g)=9.81 m/s^2;
program for simple pendulum
clear all
close all
clc
%input
b=0.05;
g=9.81;
l=1;
m=0.1;
%initial conditions
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);
ct=1;
for i=1:length(t_span)
x0=0;
y0=0;
%final position
x1=l*sin(results(i,1));
y1=-1*cos(results(i,1));
%Base line
plot([-2,2],[0,0],'linewidth',5,'color','black')
hold on
%string
plot([x0,x1],[y0,y1],'linewidth',2,'color','blue')
hold on;
%Ball
plot(x1,y1,'marker','o','markersize',15,'markerfacecolor','red')
axis([-2 2 -1.5 1])
pause(0.001);
M(ct)=getframe(gcf);
ct=ct+1;
hold off
end
movie(M)
videofile=VideoWriter('ode_main.avi','uncompressed Avi');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Program debrief:
- Our known variables are gravity ,length ,mass ,damping coefficient, angular displacement, angular velocity
- Define the initial conditions which is displacement and velocity
- Then define the value of time
- Calling the ODE function by using ODE45 which is inbuilt solver in MATLAB
- ODE45 contains 3 inputs 1.function to be integrated 2.time span 3.initial condition.
- ode_func is another function, for this function the input arguments are t, thetha,b ,g,l and m
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
Defining the first ode `d(theta_1) /dt=theta_2`
second ode is `d(theta_2)/dt=-b/mtheta_2-g/lsintheta_1`
and combine two odes.
using for loop to change the position of pendulum with respect to time and then ploting the base line , string and ball
Using Movie command to create a video file
Here the final simulation of pendulum ;
https://www.youtube.com/watch?v=5g9FS-RO-Bw
Result:
Finally the simulation of pendulum motion by MATLAB has been done.