Week 4.1 - Solving second order ODEs
Solution:
- From above question we had some parameters like,
- L=1 metre,
- m=1 kg,
- b=0.05.
- g=9.81 m/s2
- and t = 0 to 20 sec,
- Displacement = 0
- angular velocity = 3 rad/sec
- So now we need to create psuedocode for programming,
Psuedocode and explaination for programming:
- Asusual we start with close all,clear all and clc
- Then give the input like
- b = 0.05; //damping coefficient
- g = 9.81; //gravity
- l=1; //length
- m=1; //mass
- At the initial condition the displacement was 0 and angular velocity is 3,so for that we have to create the column vector like,
- theta_0 =[0;3];
- Then we need to create line for Time points that,here we create the t_span array that starts from 0 to 20 sec with 500 points,using linspace command.
- t_span = linspace (0,20,500);
- Then we need to create line for ODE that,here t is time array and we get results as a column vector.
- Then here ode45 is inbuild ode solver in matlab,
- Now we need to define the function as @(t,theta) for time and theta value,
- Next we need to use the function that 'ode_func',
- It is a predefine fuction that contains the value of input parameters
- [t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
- function dydt = ode_func(~, theta, b, g, l, ~)
% ODEs for a simple pendulum
dydt = zeros(2, 1);
% First ODE: d(theta)/dt = omega
dydt(1) = theta(2);
% Second ODE: d(omega)/dt = -b*omega - (g/l)*sin(theta)
dydt(2) = -b * theta(2) - (g / l) * sin(theta(1));
end
- In this function we have the ode equations for to find the angles that theta 1 and theta 2.
- Then comming to the plotting,we need to create plotting in column vector methode like
- figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
- figure(1)
- Then we need to create xlabel,ylabel,legend and title as like below.
- ylabel ('Plot')
xlabel('Time')
legend('Location', 'best');
title('Solving 2nd order equation')
- ylabel ('Plot')
- Then comming to the animation setup
- Plot empty lines for Angular Displacement and Angular Velocity Set labels and title for the plot
- Then Create Figure 3 Plot empty line for Simple Pendulum Animation Set axis limits, labels, and title for the animation plot Preallocate video file 'pendulum_animation.avi'
- Next Open the VideoWriter object with the specified filename For each time step in t Calculate pendulum bob coordinates (x, y) based on results(i, 1)
- Then Update animation plot with the new coordinates Force MATLAB to update the figure (drawnow)
- And Capture the current frame for the video Write the frame to the video file Pause for a short duration
- Finally Close the video file Set labels and title for the animation plot in Figure 3.
MATLAB CODE:
%Solving second order ODES
close all
clear all
clc
%input
b = 0.05;
g = 9.81;
l=1;
m=1;
%Angular displacement = 0 ,Angular velocity = 3.
%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);
%Ploting
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel ('Plot')
xlabel('Time')
legend('Location', 'best');
title('Solving 2nd order equation')
% animation setup
figure(2);
h = plot(NaN, NaN, 'o-', 'LineWidth', 2);
axis([-1.5 1.5 -1.5 1.5]);
xlabel('X-axis');
ylabel('Y-axis');
title('Simple Pendulum Animation');
grid on;
hold on;
% preallocate video
vidObj = VideoWriter('pendulum_animation.avi');
open(vidObj);
% animation loop
for i = 1:length(t)
x = l * sin(results(i, 1));
y = -l * cos(results(i, 1));
set(h, 'XData', [0, x], 'YData', [0, y]);
drawnow; % Force MATLAB to update the figure
% capture frame for video
frame = getframe(gcf);
writeVideo(vidObj, frame);
% add labels and title
xlabel('Horizontal Displacement (m)');
ylabel('Vertical Displacement (m)');
title('Simple Pendulum Motion');
pause(0.01); % Adjust the pause duration if needed
end
% close video file
close(vidObj);
% add labels and title
xlabel('Horizontal Displacement (m)');
ylabel('Vertical Displacement (m)');
title('Simple Pendulum Motion');
function dydt = ode_func(~, theta, b, g, l, ~)
% ODEs for a simple pendulum
dydt = zeros(2, 1);
% First ODE: d(theta)/dt = omega
dydt(1) = theta(2);
% Second ODE: d(omega)/dt = -b*omega - (g/l)*sin(theta)
dydt(2) = -b * theta(2) - (g / l) * sin(theta(1));
end
OUTPUT PLOT:

AVI Screen shot:

ANIMATION GOOGLE DRIVE LINK:
https://drive.google.com/file/d/1unLA77FyzmCtGT5jXCXt7Q7whulVzmds/view?usp=sharing
Conclusion:
- By using this Second order ode equation in MATLAB we can creation pendulam animation.