Week 3 - Solving second order ODEs
Pendulum animation challenge:
As provided, a pendulum is fixed with length L=1 metre,mass m=1 kg, damping co-efficient b=0.05 and accecelaration due to gravity g=9.81 m/s2.
We need to simulate the pendulum using the concept of the given Ordinary Differential equation.
The given parameters are to simulate the motion of pendulum between 0-20 seconds for the angular displacement=0 and angular velocity=3 rad/sec at time, t=0.
The program to simulate the process:
clear all
close all
clc
%Inputs
l=1;
m=1;
g=9.81;
b=0.05;
ct=1;
%Initial condition
theta_0=[0;5];
%time points
t_span=linspace (0,20,500);
%Solve ODE
[t,results]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
plot([-10,10],[0,0],'k','linewidth',2)
hold on
x=-l*sind(results(:,1));
y=-l*cosd(results(:,1));
plot([0,0],[x,y],'b','linewidth',2)
pause(0.03)
M(ct)=getframe(gcf);
ct=ct+1;
%To create a movie we will have to create a video file named M
movie(M)
videofile = VideoWriter('pendulum.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
There is a function in used in the program to solve the ODE which is ode45.
The function:
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
The values for angular displacement and angular velocity are stored in the results array.
Errors faced:
1.

Got this error while trying to use the marker command in a wrong program.
2.

Got this error because the code is not written for frame incrementation.