Challenge

Week 3 - Solving second order ODEs

Solving 2nd order ode’s using MATLAB

Second Order Differential Equation

Introduction:

Second order differential equation is a specific type of differential equation that consists of a derivative of a function of order 2 and no other higher-order derivative of the function appears in the equation. It includes terms like y'', d2y/dx2, y''(x), etc. which indicates the second order derivative of the function. Generally, we write a second order differential equation as y'' + p(x)y' + q(x)y = f(x), where p(x), q(x), and f(x) are functions of x. We can solve this differential equation using the auxiliary equation and different methods such as the method of undetermined coefficients and variation of parameters.

The differential equation y'' + p(x)y' + q(x)y = 0 is called a second order differential equation with constant coefficients if the functions p(x) and q(x) are constants and it is called a second-order differential equation with variable coefficients if p(x) and q(x) are not constants. In this article, we will understand such differential equations in detail and their different types. We will also learn different methods to solve second order differential equations with constant coefficients using the various methods with the help of solved examples and finding the auxiliary equation.

A differential equation is an equation that consists of a function and its derivative. A differential equation that consists of a function and its second-order derivative is called a second order differential equation. Mathematically, it is written as y'' + p(x)y' + q(x)y = f(x), which is a non-homogeneous second order differential equation if f(x) is not equal to the zero function and p(x), q(x) are functions of x. It can also be written as F(x, y, y', y'') = 0. Further, let us explore the definitions of the different types of the second order differential equation.

Pendulums have many applications and were utilized often before the digital age. They are used in clocks and metronomes due to the regularity of their period, in wrecking balls and playground swings, due to their simple way of building up and keeping energy.

A simple pendulum consists of a point mass suspended on a string or wire that has negligible mass. If the pendulum weight or bob is pulled to a relatively small angle from the vertical and let go, it will swing back and forth at a regular period and frequency. Figure 1 shows a simple pendulum model

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. The primary forces acting on the bob are the gravitational force that makes it move in the first place and the force exerted by the string to keep it moving along a circular path. In addition, there may be a damping force from friction at the pivot or air resistance or both. We will construct a model to describe how the angle theta of the pendulum varies as a function of time t. Let s(t) be the distance along the arc from the lowest point to the position of the bob at time t, with displacement to the right considered positive. Let theta(t) be the corresponding angle with respect to the vertical. The figure shows tangential and radial components of the gravitational force on the pendulum bob. The radial component is exactly balanced by the force exerted by the string, so the only relevant force producing the motion is the tangential component of the gravitational force. For the moment, we ignore the damping force, if any.

Program explanation

  • Before writing the code, it's important to start with clear all, close all 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 input and output from the Command Window display, giving you a "clean screen
  • Next step is to define the inputs and variables. The damping coefficient, gravity, length of the spring and mass of the bob have all been defined.
  • Next step is to define the initial conditions, the angular displacement and ongular velocity have been defined. Timespan has also been defined.
  • 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 conditions. [t, results] means that the results that have been calculated with respect to time.
  • After calculating the results, a 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 for time. The legend for the graph and the axis labels have been defined. Figure 5 shows the graph obtained
  • A function is a group of statements that together perform a task. In MATLAB, functions are defined in separate files. The name of the file and of the function should be the same. Functions operate on variables within their own workspace, which is also called the local workspace, separate from the workspace you access at the MATLAB command prompt which is called the base workspace.

Program(1)

% ode_func;

function[dtheta_dt]=ode_func(t,theta,b,g,l,mass)

theta1=theta(1)

theta2=theta(2)

dtheta1_dt=theta2;

dtheta2_dt=-(b/mass)*theta2-(g/l)*sin(theta1);

dtheta_dt=[dtheta1_dt;dtheta2_dt];

end

Program(2)

%program to solve 2nd order odes using matlab

clear all;

close all;

clc;

% inputs from challenge

l=1; %length of pendulum spring (m)

m=0.1; % mass of pendulum bob (kg)

b=0.05; %damping co efficient

g=9.81;%gravitational acceleration (m/s^2)

% inital values/conditions

theta_0=[0;3];

%line period given to 0-20 sec

t_span=linspace(0,20,500);

%ode solution of this problem

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

%ploting the results

figure(1)

hold on

plot(t,results(:,1))

plot(t,results(:,2))

title('damped harmonic motion of pendulum')

legend('angular displacement','angular velocity')

xlabel('time in (seconds)')

ylabel('plot')

% for looping the pendulum

%before starting th loop, a counter value was defined

ct=1;

for i=1:length(results(:,1))

x_1=0;

y_1=0;

x_2=l*sin(results(i,1));

y_2=-l*cos(results(i,1));

% for creating animation

figure(2)

plot([-1 1],[0 0],'LineWidth',5,'Color','g')

hold on

plot([x_1 x_2],[y_1 y_2],'LineWidth',3)

hold on

plot(x_2,y_2,'o','MarkerSize',25,'MarkerFaceColor','b')

title('animation of transient behaviour of a pendulum')

axis([-1.5 1.5 -1.5 1])

pause(0.02)

hold off

M(ct)=getframe(gcf);

ct=ct+1;

end

% for creating movie of the pendulum

movie(M)

videofile=VideoWriter('pendulum animation.avi','Uncompressed AVI');

open(videofile)

writeVideo(videofile,M)

close(videofile)

plots(1)

Plot(2)

errors faced while Programming

  • The first error I had in the code for this project in formula assign y as two times
  • my program show the error invalid color error in odes program
  • I accidentally typed Movie' instead of 'movie' since Matlab is a case sensitive program language,
  • I assign the value for m(mass of th bob) i again assigned movie(M)
  • I enterd linewidth and Axis in caps so it shows invalid text character commands
  • Once I rewrote the command the code worked fine.
  • Error in finding initial values system fixing I so I changed and its work

Steps taken to overcome those errors

  • Before execute the program prepare your program in writing work.
  • Try to make errors to over come the right program to memorise the mistakes
  • of mistakes can you perfect when you the program writer.
  • When you execute the program be patio’s and cool with mind on keyboard
  • Prepare your own program for n no.of trails with n no .of errors with solutions

Animation video will be attached at google drive link

https://drive.google.com/file/d/1h_yzUa6w61V_mhvWVei4sXnlMXFYVllF/view?usp=sharing

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.