Week 3 - Solving second order ODEs
Aim:
To write a program that solve the following ODE equation and represents the equation of motion of a simple pendulum with damping
Procedure:
As per the given condition and study video the equation is solved and the program is written and executed the values are only changed as per given condition
As per given condition need to make a video so we need the multiple frames so the for loop is used and as per the procedure the ploting is created and here for pendulum the marker is used for the creating ball and ploting used for create the sting and connecting rod
As per that the ball size are given and colors are used axis is given for unwanted scaling
Movie is need to be create so as per the condition program provided counter is used for continuous frame creating and videowriter is used to name and create movie
Open the file and run and write the command and close after completing it then after completed the video is saved need to upload to the youtube and provided the link below and program also attached with it and work file and output result are attached with this
youtube link: https://youtu.be/SBfvVa14mvw
Main program :
clear all
close all
clc
%solving the second order ODE and show the animation
b = 0.05;
l=1;
m=1;
g=9.81;
%initial condition
theta_0 = [0;3];
%time points
t_span = linspace(0,20,500)
%solve ODE
[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('plot')
legend('displacement','angular velocity')
grid on
hold off
displacement= results(:,1);
velocity=results(:,2);
%fixed coordinates
x0 = 1;
y0 = 1;
ct = 1;
for i= 1:length(displacement)
%coordinates
x1= 1-1*sin(displacement(i));
y1= 1-1*cos(displacement(i));
figure(2)
%plot fixed link
plot([0 2],[1 1],'linewidth',15,'color','b')
hold on
plot([1 x1],[1 y1],[x1 x1],[y1 y1],'-o','markersize',25,'markerfacecolor','r','linewidth',4)
plot(x0,y0,'-o','markersize',3,'markerfacecolor','b')
%axis setting
axis([0 2 -0.5 2])
M(ct)= getframe(gcf);
ct=ct+1;
hold off
end
movie(M)
videofile = VideoWriter('simple pendulum.avi','uncompressed AVI')
open(videofile)
writeVideo (videofile,M)
close(videofile)
function program:
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/1)*sin(theta1))
dtheta_dt = [dtheta1_dt;dtheta2_dt];
end