Week 3 - Solving second order ODEs
Aim: To plot for displacment and velocity of the simple pendulum oscillation and also to simulate the motion of simple pendulum for the above results using marker as well as movie command.
Theory: The following steps are used to solve the higher order ODE:
- First write a function to simplify the second order ODE into simpler terms
- Once the equation is simplified into 2 simpler ODE create a matrix with simpler ODE's
- Create another program to solve the ODE's give the inputs for damper value,gravity,length, mass and then the pendulum will oscillate for 20 sec with the angular velocity max of 3 rad / sec
- angular velocity is dependant on theta and time so create a variable with t and theta and give the intial time conditions to the variable
- Then give the time span array values from 0 to 20 secs with 500 values in middle
- after the declartion of input values use the ode solver command in matlab ode45 and call in the function program created before which has the dependant and independant variables as t and theta and then also use the intial conditions and the time intervals and bring a variable t and results
- once the output matrix is obtained plot the velocity and displacement graph and save it
- Then use the counter variable with the initial value as 1
- in for loop loop the results matrix column 1 which contains teh displacement values
- let the number of iterations be in range of 1 to the length of the result matrix
- first define the fixed point of the pendulum as x0 and y0 and the final point as x1 and y1
- x1 and y1 values are given by the trignometrical solution with the length of the pendulum and the displacement values
- after defining plot the fixed point and the pendulum cord and use the marker command to get the bob or mass that is attached to the pendulum
- create a variable oscillations to get the frames from the above iterations and then the counter is incremented by 1 for each iteration till it gives variations for all the values in result matrix
- then use movie command to create a video file or simulation for the movement of simple pendulum
Funtion program:
function[dtheta_dt] = ode_func(t,theta,b,g,l,m)
%solving the higher order ODE
theta1 = theta(1);
theta2 = theta(2);
%simplifying the ODE into 2 simpler ODE's
dtheta1_dt = theta2;
dtheta2_dt = -(b/m)*theta2-(g/l)*sin(theta1);
%defining them in a matrix format
dtheta_dt = [dtheta1_dt ; dtheta2_dt];
end
Solving ODE and animating the pendulum motion:
clear all
close all
clc
%Inputs
b = 0.05;
g = 9.81;
l = 1;
m = 1;
%Initial condition
theta_0 = [0;3];
%time points
t_span = linspace(0,20,1000);
%using ode45 solver to solve the equation which has t & theta as
%independant and dependant variables along with intial conditions values
[t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
%plotting the time and results
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
%labelling x and y
ylabel('plot')
xlabel('time')
%looping for animation
ct=1;
%looping to get the iterations for displacements
for i=1:length(results(:,1))
%declaring the start and end for x and y values
x0 = 0;
y0 = 0;
x1 = l*sin(results(i,1));
y1 = -l*cos(results(i,1));
figure(2)
%plotting the fixed point for the pendulum string
plot([-1,1],[0,0],'linewidth',10,'color','g');
hold on
%pendulum rope values plotting
plot([x0 x1],[y0 y1],'linewidth',5,'color','r');
hold on
%pendulum mass plotting
plot(x1,y1,'O','markersize',50,'markerfacecolor','b')
%defining axis and grid
axis([-2 2 -2 2])
grid on
pause(0.03)
hold off
%getting frames from the plot
Oscillation(ct) = getframe(gcf);
%incrementing counter
ct = ct+1;
end
%creating movie/animation
movie(Oscillation)
%video file naming
videofile = VideoWriter('Pendulum_Oscillations.avi','Uncompressed AVI');
open(videofile)
%writing videofile
writeVideo(videofile,Oscillation)
close(videofile)
Conclusion: The animation is obtained and can be viewed in the following link https://drive.google.com/file/d/1ERhgetUFDzLsYZJD8XYwO0n6DN_LmUpl/view?usp=sharing