Week 3 - Solving second order ODEs
Aim ;
Write a program and solve the given ODE and represent the equation of motion of simple pendulum with damping. Then create a animation to show them.
Explanation ;
The given ODE to simulate to the second order equation of the simple pendulum to create plot between angular velocity and displacement by using the given ODE45.
By using ODE45 to solve a given equation and run to generate the program with animation.
Formula :
d^2θ/dt^2 = b/m . dθ/dt + g/L . sinθ = 0
θ = 0
dθ/dt = dθ/dt = θ2 .........(1)
d^2θ/dt^2 = d^2θ/dt^2 = d/dt(dθ1/dt) = d/dt(θ2)..........(2)
d^2θ/dt^2 + b/m dθ/t + g/L sinθ = 0
dθ1^2/dt^2 + b/m dθ/t + g/L sinθ1 = 1
dθ2/dt + b/m θ2 + g/L sinθ1 = 0
dθ1/dt = θ2
dθ2/dt = -b/m θ2 - g/L sinθ1
d/dt[θ1] = [θ2]
d/dt[θ2] = [-b/m θ2 - g/L sinθ]
Where,
b = damping coefficient
m = mass
g = gravitational acceleration
l = length pf the string
Procedure ;
Step 1 : Start
Step 2 : Open the Matlab Application
Step 3 : Type the command in application
Step 4 : Save to run the program
Step 5 : Display the output
Step 6 : Stop
Program ;
- Program for simple pendulum :
clear all close all clc % inputs b = 0.05; g = 9; l = 1; m = 0.1; % initial theta_0 = [0;4] % time t_span = linspace(0,20,200) % solve [t, result] = ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0) figure(1) plot(t,result(:,1)) hold on; plot(t,result(:,2)) videofile = VideoWriter('simple_pendulum.avi','uncompressed AVI'); open(videofile) figure(2) %animation for i = 1: length (result(:,1)) x0 = 0; y0 = 0; x1 = 1*sin(result(i,1)); y1 = -1*cos(result(i,1)); plot([0,0],[-1.5,1.5]) hold on plot([x0 x1],[y0 y1],'linewidth',2,'color','b') plot(x1,y1,'o','markersize',20,'color','r') axis([-1.5 1.5 -1.5 1.5]) pause(0.005) writeVideo(videofile,getframe(gcf)) hold off end close(videofile) - program for ODE function :
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]
end
Program Explanation ;
To start clear all, close all, clc and command window to clear all. Then inputs given to simulate and values for apply to run the half of the program, then to attached the center of the program for function of ODE to combine. After to using ODE45 to add with write remaining program to run and create the animation to make a movie with a link to attach below title for them.
Output Link ;
https://drive.google.com/file/d/1mxQwo9FlHOveA4tIEW0e1CXW4RK4SMdO/view?usp=sharing
Conclusion ;
The given program for simple pendulum with function of ODE run and animation is successfully executed.