Challenge

Week 3 - Solving second order ODEs

AIM:

  • To 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.

  • To write a program in Matlab that will simulate the pendulum motion.

STEP-WISE EXPLANATION(PROCEDURE):

  1. Start the program using clear all,close all and clc.
  2. Provide the values of b,g,L and m .

    L=1 metre,

    m=1 kg,

    b=0.05.

    g=9.81 m/s2.

  3. Simulate the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.Using the command theta_0=[0;3] and t_span=linspace(0,20,500).
  4. Now a program function is written to solve the ODE .The Program is available in the program sub-division and a notepad file has also been attached.
  5. An Alternative approach to solve the second order ODE is to use this command [t,results]=ode45(@(t,theta) [theta(2);(-b/m)*theta(2)-(g/l)*sin(theta(1))],t_span,theta_0);
  6. Here,ode45 is a pre-written code in matlab to solve nonstiff differential equations of medium order.
  7. After Writting the program function to solve ODE,We plot the results for various positions of the ball with respect to time.The output of this plot has been attached under the name ODE_challenge.
  8. Now,The ODE has been solved and the required plot is generated.
  9. We now write a program to create the animation of the pendulum.
  10. For creating an animation of the pendulum a looping statement is required,hence we provide appropriate commands.
  11. The co-ordinates of the pendulum is provided.
  12. The basic idea at the end point of the pendulum is to swing in the axis at second and third quadrant ,hence we use the commands as stated.[x1=l*sin(results(i,1));y1=-l*cos(results(i,1));]
  13. Now,After providing the coordinates for the pendulum ,we use plotting commands to simulate a pendulum.
  14. Initially we plot the suspending pendulum with a line,next we use the marker command '-o' to create a circle to depict the pendulum.
  15. Adding markers to a line plot can be a useful way to distinguish multiple lines or to highlight particular data points.
  16. After defining the axis,we end the looping statement.
  17. Finally,We'll need to create the animation of the pendulum.
  18. Hence,we create a videofile with appropriate commands.The Videofile of the pendulum has been attached under the name of pendulum_file.

PROGRAM CODE:

  • Program for Solving a second order Ordinary Differential Equation that describes the position of the "ball" with respect to time.
    %Solving a Second Order ODE
    clear all
    close all
    clc
    %b=damping coefficient.
    b=0.05;
    %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;
    %The motion between 0-20 sec,for angular displacement=0,angular velocity=3 rad/sec at time t=0.
    theta_0=[0;3];
    %timepoints
    t_span=linspace(0,20,500);
    %solve ODE
    %[t,results]=ode45(@(t,theta) [theta(2);(-b/m)*theta(2) - (g/l)*sin(theta(1))],t_span,theta_0);
    [t,results]=ode45(@(t,theta) ode_function(t,theta,b,g,l,m),t_span,theta_0);
    %Plotting
    plot(t,results(:,1))
    hold on
    plot(t,results(:,2))
    ylabel('position')
    xlabel('time')
  • Program code for ODE Function
function [dtheta_dt]= ode_function(~,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
  • Program code to create animation of the pendulum

%second order ODE

clear all
close all
clc
%b=damping coefficient.
b=0.05;
%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;
%The motion between 0-20 sec,for angular displacement=0,angular velocity=3 rad/sec at time t=0.
theta_0=[0;3];
%timepoints
t_span=linspace(0,20,25);
%solve ODE
%[t,results]=ode45(@(t,theta) [theta(2);(-b/m)*theta(2) - (g/l)*sin(theta(1))],t_span,theta_0);
[t,results]=ode45(@(t,theta) ode_function(t,theta,b,g,l,m),t_span,theta_0);
%Animation of Pendulum
%looping
ct=1;
for i=1:length(t_span)
x0=0;
y0=1;
x1=l*sin(results(i,1));
y1=-l*cos(results(i,1));
%plotting
hold off
plot([x0 x1],[y0 y1],'color','r','linewidth',3)
hold on
plot(x1,y1,'-o','MarkerSize',15,'MarkerEdgeColor','red','MarkerFaceColor',[1 .9 .9])
axis([-1.5 1.5 -1.5 1])
M(ct)=getframe(gcf);
ct=ct+1;
end
movie(M)
videofile=VideoWriter('Pendulum_Challenge_Animation.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
ERRORS FACED AND STEPS TAKEN TO OVERCOME IT:
  • While creating the animation of pendulum,the animation file was not created.So I checked for the error and found out that the number of iterations had to be lesser in order to create the video file.In the command t_span=linspace(0,20,25) ,The number of iterations was set to 25 for running the video successfully.
  • While defining the coordinates of the pendulum,the end points are x1=l*sin(results(i,1)); y1=-l*cos(results(i,1));Here I initially didnt provide the negative sign for y1,Later I found the error and corrected it after understanding the concept.Hence later I was able to execute the desired motion of the pendulum.
RESULT:
  • A Program for Solving a second order Ordinary Differential Equation that describes the position of the "ball" with respect to time was written.
  • A Program function to solve a second order ODE was written.
  • A Program to create animation of the pendulum was written and compiled successfully.
  • All required files has been attached below.

OUTPUTS:

  • The Programming codes for solving a second order ODE and to create an animation of the pendulum has been attached.
  • A plot for describing the position of the ball with respect to time was generated and has been attached under the name ODE_challenge.png
  • An animation of the Pendulum has been created .The videofile has been attached under the name of Pendulum_File.avi
CONCLUSION:
  • A program that solves the following ODE was written and compiled successfully.
  • A program is written in Matlab to simulate the pendulum motion.
  • A detailed explanation of the program is written,from which each and every statement could be understood clearly.
  • The use of the command ode45 was understood.
  • It can be understood that Matlab is an essential tool which can solve differential equations of any order.
  • To conclude,with good understanding of all the commands and codes any differential equation can be solved at ease.
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.