Project

solving second order ODE

1.0 Problem definition: write a program that solves the following ODE. The ODE represents the equation of motion of a simple pendulum with damping.

\"\"

1.2. Background : In mathematics, an ordinary differential equation (ODE) is a differerntial equation containing one or more functions of one independent variable and the derivatives of those functions. The term ordinary is used in contrast with the term partial differential equation which may be with respect to more than one independent variable.

1.3 Importance :

Ordinary differential equations (ODEs) arise in many contexts of mathematics and social and natural sciences. Mathematical descriptions of change use differentials and derivatives. Various differentials, derivatives, and functions become related via equations, such that a differential equation is a result that describes dynamically changing phenomena, evolution, and variation. Often, quantities are defined as the rate of change of other quantities (for example, derivatives of displacement with respect to time), or gradients of quantities, which is how they enter differential equations.

Specific mathematical fields include geometry and analytical mechanics . Scientific fields include much of physics and astronomy (celestial mechanics), meterology (weather modelling),chemistry (reaction rates), biology (infectious diseases, genetic variation), ecology and population modelling(population competition),economy (stock trends, interest rates and the market equilibrium price changes).

Many mathematicians have studied differential equations and contributed to the field, includingNewton,Leibniz, the Bernoulli family, Ricati, Clairaut, d\'Alembert, and Euler.

A simple example is Newton\'s second law of motion — the relationship between the displacement x and the time t of an object under the force F, is given by the differential equation.

M(d2x(t)/dt2) = F(x(t))

which constrains the motion of a particle of constant mass m. In general, F is a function of the position x(t) of the particle at time t. The unknown function x(t) appears on both sides of the differential equation, and is indicated in the notation F(x(t)).

1.4 Theme and Target : our goal is to solve the ode and plot the animation of the pendulum using matlab, we are using the function ode 45 for solving the ode.

1.5 Program : the program consists of 2 parts , one is for plotting and solving the ode and the other part is function which are using and calling during the sovling of the ode.

codes for function:

