Challenge

Week 3 - Solving second order ODEs

program for the simple pendulum

clear all

close all

clc


%input for the secound order ode of a simple pendulum


b = 0.05;
g = 9.81;
l = 1;
m = 0.1;

theta_0 = [0; 3];

t_span = linspace(0,20,500);

%ode 45 function to solve second order ode.


[t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span, theta_0 );

%plotting of angular velocity and displacement


figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
grid on
title('motion of pendulum')
xlabel('time in sec')
ylabel('angular velocity(rad/s)/displacement(rad)')
legend('displacement','angular velocity')

angle = results(:,1);

%count command for iterations.


ct = 1;

for i = 1 : length(angle)
omega = angle(i);
figure(2)
title('motion of pendulum')

%fixed corordinates of one end of simple pendulum.
x0 = 0;
y0 = 0;

%moving coordinates of the bob.
x1 = l*cos((3*pi/2)+omega);
y1 = l*sin((3*pi/2)+omega);

%plotting of the bob motion.
plot([x0 x1],[y0 y1],'linewidth',3,'color','g');
hold on;
plot(x1,y1,'o','markers',15,'MarkerFaceColor','k');
hold on;
plot([-3 3],[0 0],'linewidth',2,'color','b');
hold off
axis([-3 3 -2 2])
grid on
pause(0.1)
M(ct) = getframe(gcf);
ct = ct+1;
end

%to create an animation of the simple pendulum
movie(M)
videofile = VideoWriter('simple_pendulum.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

ode function program

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

code explanation

Clear all, close all, clc are used to clear the work space, to clear the graphs, and clear the command window.
The inputs are specified b = damping coefficient = 0.05 (n-m/s), g = acceleration due to gravity = 9.81 (m/s), l = length of the string = 1(m), m = mass of the bob = 1 (kg).
t_span is a command used in the ode45 solver which specifies the time span of the iterations that are going to solve the ordinary differential equation.

ode45 is a function in MATLAB that solves partial differential equations using a numerical method. In this problem the highest order is 2. So, we have to take two constant values so that the equation is changed to simple differential equation.
The function should take at least 2 inputs to solve the equation.
Syntax of a ode45 solver
[t, results] = Ode45(@ (t, y) ode (t, y, args), t_span, y0).

Figure 1 is going to tell how the damping is done for the pendulum. The angular velocity and displacement will come to rest at end of the function.
plotting the values for the angular velocity and the displacement by using plot command.
The label command is used to label the X & Y axis.

Legend command is used to label the angular velocity and the displacement.
To animate the motion of simple pendulum, a for loop is used to create the specified set of points to keep the bob at different locations.

x0 and y0 be the fixed point at one end of the string and x1 and y1 be the different position of the bob.
At each and every point of the bob the ‘ct’ command which is used to count each and every position and stores the data.

At the end of the for loop there will be set of positions for each and every point.
Movie(M) is used to create a film of the stored data of the motion of the pendulum.
videoWriter command specifies the name of the file and type of data is stored in it.
Open command is used to open the file where the video is going to store.
Writevideo command is used to create the video animation by using M file.
Close command is used to close the video file.

Ode45 function is which solves the simple differential equations with a certain specified command.

In this function, we need to specify the derivatives of theta and the equations to be solved.

Errors faced in program

while using the 'MarkerFaceColor' i have mistakenly use the small letters.

and while writing the ode45 i am not aware to put space between bracket and ode_func

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.