●Objective:
•To solve a second order differential equation with ODE(Matlab inbuilt function) & to create an animation of a damping pendulum.
●CODE 1:
close all
clear all
clc
% Inputs
b = 0.05;
g = 9.81;
l = 1;
m = 1;
% Initial condition for displacement
theta_0 = [0;5];
% Time points
t_span = linspace(0,20,200);
% Solve ode
[t,results] = ode45(@(t,theta)ode_func(t, theta, b,g,l,m),t_span, theta_0);
figure(1)
%title('Displacement Vs Time plot')
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('Time')
ylabel('Displacement & Velocity')
ct = 1;
for i = 1:length(results(:,1));
x0 = 0;
y0 = 0;
y1 = -1*cos(results(i,1));
x1 = 1*sin(results(i,1));
plot([x0 x1],[y0 y1],'linewidth',4)
hold on
plot(x1,y1,'.','markers',50)
plot([-5,5],[0,0],'linewidth',5)
axis([-1.5,1.5,-1.5,1.5])
pause(0.09)
M(ct) = getframe(gcf);
hold off
ct = ct+1;
end
movie(M)
videofile = VideoWriter('Pendulum_animation.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
●CODE 2:
% function code
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
●CODE EXPLANATION:
•CODE 1
The first part of code1 is the program that is written to solve the damping pendulum eqautaion with the help of funtion ode45.
Then the valoue of theta(0) and velocity(5) is taken under a variable theta_0.
The linspace command is used to describe the time span from 0 to 20 and divide it in to 100 steps.
Next the ode45 is used to solve the differential eqaution following with ode funtion.
After that the plot command is used for tracing the angular velocity & angular displacement.
The ct(counter) command is actually represents each and every frame.
Next the for loop has taken to initiate the loop,getframe command is used to capture the each and every figure and M(ct) is used for making a movie from it.the next commands are use to create a animation from those frame.
LINK FOR ANIMATION VIDEO ON YOUTUBE:
https://youtu.be/o1RQTAjlmzA