Solving Second order ODE's using Matlab
AIM:
Write a program to simulate the transient behaviour of a simple pendulum and to create an animation of its motion.
OBJECTIVE:
1)To simulate the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.
2)To create an animation of the simple pendulum.
THEORY:
A pendulum is a weight suspended from a pivot so that it can swing freely. When a pendulum is displaced sideways from its resting, equilibrium position, it is subject to a restoring force due to gravity that will accelerate it back toward the equilibrium position. When released, the restoring force acting on the pendulum's mass causes it to oscillate about the equilibrium position, swinging back and forth. The time for one complete cycle, a left swing and a right swing, is called the period. The period depends on the length of the pendulum and also to a slight degree on the amplitude and the width of the pendulum's swing.
(source and image from google)
SOLVING SECOND ORDER ODE:
By using the obtained output we write the code.
PROCEDURE:
1)Initially input values are given for b,g,l,m.
2)Angular displacement is given as 0 and angular velocity is given as 3 for theta.
3)An array of time span is created to store 500 equally spaced intervals between time 0 to 20 seconds.
4)ode45 is used to solve the ode. It is an inbuilt solver in matlab.
5)After writing the function program, we give the ode function definition to the main program.
6)Then we plot angular displacement and angular velocity.
7)Finally, we create an animation using for loop and movie command.
FUNCTION PROGRAM:
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
MAIN PROGRAM:
clear all
close all
clc
%input parameters
%damping co-efficient
b=0.05;
%acceleration due to gravity in m/s^2
g=9.81;
%length in m
l=1;
%mass in kg
m=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);
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('time')
ylabel('plot')
legend('displacement','velocity')
%Animating simple pendulum
theta2 = results(:,1);
ct=1;
for i=1:length(theta2)
theta = theta2(i);
x0=0;
y0=0;
x1=l*sin(theta);
y1=-l*cos(theta);
%plotting the pendulum
figure(2)
plot([-1 1],[0 0],"color","k","linewidth",3)
hold on
plot([x0 x1],[y0 y1],"color","m","linewidth",3)
plot(x1,y1,'o','markers',10,"markerfacecolor","b")
hold off
grid on
axis([-2 2 -2 2]);
pause(0.03)
A(ct)=getframe(gcf);
ct=ct+1;
end
movie(A)
videofile=VideoWriter('Simple_pendulum.avi','UNCOMPRESSED AVI');
open(videofile)
writeVideo(videofile,A)
close(videofile)
OUTPUT:
I have attached the link for animation of simple pendulum. Since I am using matlab online i have screenrecorded and uploaded on youtube.
https://www.youtube.com/watch?v=jaoPb1mFyMk&feature=youtu.be
RESULT:
Thus the second order ODE has been solved,graph has been plotted and animation of simple pendulum has been done.