When a gun is triggered, it will generate a counterforce on the shooter, and this is called a recoil force. This recoiling force is the reaction force to the accelerating bullet and the momentum transfer caused by exploding gasses.


The objective of this project is to calculate the time of recoil numerically and analytically using ODEs. Using these methods, we can verify the strength of our mathematical model representing a system – In this case, the gun.

Steps to be done:

  1. First, we will solve the problem analytically using a Forced Harmonic Oscillator function and plot the velocity vs. time graph.
  2. Then we will solve the problem numerically using:
    • Runge-Kutta (4,5) method for solving ODEs
    • Impulse formulation using a transfer function
  3. Compare the results.

To solve the problem numerically, we use MATLAB.

Problem Statement:

Solving the system analytically:

To represent this system, we use the Forced Harmonic Oscillator function which is of the form,

In our specific case let us assume:

Mass of the gun = 1 kg

Spring coefficient (K) = 2000 N/m

Damping force = 0.1

When you fire the gun, pressure builds up inside the barrel for a short amount of time as the bullet moves from the closed chamber to open. When the bullet exits the gun, the force falls. This force that acts for a short duration is called as pulse function. This pulsing function reacts by causing the gun to recoil and we going to calculate the recoil time. Let us assume that this pulse function lasts for 0.7 milliseconds. So the ODE can be broken into 2 parts when the pulsing function is present and when it is not present,

Solution for the first part of equation is,

To find the solution to the next part of the combined D.E, the x(0.7 msec) and x’(0.7 msec) needs to be found.

Solution for 2nd part of equation,

Plot of the function is as follows,

The combined equation for the system

Plot of the function is as follows,

Distance vs time plot (pulse forcing function).

And the duration of the recoil cycle is approximately 0.0133 seconds.

Solving the system numerically:

Now that the result has been attained using an analytical method, the next step will be to verify the results using 2 numerical models:

  1. Runge-Kutta (4,5) method for solving ODEs
  2. Impulse formulation using a transfer function

Runge-Kutta(4,5) – ODE 45 Method using MATLAB

Range-Kutta(4,5) method is a numerical method which uses higher order Taylor series approximation to predict the solution of the ODE. (4,5) refers to the fact that the equation uses up to fourth order Taylor series to approximate the numerical scheme.This method is an explicit numerical integration and is inherently more accurate than lower order schemes. However, the input equation to be solved should be given as a set of first order coupled equations. 

Screen capture from MATLAB with code, execution and velocity and displacement plots.

From here it can be observed that the time for recoil is approximately 0.013 sec.

Impulse formulation using a transfer function

To further verify the solution, it would also be beneficial to show that the results are consistent and converge. This can be proved using the impulse dynamic system model.

The Impulse solver is a tool/function offered by MATLAB. It takes in information about the system in the form of its transfer function and subjects it to an impulse at any given time instant and measures its response afterwards. The answer is formulated in the form,

Screen capture from MATLAB showing code, execution, and displacement vs time plot for dynamic system model.

From here it is observed that the time of recoil is found to be approximately 0.013 sec.


The results from the various analysis can be compared with regards to time and interpreted,

Name of methodology used Time of recoil
Analytical (pulse forcing function) 0.0133
Numerical (ODE solver analysis) 0.013
Dynamic system (impulse function) 0.013

As you can see here, the solutions obtained are approximately equal.


From here it is clear that the ODE representing our system is robust since the results are very consistent. From the results, you can deduce that the time of recoil calculated analytically is more accurate, giving a solution up to four decimal places. One might ask, why then do we use numerical methods?

The answer is quite simple – solving an equation analytically is complex as compared to numerical methods. In numerical methods, you solve an equation by finding one approximated value and then proceed to the next step to find the next value. This is repeated until we arrive at the desired value. This is called a time-marching. In analytical methods, you derive a function or a formula and substitute the variable with the necessary values to find the desired result. You don’t have to find a string of preceding values but rather jump straight to the desired point. While this sounds easy, the catch is that deriving the function to solve analytically is a complicated process, and in some cases, deriving a function would be impossible. Some ODEs/PDEs cannot be solved analytically. In such cases, numerical methods are more convenient.

Let us assume that you are designing a gun with minimal recoil force. To create such a gun, you will have to calculate the different recoil velocities to see which value will work efficiently. Doing this by analytical methods will consume a lot of time but numerically, you can just change the value in the code and you will be able to analyse different values in a matter of minutes. This is another major reason why numerical methods are preferred widely.


Get a 1-on-1 demo to understand what is included in the CAE course and how it can benefit you from an experienced career consultant.

Request a Demo Session

These courses will launch your career in mechanical engineering

See all


See all

© 2022 Skill-Lync Inc. All Rights Reserved.