close all
clear all
clc
%write a program for oscilating pendulum
%inputs parameters
%coordinates
x=[0,3];
y=[0,4];
%damping coefficient
b=0.05;
%gravity(m/s2)
g=9.81;
%length(m)
l=1;
%mass(kg)
m=1;
ct=1;
%initial angle of pendulum
theta_0 = 0; %in degrees
%theta_0 = 30*pi/180; %in radiance (if required)
%angular velocity (rad/sec)
v0=3;
%Initial state vector(angular displacement)
z0= [theta_0; v0];
%times for simulation (sec)
t_span=[0,20];
%maximum allowable time step (sec)
dtMax = 0.01;
%number of simulation steps
n_Step = ceil(diff(t_span)/dtMax);
%time vector
t= linspace(t_span(1),t_span(2),n_Step);
%initial state matrix
z = zeros(2,n_Step);
z(:,1) = z0;
%for loop for running thr simulation with ode function
for i=2:n_Step
dt = t(i)-t(i-1);
zinitial = z(:,i-1);
z(:,i) = zinitial + dt*ode_funct(zinitial,l,b,g,m);
end
%Generate plot of pendulum
for i = 1:length(t)
X(2) = l*sin(z(1,i));
Y(2) = -l*cos(z(1,i));
plot(X,Y,'k-');
hold on
plot(X(2),Y(2),'k.','MarkerSize',40);
hold off
grid on
axis equal
axis([-1.5 1.5 -2 0]);
title([num2str(t(i)),'sec'])
xlabel('X[m]');
ylabel('Y[m]');
pause(dtMax)
M(ct) =getframe(gcf)
ct=ct+1
end
movie(M)
videofile=VideoWriter('new_pendulum.avi','uncompressed avi');
open(videofile)
writeVideo(videofile,M)
close(videofile)
%ode function use for pendulum
function [dz] = ode_funct(z,l,b,g,m)
theta = z(1,:);
v = z(2,:);
dtheta = v;
%angular velocity
dv = -(g/l)*sin(theta) - (b/(m*l^2))*v;
%angular displacement
dz = [dtheta; dv];
end
%youtube link
https://youtu.be/VkvjXey6nq8
%brief procedure of oscilating pendulum by using matlab
step-1
assuming the coordinates of x and y .
step-2
assining the variable for ODE like
damping coefficient, mass, length, gravity ,etc
step-3
assining the initial angle or state of the pendulum which is 0 degree.
step-4
assing the variable and values of angular velocity and displacement for the oscilation.
step-5
providing the given time span between 0 to 20 sec.
step-6
providing the maximum derivate value or speed (0.01)
step-6
creating the forloop to repeate the same oscilation produre to the given time span.
step-7
creating forloop for repeating oscillation until time span finish.
step-8
creating the video file cammand
step-9
last step running the program if any error get encountered they rectifying it and again start running the program.