Error-2
In this Error,figure1 should be written as figure(1).
Error-1
In this error,velocity column size is missing.Thus writing it solves the error.
https://youtu.be/tCDJ-o9YxHI
Animation link
Graph Velocity vs Time
1.This Graph show relationship between Time and Velocity
2.Blue line is Displacement and res line is Velocity.
3.When Displacement is zero velocity is maximum and so on.
3.With increase in time Displacement and Velocity get decreases.
4.This is because of damping effect as it tends body to be in equilibrium condition.
Code Explanation
In the main program we have called the function program.
This program consist of anonymous fuction.
The Main program is solved using ODE solver i.e, ode45 as per the syntx.
After solving the general ode, we moved toward next task.
writing program for Simple pendulum as per the given values.
and generating movie animation.
where we provide initial and final coordinated of the pendulum, at extreme position pendulum has two components i,e vertical and horizantal component.
than plotting graph for velocity vs time.
and generating animation.
2.Function Program
function [dtheta_dt]=ode_func(t,theta,b,g,l,m)
theta1=theta(1)
theta2=theta(2)
dtheta1_dt=theta(2)
dtheta2_dt=-(b/m)*theta2-(g/l)*sin(theta1);
dtheta_dt=[dtheta1_dt;dtheta2_dt];
end
1.Main Program
%PROGRAM TO SOLVE 2ND ORDER DIFFERNTIAL EQUATION
clear all
close all
clc
%Given Equation, d^2theta/dt^2+(b/m)*dtheta/dt+(g/l)sin_theta=0
%for solving we need to break down the equation into first order differential equation
%We get the final two eqn
%dtheta1/dt=theta2.....eq(1)
%dtheta2/dt=-(b/m)theta2-(g/l)sin(theta1)
%given
%b, damping coefficient
b=0.05
% g,acceleration due to gravity(m/s^2)
g=9.81
% l, length of pendulum (m)
l=1
% m,mass (kg)
m=1
%theta1,tehta2, angular displacement initially is zero & angular velocity(rad/sec)
theta_0=[0;3]
%t, time(s)
%initially t=0 upto t=20
t_span=linspace(0,20,500)
%using ODE Solver command with anonymous function
[t,results]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('time')
ylabel('velocity')
legend('Displacement','Angular Velocity')
hold off
displacement=results(:,1)
velocity=results(:,2)
ct=1
%for loop
for i=1:length(displacement)
%initial coordinates
x=0
y=0
%coordinates at position x1,y1
x1=1-l*sin(displacement(i))
y1=1-l*cos(displacement(i))
figure(2)
%plot fixed link
plot([0 2],[1 1],'linewidth',5,'color','y')
hold on
plot([1 x1],[1 y1],[x1 x1],[y1 y1],'-O','markersize',10,'markerfacecolor','r','linewidth',1)
plot(x,y,'-O','markersize',3,'markerfacecolor','b')
%axis setting
axis([0 2 -0.5 2])
M(ct)=getframe(gcf)
%incrementing
ct=ct+1
hold off
end
%creating Movie & Animation
movie(M)
videofile=VideoWriter('simple pendulum.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)