- First define funcion of vetiable of angle and velocity.
function dadt = damp_sys (t,theta,b,m,g,l)
dadt = [theta(2);-(b/m)*theta(2)-(g/l)*sin(theta(1))]
end
- After define and saving function create new file to evaluate our output.
- Firt clear all and define variables.
clear all
close all
clc
b = 0.05
m = 0.1
g = 9.8
l = 1
ct = 1
tspan = linspace(0,20,500)
thetao = [0;30]
- Now define function of second order differencial equation. In that define function and calculate value and store in array.
[t,r] = ode45(@(t,theta) damp_sys(t,theta,b,m,g,l),tspan,thetao)
- Velocity metrix is generate which shows agle variation according to time. So, define for loop for getting value of theta with repect to time. And also define x and z varaiable to store x,y coordinates at various angles. After getting coordinates plot it on chart and define axis.
- Marker command use to create circle at ends. ANd marker size define radius of circle.
- Pause command use to decide interval between two successive plot.
- Now, store this all plot in a variable M by getframe command.
for i = 1:length(r)
x = -l*cos(r(i,1));
z = -l*sin(r(i,1));
plot([0 z],[0 x],'Marker','O',MarkerSize=30,MarkerEdgeColor='r');
title('Pendulum Effect');
axis([-1.5 1.5 -1.5 1.5]);
i= i+1;
pause(0.01);
M(ct) = getframe(gcf);
ct = ct+1;
end
- After getting frames we demp in movie. So first create file,open it and write this frames into it. Then close command.
movie(M)
videofile = VideoWriter("Pendulum_movement.avi","Uncompressed AVI");
open(videofile);
writeVideo(videofile,M);
close(videofile);
- It will show animation of Pendulum system.
- Video link on Youtube is below.
- https://youtu.be/B_UFmIVJEHo