Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

ME-518

SUMER MOHAN SINGH BASSI


2022MEB1351

LIBRARIES IMPORTED
1. NumPy (import numpy as np): NumPy is a fundamental library for numerical computations in
Python . It provides support for arrays and matrices, along with mathematical functions to
operate on them .In this code, it's used for handling numerical data efficiently, especially for
storing time, displacement, velocity, and acceleration values in a particular format.
2. Matplotlib.pyplot (import matplotlib.pyplot as plt): Matplotlib is a popular library for
creating static, animated, and interactive plots and visualizations in Python.The pyplot
module from matplotlib provides a simple interface for creating charts and graphs. In this
code, it's used for plotting the displacement vs. time graph.
3. Sympy (from sympy import sympify, symbols, integrate, diff): Sympy is a Python library for
symbolic mathematics. It takes as input a mathematical expression as a string and using
sympify converts it into a symbolic mathematical expression.

CODE

1. User Input: The user gives inputs for various parameters such as mass (m), damping
coefficient (c), spring constant (k), α, Δt, initial velocity (v₀), initial time (t₀), initial
displacement (x₀), and end time (tₑ).
2. Function f(t): A function f(t) is defined, which represents an external force as a function
of time t. In the current code, it's set to np.sin(t). It can be replaced with any other
function as needed.
3. Data Initialization: Arrays (time, disp, vel, and acc) are initialized to store time,
displacement, velocity, and acceleration values. The initial values (ti, xi, v0, etc.) are
assigned to the corresponding arrays. These values are then used to plot the
corresponding graphs.
4. Differential Equation Solver: This code uses the generalized alpha method to solve a
second order differential equation .A loop runs n times, where n is calculated based on
the difference between te (end time) and ti (initial time) divided by Δt (time step). In
each iteration of the loop, the code calculates acceleration (a1 and a2) and updates
displacement and velocity using the finite difference method. The first part of the
acceleration a1 is calculated using the parameter values and the external force f(t). The
second part of the acceleration a2 is also calculated using the updated values. The
displacement and velocity are updated based on the calculated accelerations. Time is
also updated in each iteration.
5. Plotting: After solving the differential equation, the code uses matplotlib.pyplot to create
a plot of displacement vs. time. Labels, title, and grid lines are added to the plot to make
it more informative.
6. Conclusion: The code concludes by displaying the plot to visualize the displacement vs.
time graph.

1. The solution we get, when we use x(t)=sin(t) to construct the differential equation.

2. The solution we get for the equation : x”+7x’+12x=0 for the initial conditions: x(0)=2
and x’(0)=-7.

3. For the same equation that is mentioned above:

a) α = 0 and Δt = 0.001

b) α = 1 and Δt = 0.000001
4. After substituting the required values in the expression, the values of the critical
velocity which we get from the analytical method as well as the numerical method
are approximately the same.

You might also like