Week 3 - Solving second order ODEs
Aim:
Using Matlab : * Solve the second order ODE which represents the motion of simple pendulum with damping and
* TO Stimulate the motion of the pendulum between time_span of 0-20sec for angular displacement=0, angular velocity=3rad/sec at time t=0.
CODE AND EXPLANATION:
Program 1:To create a simple pendulum with damper:
clear all
close all
clc
% input parameters:
% b= damping co-effecient
% g= gravity
% l= length
% m= mass
b=0.05;
g=9.81;
l=1;
m=1;
%initial conditions we have two 1st order ODE
%one for velocity and other for displacement
theta_o = [0,5];
%Here initial displacement is 0 & initial velocity is 5m^2
%Time points
t_span = linspace(0,20,300);
%solve ODE ;
[t,result]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_o);
ct=1;
%where ode45 is an in-built solution in matlab
figure(1)
plot(t,result(:,1))
hold on
plot(t,result(:,2))
title('Motion of a simple damper pendulum')
xlabel('time')
ylabel('plot')
legend('Displacement(rad)','velocity(rad/s)')
for i=1:length(result(:,1))
ref=result(i);
%co-ordinates
x0=0;
y0=0;
x1= 1*sind(ref);
y1=1*(-cosd(ref));
figure(2)
plot ([x0 x1],[y0 y1],'linewidth',3,'color','b');
axis([-1 1 -1 1]);
%get individual frames to create a animated one
%M is the frame we capture
M(ct) = getframe(gcf);
ct=ct+1;
end
%conversion of plot into video
movie(M)
videofile = VideoWriter('ODE_MAIN.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Description:
Here * inorder to create an animation ,all the frames corresponds to each time_span value needs to be saved. we use For loop function with a required command to captures each frame and convert into graphical representation of a pendulum.
* ode45 is an inbuilt solution in matlab.
* Conversion of plots into video Under the videofile command: where the videoWriten with the required file name & type(.avi)
CODE AND EXPLANATION:
PROGRAM 2: Calling the ode_func in the main program:
Where * The 2nd order ODE is splitted into two 1st order ODE,then the 1st order ODE is solved individually.
* Here we call the function as ode_func.
* Calling the function ode_func into the main function.
function [dtheta_dt]=ode_func(t,theta,b,g,l,m)
%solution array
theta1 = theta(1);
theta2 = theta(2);
% 1st ODE
dtheta1_dt=theta2;
% 2nd ODE
dtheta2_dt=-(b/m)*theta2-(g/l)*sin(theta1);
dtheta_dt=[dtheta1_dt;dtheta2_dt];
end
We get the successive result of the program for damping effect on the pendulum in motion as per the assigned values.
Output:
youtube link attached :
https://youtu.be/Q9kgjzK1NQE