Challenge

Week 3 - Solving second order ODEs

subject:write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping

In the above equation,

g = gravity in m/s2,

L = length of the pendulum in m,

m = mass of the ball in kg,

b=damping coefficient.

Write a program in Matlab that will simulate the pendulum motion, just like the one shown in the start of this challenge.

use,

L=1 metre,

m=1 kg,

b=0.05.

g=9.81 m/s2.

Simulate the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.

When you do this you will get the position of the pendulum as a function of time. Use this information to animate the motion of the pendulum, as shown in the below figure.

Programme:

clear all
close all
clc

Above commands helps to remove the variables,workspace window and command window

Inputs:

g = gravity in m/s2 ;=g=9.81;

L = length of the pendulum in m ;l=1;

m = mass of the ball in kg ;m=1;

b=damping coefficient ;b=0.05;

% intial condition

theta_0=[0;3];
Here we consider angular displacement=0,angular velocity=3 rad/sec
%time points
t_span=linspace(0,20,700);
we consider time space between 0-20 seconds, By using the linspace command helps to create evenly spaced points as 500's.
% solve ODE
[t, results]= ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
In a function file which contains only function definitions. The name of the file must match the name of the first function in the file.
In order to solve the second orderr equation, we coverted into fisrt order and save the function as 'ode_func' and uses the inbulit function 'ode45'
ode45 command is an inbuilt command uses to solve nonstiff differential equations .
by taking inputs from subject and frame the function,dtheta1 column represents angular displacement values and second column dtheta2 represents angular velocity values.
function [dtheta_dt]=ode_func(t,theta,b,g,l,m)

 theta1=theta(1)
 theta2=theta(2)
 dtheta1_dt=theta2;
dtheta2_dt= -(b/m)*dtheta1_dt-(g/l)*sin(theta1);
dtheta_dt= [dtheta1_dt;dtheta2_dt];
end
Main programme:
In figure(1) we plot the velocity with respect to time.
In figure(2), we used the for loop command to know the loaction of the pendulum with respect to changes in time by the values of angular displacement and angular velocity.
we can plot by using marker command to determinre size and shape we given 'o' for bob,
gcf command is a current figure handle helps to get the all frames.
finally, we will make a animation video by using the movie command,by saving by videofile by using the videowriter and save the video in avi format.
clear all;
close all;
clc;

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


% intial condition
theta_0=[0;3];

%time points
t_span=linspace(0,20,500);

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

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


theta2= results(:,1);

for i=1:length(theta2)
   theta_x=theta2(i);

   x0=0;
   y0=0;

   x1=l*sin(theta_x);
   y1=-l*cos(theta_x);

figure(2)
plot([-1.5 1.5],[0 0],'color','k','linewidth',10);
hold on
plot([x0 x1],[y0 y1],'color','b','linewidth',4)
hold on
plot(x1,y1,'o','markers',25,'markerfacecolor','r');
hold off
grid on
axis([-1.5 1.5 -1.5 0.5]);
pause(0.0003)
M(ct)=getframe(gcf);
ct=ct+1;
end

movie(M)
videofile=VideoWriter('pendulum.avi');
open(videofile)
writeVideo(videofile,M)
close(videofile)





Result:
Figure (1) represents the plot between the velocity and time and the same is attached here.
Figures(2) have the animation plot reprsents the pendulum oscillations .
while writing the programming some errors are faced, which is solved and errors are attached.
Animation is saved in youtube and the same is attched through link.
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.