Week 3 - Solving second order ODEs
Differential equations are governing eqations for most of mechanical mechanism
ODE

this is second order differential equation
In program we have to split all higher order DE in first order ,to simplify it
To simplify the comand Function comand is usedn for making 2nd order DE in 1st
Full program according to given inputs from question.
clear all
close all
clc
%inputes
b = 0.05; %damping coefficient
g = 9.81; %gravity
l = 1; %length of pendulum
m = 1; %mass of pendulum
%initial condition (andular displacement and velocity)
theta_0 = [0;3];
%time points (time limit)
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);
figure(1)
%time displacement grapg
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('plot')
xlabel('time')
ct=1;
%loop to get different orientation of pendulum
for i = 1:length(results(:,1))
figure(2)
%coordinates of pendulum
x1=1*sin(results(i,1));
y1=-1*cos(results(i,1));
plot([-1,1],[0,0])
hold on
%use marker commaned to give charesterustics to bob marker
plot(x1,y1,'o',MarkerSize=15,MarkerEdgeColor='r',MarkerFaceColor='g')
%fixing the grid
axis([-1.5 1.5 -1.5 1.5]);
%ploting lines following pendulum
plot([0,x1],[0,y1],'linewidth',3)
plot([-1,1],[0,0],'linewidth',3)
hold off
pause(0.05)
%storing frames into M
M(ct) = getframe(gcf);
ct=ct+1;
end
%to creat animation file in avi format
movie(M);
videofile = VideoWriter('ode_pendulum.avi','Uncompressed AVI');
open(videofile);
writeVideo(videofile,M);
close(videofile);
Function is used in program to simplify main progream
Function code
%function code for Differential equation simplification
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
PLOT for pendulum

Flinal screen after executing program

Pendelum animation file
Error faced

Error occured because of not givving enough time to complete program
solved as waited to complete animation from matlab to save video file