MATLAB Report on: - understand Ordianary Differential equation works in moving Pendulum Application.
Objective: - Write a program to simulate the transient behavior of a simple pendulum and to create an animation of its motion.
⦁ ODE: - Ordinary differential equation which has one or more than one derivatives of a dependent variable, y, w.r.t. an independent variable, t, usually referred to as time domain.
Image shows the Differential Equation which has to be solved for pendulum problem
⦁ There are different types of ODE's i.e. ODE-45, ODE-23, ODE-113,...., etc. We needed medium accuracy to solve this equation. As it is considered the first choice of solver and if we required tighter accuracy we go with ODE-23 or ODE-113 solvers.
ODE-45
Problem type - Nonstiff type
Application - Most of the time ODE-45 should be the first solver you try.
Description - ODE with time and state-dependent and state-dependent mass matrix - the motion of the pendulum.
Step 1: Write a function code for the differential equation to simple pendulum motion.
% Programmed by Ravi Thakur
function [dtheta_dt] = ode_func(t,theta,b,g,l,m);
theta_1=theta(1);
theta_2=theta(2);
dtheta1_dt = theta_2;
dtheta2_dt = -(b/m)*theta_2 - (g/l)*sin(theta_1);
dtheta_dt = [dtheta1_dt; dtheta2_dt];
end
Step 2: Write an editor program to call the function code to solve the ODE-45
% Programmed by Ravi Thakur
close all
clear all
clc
% Given inputs
l=1 ; % length of the pendulum in m
m=1; % mass of the ball in kg
b=0.05; % damping coefficient
g=9.81; % gravity in m/s2
% Initial Start condition
theta_A = [0;3];
% Time range allotment
t_span = linspace(0,30,500);
% solving ODE-45_function
% Calling function code to solve inside ODE
[t, final_result] = (ode45(@(t,theta) ode_func(t,theta,b,g,l,m), t_span, theta_A));
figure(1)
plot(t,final_result(:,1),'linewidth',3);
hold on
plot(t,final_result(:,2),'linewidth',3);
xlabel('Time');
ylabel('Amplitude');
grid on
legend({'Angular Displacement','Angular velocity'});
% Creating a for loop to generate pendulum in graph format
for i=1:length(final_result(:,1));
x0=0;
y0=0;
x1= l*sin(final_result(i,1));
y1= (-l*cos(final_result(i,1)));
figure(2)
plot([-0.8,0.8],[0,0],'linewidth',15,'color','y');
axis([-1.2 1.2 -1.2 0.2]);
line([x0 x1],[y0 y1],'linewidth',3,'color','r');
hold on
plot(x1,y1,'o','markers',35,'markerfacecolor','b');
grid on
pause(0.03)
hold off
Pendulum(i)=getframe(gcf);
end
% Converting generated frames to animation
movie(Pendulum)
videofile = VideoWriter('Pendulum_motion.mp4','MPEG-4');
open(videofile)
writeVideo(videofile,Pendulum)
close(videofile)
Step 3: Results
The plot shows Pendullum's Amplitude Vs. Time graph
The Plot shows an image of the pendulum at rest after the motion is completed.
Step 4: Commands used
- ODE function command helps us to solve the differential equation and its ability to change the accuracy as per our requirement by changing the ODE number.
- The get-frame command helps to generate the frame to the pendulum in motion.
- We understood by seeing code that how we can convert frames to animation by using movie command.
- Video file command helps us to save the particular video file to a certain format
Step 5: Conclusion
Here we understand how ODE command works in MATLAB and how we can use it to solve differential equations. We also understood how we can generate and convert frames into animation by using different types of commands.
Step 6: Animations video YouTube link