Solving second order ode for simple pendulum using octave
Solution:
clear all
close all
clc
b=0.05; %damping co-efficient%
m=1;%mass%
g=9.81;%acceleration due to gravity%
l =1;%length%
theta=[0;3]; % initial condition 0 meaning at rest and velocity 3rad/s%
t_span= linspace(0,20,50);% t_span is the limits or boundary conditions which user (I ) define according to problem,%
%here it is defined from 0 to 20s and 50 defines the number of times or values to be returned%
[t,Results]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta); %function call and using ode45 solver to solve the differential equations%
ct=1
for i=1:length(Results(:,1)) %for loop where the value of theta coloumn in the solution matrix or result matrix to be accessed%
x0=0;
y0=0;
x1=-l*sin(Results(i,1));
y1=-l*cos(Results(i,1));
plot([x0,x1],[y0,y1],\'linewidth\',3,\'color\',\'k\'); %plotting the line or string of pendulum%
hold on;
plot(x1,y1,\'-o\',\'markers\',20,\'markerfacecolor\',\'b\');%plotting the bob of the pendulum%
hold on;
grid on;
axis([-1.5 1.5 -1.5 1.5]);
pause(0.03)
hold off;
text=sprintf(\"damping%d.png\",ct);
saveas (gca,text);
ct=ct+1;
end
Youtube link to the video: