Week 3 - Solving second order ODEs
Generating Second Order ODE function
Youtude link is provide "https://youtu.be/16SAazQsiyg"
clear
close all
clc
%Inputs
b=0.2; %damping co_eff
g=9.81; %gravitional m/s^2
m=1; %mass of ball in kg
l=1; %length in metre
%Initial values of angular disp and anugular velocity
theta_0=[0;3];
%time taken in between the points
t_span=linspace(0,20,600);
%Results of ODE
[t,results]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
%plotting
figure(1)
hold on
plot(t,results(:,1))
plot(t,results(:,2))
title('Simple pendulum by Sathishkumar')
xlabel('Time (sec)')
ylabel("Angular velocity/Displacement")
legend('Displacement','Angular velocity')
%Animating for pendulum
amplitude=results(:,1); %Amplitude values in matrix for loop
p=1;
for i=1:length(amplitude)
theta_ang=amplitude(i);
figure(2)
title('Simple Pendulum by Sathishkumar')
% coordinates of starting point of pendulum
x0=0;
y0=0;
%another end of pendulum(ending_point)
x1=l*sin(theta_ang);
y1=-l*cos(theta_ang);
%plot
plot([-1.1,1.1],[0,0],'color','g','Linewidth',6);
hold on
plot([x0 x1],[y0 y1],'color','b','LineWidth',3);
plot(x1,y1,'O','markers',20,'markerfacecolor',[1 1 0])
hold off
grid on
axis([-1.5 1.5 -1.5 0])
xlabel('Motion of the pendulum')
ylabel('Fixture of the string')
pause(0.002)
M(p)=getframe(gcf);
p=p+1;
end
%Movie
movie(M)
videofile1 = VideoWriter('simple pendulum.avi','Uncompressed AVI');
open(videofile1)
writeVideo(videofile1,M)
close(videofile1)
Inputs
To run the motion of pendulum basic inputs are needed such as
Damper coeff (b) 0.2;
Gravitional force = 9.81;
Mass of ball in KG = 1;
Length of string = 1 metre;
Initial Value for angular dispalcement(rad) and velocity(rad/sec):
theta value is given as input tends from 0 to 3
time taken for swing upto 20sec for 600 points
By creating ODE function to run the program and to input the given in that function
To plot the damping figure using 't,results' giving the suitable title for the motion and giving the name for x axis and y axis
Next is to creat a plot for Animating the Motion of Pendulum
Using the 'For' loop command
i=1:length(amplitude) is used for that figure(2)
To start up the motion the coordinates are given x0,x1,y0,y1
Formula used for x1,y1:
x1=l*sin(theta_ang);
y1=-l*cos(theta_ang);
To graphical representation for
x axis tends from -1.1 to 1.1
y axis tends from 0 to 0
mainly 'Hold On' command used to hold the two different inputs
Axis commands for pendulum from [-1.5 1.5 -1.5 0]
Label for X axis and Y axis are given
xlabel ('Motion of the pendulum')
ylabel ('Fixture of the string')
Command prompt
p=1; and p=p+1 is used for continue for 20 sec
By the end 'movie' command is used
The output of the figure 1 and figure 2 are attached
Error occured during the programming
Error 1 :
line 6 : b=0.05 by using b as 0.05 vibration can't be arrested so that changed into 0.2 is used
Error 2 :
line 31 : amplitude=results(:,1) in this using result i have used Result.