Challenge

Week 3 - Solving second order ODEs

ODE FUNCTION PROGRAM

function [dtheta_dt] = ode_func(~,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

* Ode45 function is which solve the simple differential equation with a certain specified command.

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

PROGRAM FOR THE SIMPLE PENDULUM

clear all
close all
clc

b=0.05;      % damping coefficient
g=9.81;      % acceleration due to gravity
l=1;         % length of string
m=1;         % mass of the bob

% initial condition 
theta_0= [0,3];

%time points
t_span = linspace(0,20,150);
% solve ode
[t, y] = ode45(@(t,theta) ode_func(t,theta, b,g,l,m),t_span,theta_0);
%
%subplot(2,1,1);
figure(1)
plot(t, y(:,1), '-b','linewidth',3)
hold on
plot(t,y(:,2))

xlabel('time t, in sec');
ylabel('Angular velocity(rad/s)/displacememt(rad)');
legend('displacement','angular velocity');

grid on

% Animation

%fixed cordinates
X0=0;
Y0=0;

ct =1;
for i = 1:length(t_span)

    % moving coordinates of the bob
    X1 = cos(y(i,1)-pi/2);
    Y1 = sin(y(i,1)-pi/2);
    
    
    figure(2)

    % ploting of the bob motion 
    plot([-1 1] ,[0,0], 'linewidth',6,'Color','g');
    axis([-2 2 -2 2]);
    line([X1 X0],[Y1 Y0], 'linewidth',4,'color','b');
    hold on
    plot(X1,Y1,'o','markersize',6,'markerfacecolor','y');
    plot(X1,Y1,'o','markersize',20,'markerfacecolor','r');
    title('Damping of simple pendulum');
    legend('Support rod','length of pendulum','Fixed point','Ball of pendulum');
    grid on;
    hold off;

    M(ct) = getframe(gcf);
    ct = ct+1;
end    


movie(M)
videofile = VideoWriter('simpal_pendulum.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

EXPLANATION OF PROGRAM:

clear all - To clear all the variables from the current workspace.

close all - It close all figures whose handles are visible.

clc - clc clears all the taxt from the command window.

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 the ordinary or partial differential equations using a numerical methad. 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 eqation.

The function should take at least 2 inputs to solve the equation to the approximate result.

The ode45 function has a syntax in which we need to specify which should be, 't' and 'y' and a timespan for the function to work and the initial value of the equation.

* Figure 1 is going to tell how the damping is done for the pendulum. The angular velocity and displacment will come to rest at end of the function

* Ploting the values for the angular velocity and the displacement by using plot command.

* The lable command is used to lable the X & Y axis

* To animate the motion of simple pendulum, a for loop is used to create the specified set of points to keep 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 store the data.

* At the end of the for loop there will be set of position for each and every point.

* title command is used to give the title of graph.

* legeng command is used to label the support rod, length of pendulum, fixed point, ball of pendulum

*Movie(M) is used to creat a film of the stored data of the motion of the pendulum.

*videowriter command specifies the name of the file and type 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 creat the video animation by using M file.

* close command is used to close the video file.

The blue curve is the displacement where it starts from zero. And the angular velocity is starting at 3(rad/sec).

CONCLUSION:

* We have animated the simple pendulum using the ode45 solver where the second order is reduced to a simple differential equation where it is easy to solve the given equation.

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.