Challenge

Week 3 - Solving second order ODEs

Aim:

1)To write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping;

2)To wrtite a matlab program to stimulate pendulum motions;

Theory:

A simple pendulum consists of a mass m hanging from a string of length L and fixed at a pivot point P. When displaced to an initial angle and released, the pendulum will swing back and forth with periodic motion. ... with being the natural frequency of the motion.

The figure at the right shows an idealized pendulum, with a "massless" string or rod of length L and a bob of mass m. The open circle shows the rest position of the bob. When the bob is moved from its rest position and let go, it swings back and forth. The time it takes the pendulum to swing from its farthest right position to its farthest left position and back to its next farthest right position is the period of the pendulum.

We make the simplest possible assumption about the damping force, that it is proportional to velocity. Since arc length and central angle are themselves proportional (with proportionality constant L), it makes no difference whether we use linear or angular velocity. Having selected theta as our dependent variable, we will represent the damping as proportional to angular velocity, say, -b (d theta / dt). The negative sign is because the damping force has to be opposite the direction of motion. When we include this term in the model, our equation becomes

When we bring all the terms to the left-hand side, our model equation becomes

Objective:

1) Write a program in MatlabOctave 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.

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

3)Understanding ode45 command in Matlab.

4)Ploting the graph.

Body of content

%we have
%differntial equation of simple pendulum
%(d^2_theta/dt^2)+(b/m)d_theta/dt+(g/l)

%dtheta1_dt=theta2;
%dtheta2_dt= -(b/m)*theta2-(g/l)*sind(theta1);


%theta=displacement

%damping coefficient
b=0.05;

%mass
m=0.1; %kg

%specific gravity
g=9081; %(m/s^2)
%length of the string
l=1; %m

%initial condition

%initial angular disp=0;initial angular vel=3rad/sec
theta_0=[0;3];

%clc
%close all
%clear all


%we have
%differntial equation of simple pendulum
%(d^2_theta/dt^2)+(b/m)d_theta/dt+(g/l)

%dtheta1_dt=theta2;
%dtheta2_dt= -(b/m)*theta2-(g/l)*sind(theta1);


%theta=displacement

%damping coefficient
b=0.05;

%mass
m=0.1; %kg

%specific gravity
g=9081; %(m/s^2)
%length of the string
l=1; %m

%initial condition

%initial angular disp=0;initial angular vel=3rad/sec
theta_0=[0;3];

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

%second order differential equation solving
[t,results]=ode45(@(t,theta) fode_func(t,theta,b,m,g,l),t_span,theta_0);

%plotting time vs results

plot(t,results(:,1))
hold on
plot(t,results(:,2))



title("numerical solution of second order differential equation")

xlabel('time(s)')
ylabel('theta(displacement)')

grid on ;

Errors occured and faced duiring execution

Stimulation of simple pendulum of position of ball wrt time and clear the errors and execution

%stimulation
% below code will help to animate the motion of simple pendulum

figure(2)
ct=1;
for i=1:length(results(:,1))
x0=0;
y0=0;
plot([-1 1],[0 0],'linewidth',4,'color','k');
hold on

%position of ball
x1=l*sin((results(i,1)));
y1=-l*cos((results(i,1)));

%plotting length of pendulum
plot([x0 ,x1],[y0 y1],'LineWidth',2,'Color','r');

%plotting length of fixxed end
plot(x0,y0,'-s','MarkerEdgeColor','k','MarkerFaceColor','b','MarkerSize',10);

%plotting of ball
plot(x1,y1,'-o','MarkerEdgeColor','k','MarkerFaceColor','c','MarkerSize',20);

hold off
axis([-1.4 1.7 -1.5 0.5]);
grid on
titlestring=['pendulum motion at time t=',num2str(t(i)),'seconds'];
title(titlestring,'fontsize',10);

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

end

%code to animate the motion
movie(M)
videofile=VideoWriter('odesimplependulum.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

Code wrt function used to solve ode convertiong sode to fode

%ODE FUNCTION
function[dtheta_dt]=fode_func(t,theta,b,m,g,l);

%we have theta1 and theta2

theta1=theta(1);
theta2=theta(2);

%differentiation of above theta1 & 2

dtheta1_dt=theta2;
dtheta2_dt= -(b/m)*theta2-(g/l)*sind(theta1);
dtheta_dt=[dtheta1_dt;dtheta2_dt];

end

Yopu tube link for the stimulation of simple pendulum animated plot

https://youtu.be/04WAAEtus3M

conclusion

1)Ode has been solved using ode45 in matlab

2)Motion of simple pendulum as been animated

3)From the above example we understand the concept of damping systems using second laws of Newton.

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.