Week 3 - Solving second order ODEs
- g=9.81 , gravity in m/s^2
- l= 1 , length of the pendulum in m
- m=1 , mass of the ball in kg
- b=0.05 , damping coefficient
- angular displacement=0
- angular velocity=3 rad/sec
- simulate the motion between 0-20 sec,for angular displacement=0,velocity=3 rad/sec,at a time t=0.
NOTE* in the given equation we have the 2nd order ode, so we want to convert in to two first order ODE's ,because the
ode45 accept only the first order ODE's
ie, theta = theta1
dtheta/dt=dtheta1/dt=theta2 ..........(1)
CODE EXPLANATION
The function of ODE work by 3 inputs
1) function has been integrated
2) Time intervel
3) initial condition of displacement and velocity
FOR CREATING LOOP FOR ANIMATING
1) counter value of ct=1 which means to capture each and every position of the pendulum
2) using loop of i=1:t
3) ploting the coordinates [x0 x1],[yo y1]
4) create a marker at the end of x1,y1 using 'marker command'
5) stricting of all position of the figure by using M(ct)=getframe(gcf)
CREATING ANIMATION VIDEO
1) create a movie by using movie(M)
2)create a video file named 'ode pendulum' by using video writer command
3)open the video file by ' open(videofile) ' command
4)close the video file using 'close(videofile)' command
CODE
clc
clear all
close all
% input parameters
l=1; % length of pendulum in m
m=1; % mass of the ball in kg
b=0.05; % dambing coefficient
g=9.8; % gravity in m/s^2
% initial condition of displacement and velocityt
theta_dv=[0;3];
% time points
t_span=linspace(0,20,100);
% to solve the ODE
[t,results]=ode45(@(t,theta)ode_funct(t,theta,b,g,l,m),t_span,theta_dv);
% plotting the required 1st figure
figure(1)
plot(t,results(:,1),'Color','r');
hold on
plot(t,results(:,2),'color','g');
hold off
xlabel('time');
ylabel('angular_displacement');
% animation of pendulum
ct=1;
for i=1:length(t)
x0=0;
y0=0;
x1=l*sin(results(i));
y1=-l*cos(results(i));
figure(2)
plot([x0 x1],[y0 y1],'linewidth',2);
hold on
plot(x1,y1,'o',MarkerSize=20);
hold off
axis([-2 3 -2 0]);
pause(0.01);
M(ct) = getframe(gcf);
ct=ct+1;
end
movie(M)
videofile= VideoWriter('ode pendulum','uncompressed AVI');
open(videofile);
writeVideo(videofile,M);
close(videofile);
FUNCTION
Writing a program and naming the function 'ode_funct ' and saving the program with the the name same
function[dtheta_dt] = ode_funct(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
The uploaded link of the animation in the youtube : https://youtu.be/HRE_vvfSSC0