Week 3 - Solving second order ODEs
Solving second order ODEs
1. Step wise explanation of the code:
- Use of the command 'close all','clear all' & 'clc' to erase all the previously programed graph,workspace & command window.
- entering the available data of the constants
- Writing the inetial conditions of the angular displacement and angular velocity as required
- Assigning the time span as required using the 'linspace' command
- solving the differential equation to find angular velocity &displacement with respect to time using the defined function in 'ODE45' command
- use 'figure' command to have different figures as we are going to have 2 figures.
- Plotting of the graph is done using 'plot' command for angular displacement&velocity with respect to time
- xlabel and ylabel commands are used to label the graph
- create the image for each value of time using 'for' loop
- point location of rod opposite to bob as a initial condition,assign the bob location with respect to time using Trigonometry.
- Plot moving rod ,base rod & Bob with required linewidth ,bob size using marker.
- Assigne the axis
- Get the frames using 'getframe'command and end the for loop
- Know create a movie using 'movie' command
- use 'videowriter' and create avi file,open the file using 'open' command and write the moovie using frames and close the file after completion.
Defining function:
- The function is defined as 'ode_fun' with inputs b.g.l.m,t,theta and assigned output as dtheta_dt
- theta1 is assigned to angular displacement & theta2 is assigned to angular velocity
- Know theta1 is equaled to dtheta_da1 and '-(b/m)*theta2-(g/l)*sin(theta1)' to dtheta2_dt as per requirement in logic
- Finally dtheta_dt is assigned,And the function is ended.
CODE:
clear all
close all
clc
g=9.8; %g = gravity in m/s2,
l=1; %L = length of the pendulum/rod in m,
m=0.1; %m = mass of the ball in kg,
b=0.05; %b=damping coefficient
%inetial condition
theta_0=[0;3];%angular displacement;angular velocity
%time points
t_span=linspace(0,20,500);
%solve ode
[t,results]=ode45(@(t,theta) ode_fun(t,theta,b,g,l,m),t_span,theta_0)
%plotting
figure(1) %use figure command to have different figures
plot(t,results(:,1)) %plotting angular displacement
hold on
plot(t,results(:,2)) %plotting angular velocity
%labeling the axis
ylabel('plot')
xlabel('time')
hold off
%creating the image for each value of time
figure(2)
ct=1;
for i=1:length(t_span)
x0=0; %point location of rod opposite to bob
y0=0;
x1=l*sin(results(i,1)); %select column 1 in results as it is displacement
y1=-l*cos(results(i,1));
%plotting
plot([x0 x1],[y0,y1],[-0.5 0.5],[0 0],'LineWidth',3) %making moving rod and base rod
hold on
plot(x1,y1,'o','MarkerSize',35,'MarkerFaceColor','k') %making bob
axis([-1.5 1.5 -1.5 0.5])
grid on
pause(0.001)
hold off
M(ct)=getframe(gcf); %getting frames
ct=ct+1;
end
%creating the movie
movie(M)
video_file=VideoWriter('swinging_pendulum.avi','Uncompressed AVI');
open(video_file)
writeVideo(video_file,M)
close(video_file)CODE OF FUNCTION:
function [dtheta_dt]= ode_fun(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
2. Errors faced while programming
- Undefined variable was the error which came due to writing tspan rather than t_span, it was rectified later .
Error image:

Rectified Image:

Graph plot v/s time:

VIDEO LINK of youtube: https://youtu.be/xRzpzRRpFuE