Week 3 - Solving second order ODEs
AIM: TO WRITE A PROGRAM TO SHOW ANIMATION VIDEO OF SIMPLE DAMPPED PENDULUM USING ODE EQUATION.
EXPLANATION OF CODE:
Step1. Use of regular matlab code clear all, close all & clc for clearing temporary memory closing plots & clearing command window.
Step2. Intializaing inputs in the program l,g,b,m(length of the pendulum in m, mass of the bob in kg, gravity in ms/s2, damping coefficient).
Step3. Array for time is generated using linspace.
Step4. Now the ode45 function has defined.
Step5. Once excecution ode45 function is executed ode45(@ode_func,t_span, theta_0) it will help us to calculate the ode equations of simple pendulum.
Step6. Now once the calculation is done will plot the graph of time vs angular displacement.
Step7. In next srep the loop is created.
Step8. Marker command is used to create sphere.
Step9. In this step the move command is used to create animation file of all the figures.
ERRORS FACED WHILE PROGRAMMING & STEPS TO OVERCOME:
1. This was a basic error, which is calling a function ode_func, which I overcome by going back in engine kinematics video.
2. During plotting some error were faced like inavalid expression & operators to overcome which valid expression and operators are used.
3. I was not aware of marker command before, so to overcome which I took refrence from some projects and videos and still it took several mistakes to perform this code properly.
*Below theYoutube link of animation video is posted.
*REPRESENTATION OF CODES:
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/l)*sin(theta1);
dtheta_dt = [dtheta1_dt; dtheta2_dt];
clear all
close all
clc
b = 0.05;
g = 9.81;
l = 1;
m = 1;
% initial condition
theta_0 = [0;5];
% time points
t_span = linspace(0,10,500);
% solve ODE
[t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m), t_span, theta_0);
figure(1)
subplot(1,2,1)
plot(t,results(:,1),'color','r')
xlabel('time')
ylabel('angular displacement')
subplot(1,2,2)
plot(t,results(:,2),'color','b')
xlabel('time')
ylabel('angular velocity')
figure(2)
plot(t,results(:,1),'color','r')
hold on
plot(t,results(:,2),'color','b')
xlabel('time')
ylabel('angular displacement and angular velocity')
displacement = results(:,1);
ct = 1;
for i=1:length(displacement)
THETA = displacement(i);
l = 1;
x0 = 0;
y0 = 0;
x1 = 1*sind(THETA);
y1 = 1*cosd(THETA);
figure(3)
plot([1 -1],[0 0],'linewidth',3,'color','r')
hold on
plot([x0,x1],[y0,y1],'linewidth',3,'color','g')
axis([-1 1 -1 1])
hold on
plot(x1,y1,'o','markers',15,'markerfacecolor','b')
hold off
pause(0.01)
M(ct) = getframe(gcf);
ct = ct+1;
end
movie(M)
videofile = VideoWriter('ode_func.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)