Week 3 - Solving second order ODEs
in the above equation
g= gravity in m/s^2
L= length of the pendulum in meter.
m= mass of the ball in kg.
b= damping coeffecient.
let Θ=Θ1
dΘ1/dt=Θ2
dΘ^2/dt^2=dΘ/dt
Input:
L=1 metre
m= 1kg
b=0.05
g= 9.81m/s^2
time span=0-20 sec
angular displacement=0
angular velocity=0
angular velocity=3 rad/sec2 at time t=0
Code:
% program to simulate the transient behaviour of a simple pendulam
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,500);
%solve ODE
[t, results]= ode45(@(t, theta) ode_func(t, theta,b,g,l,m),t_span, theta_0);
%ploting
plot(t, results(:,1))
hold on
plot(t, results(:,2))
ylabel( 'Plot' )
xlabel( 'Time in sec' )
%Iletration using for loop to create animation of simple pendulam of simple pendulam
ct=1;
theta_1= results(:,1);
for i= 1:length(results(:,1))
%initial co-ordinates
x_start=0;
y_start=0;
theta_2= theta_1(i);
%final co-ordinates
x_end= 1*sin(theta_2);
y_end= -1*cos(theta_2);
% ploting for animation
figure(2)
%base of pendulum
plot([-1,1],[0,0], 'linewidth',4, 'color', 'black')
hold on
%string of pendulum
line([x_start x_end],[y_start y_end], 'linewidth',6, 'color', 'b')
hold on
% bob of pendulum
plot(x_end,y_end, 'marker', 'o', 'markersize', 28, 'markerfacecolor','r')
axis([-2 2 -2 2])
pause(0.03)
hold off
M(ct)=getframe(gcf);
ct=ct+1;
end
Code for function program:
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
Explanation for each step for the problem:
1. in the step input and initial condition of a simple pendulum are fed.
2. then the program is written to solve the ODE in which Matlab inbuilt function (ode45) is used with constant theta t, and another function is used, which is defined seperately in function program.
3. In the next step fo result of solved ODE is plotted w.r.t time using plot command.
4. Now iteration of co-ordinate is fed using for loop to create animation.
5.Then plot command is used to plot pendulum string and bob.
Output:
1. The output obtained is graphical representation velocity curve and displacement curve w.r.t time.
Angular displacement and angular velocity plots wer generated for a time range of 20 seconds.