Week 4.1 - Solving second order ODEs
1) REPORT
MAIN CODE
clear
close
clc
%Step1
%Given data
%variable defenition
b=0.05; %damping coefficient
g=9.8; %acceleration due to gravity
l=1; %length of pendulum string
m=1; %mass of bob
%Step2
%initial condition
theta_0=[0;3];
%Time duration for simulation
t_span=linspace(0,20,100);
%Step4
%solving the differential equation
%Calculation using ODE45
[t,r]=ode45(@(t,theta) ode_function_pendulum(t,theta,b,g,l,m),t_span,theta_0);
%step5
%Plot of Angular displacement & Velocity with respect to Time function.
ct=1;
theta=r(:,1);
for i = 1:length(t_span)
Theta1 =theta(i);
%Polar Co-ordinate To Cartesian Co-ordinate
x0 = 0;
y0 = 0;
x1 = sin(Theta1);
y1 = -cos(Theta1);
figure(1)
plot([x0 x1],[y0 y1]);
hold on
axis([ -1 1 -2 0]);
plot(x1,y1,Marker="o",MarkerFaceColor="g",MarkerSize=10);
hold off
%Video for Pendulum motion
M(ct) = getframe(gcf);
ct = ct+1;
end
%Step6
movie(M)
videofile = VideoWriter('pendulum.avi','Uncompressed AVI');
open (videofile)
writeVideo (videofile,M)
close(videofile)
%Step3
FUNCTION CODE (Name:ode_function_pendulum)
function [dtheta_dt] = ode_function_pendulum(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
The given datas were typed in and further values were found using suitable equations and given data.The time was typed in by using ‘linspace’ command from 0 to 20 seconds with 100 value in between them.
The second order ordinary differential equation is convert into 1st order ordinary differential equation.ODE45 function is used for solving ordinary differential equation.
Suitable graph was plotted.We chnged the polar co-ordinate to cartesian co-ordinate because we used ‘sin’ command instead of ‘sind’ command in function.
A loop was made for creating the frame motion for the simple pendulum and saved in the movie.
MOVIE
2)Error faced
Unrecognized function or variable 'ct'
3) Resolving the error
value for 'ct' was provided
4)
