Challenge

Week 3 - Solving second order ODEs

AIM:

To solve ordinary differential equations(ODE) and use the calculated information to animate the motion of the pendulum via MATLAB.

INTRODUCTION:

Pendulum motion basically depicts the motion of a mass hanging from a string that moves back and forth. The variables in pendulum motion are the mass, the length of the string, and the location, which is measured by an angle. Forces acting on the mass in pendulum motion are tension and gravity.

m = mass of the bob kg, g = acceleration due to gravitym/s^2, l = length of the string m. The simple pendulum problem can be solved by using the second-order ode functions and by solving these we will get the theta1 and theta2 values of the simple pendulum.

An ordinary differential equation (ODE) is an equation containing one or more functions of one independent variable and derivatives os those functions. The ODE equation and calculation are shown below.

Code:

%the function which will be called in main program to solve the ode

function[dtheta_dt]=ode_function(t,theta,b,g,l,m)


theta1=theta(1)
theta2=theta(2)
dtheta1_dt=theta2;
dtheta2_dt=-(b/m)*theta2-(g/1)*sin(theta1);
dtheta_dt=[dtheta1_dt;dtheta2_dt];

end
%second order ode of pendulum

%inputs

b=0.05;%damping coefficient
g=9.81;%gravity(m/s^@)
l=1;%length of spring in m
m=0.1;%mass in kg

%inital condition
%angular displacement is 0 and angular velocity is 5
theta_0=[0;5];

%time span
%pendulum oscillation
t_span=linspace(0,20,300);

%solving ode using fuction created 
[t,results]=ode45(@(t,theta)ode_function(t,theta,b,g,l,m),t_span,theta_0);

plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('plot')
xlabel('time')

%to create a simple pendulum
%counter value is defined 
c=1;
for i=1:length(results(:,1))
x_start=0;
y_start=0;
x_end=1*sin(results(i,1));
y_end=-1*cos(results(i,1));

%plotting the animation
figure(2)

plot([-1,1],[0,0],'linewidth',3);
hold on
plot([x_start x_end],[y_start y_end],'linewidth',3)
hold on

%to get the bob using marker function 
plot(x_end,y_end,'o','markersize',20,'markerfacecolor','b')
title('animation of pendulum motion')
axis([-1.5 1.5 -1.5 1])
pause(0.03)
hold off
M(c)=getframe(gcf);
c=c+1;

end

%animation of pendulam motion
movie(M)
Videofile= VideoWriter('second_order_ode.avi','uncompressed AVI');
open(Videofile)
writeVideo(Videofile,M)
close(Videofile)

Before writing the code it is important to start with clear all, close, and clc commands since

clear all-removes data from the workspace, freeing up system memory

close all - deletes the current figure or the specified figure

CLC - clears all inputs and outputs from the command window display, giving you a clean screen

The next spet is to define the input variables. The damping coefficient, gravity, length of the spring, and mass of bob have also been defined.

The next step is to define the initial conditions, the angular displacement and angular velocity have been defined. Timespan has also been defined.

The next step is to solve the ODE,ode45 was used, it is a function in Matlab that solves ordinary or partial differential equations using a numerical method. The numerical method in question here is the 4th order Runge Kutta method, the function ode45 takes 3 inputs. The 3 inputs are the function to be integrated, the time span, and the initial condition.[t, result]means the results that have been calculated with respect to time.

After calculating the results, the graph has to be plotted, the graph contained two plots. One for angular displacement with respect to time and one for angular velocity with respect to time. The x and y-axis labels have been defined.

The second part of the code is to establish a for loop, a for loop is a reputation control structure that allows you to efficiently write a loop that needs to execute a specific number of times. A counter was added before the for a loop. The start and end points of the x and y-axis were defined. The loop is where all the values of timespan are assigned to a variable 'i' one at a time. The coordinate of [x0 x1 y0 y1] and their corresponding relations are entered for a 'string' of the pendulum. The marker size command is used to form the bob of the pendulum.

The 'pause' command is used for pausing the command for a particular time (0.03s) we used.M command is used to capture the current frame every time foe loop runs. The final step of the loop is to add +1 to the variable 'c' this will increment the frame every time the for loop runs. End command is used for the plot and for the for loop to stop both from running.

The final step of the code is to create a movie of the pendulum movement, so the movie command is used. The video writer command was used to assign the video format, for this project.avi is used as the format. The open command was to open the video file. write video command is used to write the video with plot frames. Finally, the close command is used to close the video file.

Conclusion:

The values of x_start,y_start and x_end,y_end is shown below

x_start= 0

y_start= 0

x_end= -0.0113

y_end= -0.9999

Link for a video animation of the pendulum: simple pendulum

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.