Week 3 - Solving second order ODEs
We solve this Initial VAlue Problem ODE using reuction of the orer method. The goal is to simplify the order of the problem and to solve for the output for a system of equations. We input all the parameters required to define the system! The length in question should be length of the string+ distance to center of gravity. So I took the length as 0.95 and a uniform bob of radius 0.5 units( relative unit to the string).
clear all
clc
%inputs
b=0.05;%damping constant
g=9.81;%acceleration due to Gravity
l=0.95;% length of the pendulum
m=0.1;%mass of the bob
%initial conditions
theta_0=[0 ; 5];%Initial conditions to solve the IVP problem
%timepoints
t_span=linspace(0,20,200);
%ode_solver
[t,results] = ode45( @(t, theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
plot(t,results(:,1))
figure(1)
hold on
plot(t,results(:,2))
legend('Displacement','Angular')
xlabel('time')
ylabel('Angular Displacement and Velocity')
count=1;
for i=1:length(results)
x_0=0;%initial positions
y_0=0;
x_final=l*sin(results(i,1));%results(i,1) gives you location of the bob at each instant
y_final=-l*cos(results(i,1));
figure(2)
plot([-2 2], [0 0], 'linewidth',5)
hold on
plot([x_0 x_final] ,[y_0 y_final],'linewidth',3);
hold on
plot(x_final,y_final,'o','markersize',10,'markerFacecolor','g')
axis([-2 2 -2 2])
pause(0.05)
hold off
fr_ame(count)=getframe(gcf);
count=count+1;
end
movie(fr_ame)
videofile=VideoWriter('Pendulum_skilllync.avi', 'Uncompressed AVI');
open(videofile)
writeVideo(videofile,fr_ame)
close(videofile)
%Ode_func
function[diff_theta]= ode_func(t,theta,b,g,l,m)
theta_1=theta(1)
theta_2=theta(2)
diff_theta1= theta_2;
diff_theta2= -(b/m)*theta_2 -(g/l)*(sin(theta_1))
diff_theta=[diff_theta1;diff_theta2];
end