Week 4.1 - Solving second order ODEs
code with explaination:-
clear
close
clc
b=0.05; % DAMPING COEFFICIENT
g=9.81; % ACCELERATION DUE TO GRAVITY
l=1; % LENGTH OF THE PENDULUM STRING
m=1; % MASS OF THE BOB
%INITIAL CONDITION for angular displacement and angular velocity
theta_0=[0;5];
%TIME PERI0DS
t_span=linspace(0,10,500);
%solving ORDINARY DIFFERENTIAL EQUATION
[t,y]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
%extractiom of theta from y array
theta=y(:,1);
ct=1;
for i=1:length(theta) % here we use for loop for checking each element of y
x0=1;
y0=1;
x1=1-l*sin(theta(i));
y1=1-l*cos(theta(i));
% plotting
plot([0 2],[1 1],'linewidth',3)
hold on
plot([x0 x1],[y0 y1],'linewidth',3)
hold on
plot([x1 x1],[y1 y1],'o','MarkerSize',20)
hold off
axis([0 2 -0.5 1.5])
ylabel('plot')
xlabel('time')
pause(0.03)
M(ct)=getframe(gcf);
ct=ct+1;
end
% CREATING Animation of the pendulum
movie(M)
videofile=VideoWriter('Ode_code.avi','uncompressed.avi');
open("videofile")
writeVideo(videofile,M)
close(videofile)
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

ERRORS FACING:-

i overcome this error problem by the help of mentoring session.