function [dtheta_dt] = ode_func(t,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
  • The above given codes are for the function to solve the ode,here we are giving the inputs and breaking the 2nd order ode to 2 simple first order odes.

codes for plotting and animation of the pendulum:

clear all 
close all 
clc
% inputs 
b = 0.05;
g = 9.81;
l = 1;
m = 1;
% intial conditions
theta_0 = [0;3];
% time plot 
t_span = linspace(0,20,1000);
% solving the ode
[t, results] = ode45(@(t,theta)ode_func(t,theta, b, g, l, m), t_span, theta_0);
plot (t, results(:,1),\'linewidth\',2)
axis([-1 40 -4 4])
hold on
plot(t, results(:,2),\'linewidth\',2)
xlabel(\'time\')
grid on 
% animation 
 figure
ct = 1;
for i = 1: length(t_span)
    x_start = 0;
    y_start = 0;
    x_final = l*sin(results(i,1));
    y_final = -l*cos(results(i,1));
    plot([-1 1],[0 0],\'linewidth\',2,\'color\',\'g\')
    hold on
    plot([0 0],[0 -1.3],\'linewidth\',2,\'color\',\'k\',\'linestyle\',\'--\')
    hold on 
    plot ([x_start x_final],[y_start y_final],[-0.5 -0.5],[0 0],\'linewidth\',3,\'color\',\'b\')
     hold on
    plot(x_final, y_final,\'Marker\',\'O\',\'MarkerSize\',30,\'MarkerFaceColor\',\'r\')
    hold off
    axis([-1.5 1.5 -1.5 0.5])
    grid on 
    pause(0.0003)
    M(ct) = getframe(gcf);
    ct = ct+1;
end
movie(M);
videofile = VideoWriter(\'pendulum_animation_ode\',\'Uncompressed AVI\');
open(videofile);
writeVideo(videofile,M);
close(videofile);
  • here the given inputs to the codes are b,g,l,m where

    g = gravity in m/s2,

    L = length of the pendulum in m,

    m = mass of the ball in kg,

    b=damping coefficient.

  • we wre giving the intial condition as theta[0 3] which means that the intial displacement is 0 and initial velocity as 3 rad/sec.
  • next we are using linspace command to generate values 1000 values between 0 and 20 second for the time span.
  • now we are using the function for solving the ode and it is stored in results with the coresponding t value.here we are using ode45 which is a inbuilt solver avaliable in matlab for solving ode. Here we are obtaining the angular velocity and angular displacement.
  • now we are using the plot command for plotting the displacement and the angular velocity.
  • at the first we are plotting the angular displacement as the result containts 2 ste of values that is diplacement and velocity, it starts from 0 and oscillates and slowy reduces.
  • next we plot the angular velocity, it starts from 3 and slowly reduces.
  • now we are going to create a animation for the pendulum with the values which we obtained from solving the equation.
  • now we create a figure window, and initiate the counter to 1,
  • now we begin with the forloop, so it runs the loop upto the values of time span that is 1000 time for this perticular program.
  • now the upper end is fixed so we initiate x_start and y_start to 0, and we calculate the end points x_final and y_final as the end points are moving with respect to the displacement and velocity. here one cos term is given in negative so as the pendulum has to move in oppsite direction.,else the pendulum would oscillate in updide down condition.
  • now we are plotting a straight line in green color which acts as a fixed support of the pendulum.
  • we are also plotting a black broken line which marks the mean position of the pendulum.
  • here we are using the hold on command after every plot since we need the frames as we have plotted in different plot command we require all the plots in a single frame.
  • now we plot a straight line which is blue in color, which acts as a string of the pendulum, which connects the staeting and ending points that is (x_start y_start and x_final y_final) .
  • now we are plotting these end points, along with the bob, for the bob we make use of marker comand \'o\' which creates a bob and we give the size and color for the same.
  • here we are using the holf off command to avoid the muticoloring produced by animation.
  • we mention the axis limit.
  • we store all the frames in an array M
  • and now we create a movie file with the help of movie command the video writer command, where we mention the video file name and the attribute type of the video, and the animation is created.

1.6 Plot

\"\"

the above plot shows the angular displacement(blue) and the angular velocity (orange) with the time interval of 20 sec with a damping coefficient of 0.05, mass as 1kg and length of the string as 1 meter.

1.7 Animation :

the above animation reprents animation of the pendulum for the ode which we had solved using ode45 for mass of 1kg, length of 1 meter with a damping coeffient of 0.05.

1.8 Errors and Rectification :

1) \"\"

this error was a syntax caused due to missing of parenthesis, this was rectified by adding the coresponding parenthesis.

2) \"\"

this error was caused due to missing multiplication symbol in the function, this was rectified by adding the symbol at the right position.

3)

\"\"

this error was caused due an extra comma, so it was rectified by removing it.

4)

\"\"

this error was caused as the marker option was not used and the hold on option was not turned off, this was solved by using the marker option and hold off command.

5)

\"\"

this error was caused due to missing of 2 hold on commands , so this was rectified using 2 more hold on commands which held the other lines in a single frame.

2.0 Result : We have solved the 2nd order ode function using matlab witha a inbuilt solver ode45, and obtained the plot and animation of the ode which was solved. We can solve odes with the help of matlab by splitting the odes into smaller first order odes.

3.0 conclusion : we have solved the ode and obtained the plot and animation of the same, but from the graph we can observe that the velocity and displacement are discontinued and does not comes to a rest, this is because the mass is more and the damping force is less, and we have specified the time span as 20 sec as the pendulum would not come to rest in 20 sec the same would be observed in animation as well.

  • either if we in increase the damoing coefficient or
  • if we decrease the mass or
  • if we extend the time period for the same parameters then we can observe the coinciding of the velocity and displacement line.

\"\"

then the plot would look similar like this, so here the mass was altered to 0.1kg, here the velocity and the displacement nearly damps to zero at 20 sec time interval.

so we can also observe the influence of mass and damper coefficient effect on the pendulum and the results of the equations.

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.