Week 3 - Solving second order ODEs
%defining ode function for simple pendulum.
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/t)*sin(theta1);
dtheta_dt = [dtheta1_dt;dtheta2_dt];
end
% second order ode simple pendulum
close all
clear all
clc
%step1 : inputs
%damping co_eff
b = 0.05;
%gravity
g = 9.81;
%length of string
l = 1;
%mass
m = 0.1;
%step2 : initial condition
theta_0 = [0;5];
%step3 : time points
t_span = linspace(0,10,5000;
%step4 : solving ode function
[t , results] = ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
%step5 : plotting
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel(time)
ylabel(plot)
%Step6 : animating pendulum
amplitude = results(:,1)
p = 1;
for i = length(amplitude);
angle = amplitude(i);
figure(1)
tite('simple pendulum')
x0 = 0;
y0 = 0;
x1 = l*sin(angel);
y1 = -l*cos(angle);
plot([-1. 1.1],[0 0],'linewidth',2);
hold on
plot([x0 x1],[y0 y1],'linewidth',2);
plot(x1,y1,'o','MarkerSize',20,'MarkerFaceColor','b');
hold off
axis([-1.5 1.5 -1.5 0.5]);
pause(0.003);
M(p) = getframe(gcf);
p = p+1;
end
%Step7 creating video file
movie(M)
videfile = VideoWriter('simple_pendulum.avi','uncompressed avi')
open('videofile')
writeVideo('vifeofile,M)
close('videofile')
the following error was found and rectified with the help of mentor provided by skill-link.