Function Code
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
in this program the second ordee ODE is simplified to the first order ODE by introducing term theta1 and theta2
@fixed parameters
clear all
close all
clc
b=0.05;
g=9.81;
l=1;
m=0.01;
%intial condition
theta_0 = [0;5];
%time points
t_span = linspace(0,10,500);
%solve ODE
[t, results] = ode45(@(t,theta) ode_func(t, theta, b,g,l,m),t_span, theta_0);
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('Plot')
xlabel('Time')
%plotting
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
title('motion of pendulum')
xlabel('time in sec')
ylabel('angular velocity(rad/s)/displacement(rad)')
legend('displacement','angular velocity')
%animation
ct = 1;
for i= 1: length(results (:,1))
X0 = 0;
y0 =0;
x1 = l*sin(results(i,1)+pi);
y1 = l*cos(results(i,1)+pi);
figure(2)
plot([x0 x1], [yo y1], '-o', 'MarkerSize',10, 'MarkerFaceColor', 'r', 'MarkerEdgeColor','r','linewidth',4)
axis([-2 2 -2 21])
hold on
plot([-1 1], [0 0], 'linewidth', 5, 'color', '[0 0.3 0]')
M(ct) = getframe(gcf);
hold off
ct = ct + 1;
end
% Movie & Videofile
movie(M)
v_f = VideoWriter('Pendulum_Challenge.avi','Uncompressed AVI');
open(v_f)
writeVideo(v_f,M)
close(v_f)
explanation
fixed parameters are lengh , mass, damping coe.and accelartion due to gravity.
time and angle given in intial condition