Week 3 - Solving second order ODEs
Explanation for 2nd order differential eqation,
using the below mentaion equation for write the cord,
take some input values like given value
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2.
in initial condition theta_0 = [0;5] and time point t_span = linspace(0,10,500);
% solve ODE
[t,results] = ode45(@(t,theta) ode_function(t,theta,b,g,l,m),t_span,theta_0);
function(t,theta,b,g,l,m) write seperatly
then plot the figure after creating the plot then make a animation video usind for loop commend
take some initial and final coordinates then creat plot figure 2.
after creating the figure 2 then mention the string of pendulum and bob of pendulum and using the pause commend for ploting speed.
And finally using move commend for creat the amimation.
Errors facing,
mention wrong spelling for x label after correct that makes x axis named.
wrongly mention apostrophe for linewidth its shows error after removing that error resolved.
by mistake mentioned zero in maker , after it changed into o, make the animation video.
clear all
close all
clc
b= 0.05;
g= 9.81;
l= 1;
m= 1;
% initial condition
theta_0 = [0;5];
% time point
t_span = linspace(0,10,500);
% solve ODE
[t,results] = ode45(@(t,theta) ode_function(t,theta,b,g,l,m),t_span,theta_0);
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('plot')
xlabel('time')
% for loop for creat animation of pendulum
ct=1;
theta_1 = results(:,1);
for i= 1:length(results(:,1))
% initial coordinates
x_start= 0;
y_start= 0;
theta_2 = theta_1(i);
%final coordinates
x_end = 1*sin(theta_2);
y_end = -1*cos(theta_2);
% plotting
figure(2)
%base of pendulum
plot([-1,1],[0,0],'LineWidth',3,'Color','b')
hold on
% string of pendulum
line([x_start x_end],[y_start y_end],'linewidth',6,'color','r')
hold on
% bob of pendulum
plot(x_end,y_end,'Marker','o','markersize',30,'MarkerFaceColor','g')
axis([-2 2 -2 2])
pause(0.03)
hold off
M(ct)= getframe(gcf);
ct=ct+1;
end
movie(M)
videofile = VideoWriter('simple_pendulum','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close (videofile)
function [dtheta_dt] = ode_function(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