Week 3 - Solving second order ODEs
Aim:- To write a program in matlab to simulate the pendulum motion.
Some Definitions:-
A simple pendulum can be described as a device where its point mass is attached to a light inextensible string and suspended from a fixed support. The vertical line passing through the fixed support is the mean position of a simple pendulum.
The vertical distance between the point of suspension and the centre of mass of the suspended body (when it is in mean position) is
called the length of the simple pendulum denoted by L.This form of the pendulum is based on the resonant system having
a single resonant frequency.
Required Parameters:-
a.) Damping Coefficient b=0.05
b.) mass in kgs, mass=1kgs
c.) Length of Pendulum, L=1m
d.) Acceleration due to gravity,g=9.81m/sec^2
Procedure-
1. Give the theta value in the matrix its contain 2 value named as angular velocity and angular displacement.
2. We have to allocate the estimate time and convert this space into an equivalent space with linespace command.
3. Simulate the motion between 0-20 sec.
4. odi45 to solve ODE with using ode_fun Solver.
5. using iteration to simulate the mechanism.
6. We have to set the pivot line the and give the correct value of x and y.
7. We have to create a new M variable to keep the current frame
8. Provide an addendum to resist the command.
9. Then after the loop we have to make a converson movie that is use to save the frame,Therefore the final image is constructed.
PROGRAM FOR CREATING ODE_FUNCTION
function [dtheta_dt]=ode_function(b,g,l,mass,t,theta)
theta_1=theta(1)
theta_2=theta(2)
dtheta_dt=[theta_2;(-b/mass)*theta_2-(g/l)*sin(theta_1)];
end
PROGRAM FOR CREATING ANIMATION FOR SIMULATION OF CODE
clear all
close all
clc
% Given Inputs
% damping coefficient
b=0.05;
% mass in kgs
mass=1;
% Length of Pendulum in meters
l=1;
% Acceleration due to gravity
g=9.81;
% time array
t_span=linspace(0,20,500)
% Initial boundary conditions for angular displacement and angular velocity
theta_initial=[0;5];
% Soving using Differential Equation
[t,results]=ode45(@(t,theta) ode_function(b,g,l,mass,t,theta),t_span,theta_initial);
% plotting
figure(1)
plot(t,results(:,1),"Color",'r')
hold on
plot(t,results(:,2),'Color','b')
xlabel('Time')
ylabel('Displacement/velocity')
% Now Creating an animation for the above code
ct=1;
for i= 1:length(results(:,1));
figure(2)
x_0=0;
y_0=0;
x_1=l*sin(results(i,1));
y_1=-l*cos(results(i,1));
plot([-1 1],[0 0],'linewidth',3,'color','r')
axis([-2 2 -2 2])
hold on
line([x_0 x_1],[y_0 y_1],'linewidth',3,'color','b')
plot(x_1,y_1,'o','MarkerSize',25,'MarkerFaceColor','g')
hold off
M(ct)= getframe(gcf);
ct=ct+1;
end
% Creating a video file
movie(M)
Videofile=VideoWriter('sec_order_ODE.avi','Uncompressed AVI')
open(Videofile)
writeVideo(Videofile,M)
close(Videofile)
RESULT
1.) You can go through the animation video of simulation of simple pendulum through the You Tube Video Link as
2.)In figure(1) Red Curve Represents angular displacement where as Blue curve Represents angular velocity
ERRORS
1.) Basic Typo Errors which was further resolved through reviewing code again and again.
2.) Was not able to obtain animation because I was closing the figure window while it was running, so closing the figure window only after completing the animation avoided this error.
3.) Not Saving the function name by it's file name.